• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / tomo_mapper / 21944637261

12 Feb 2026 11:24AM UTC coverage: 86.453% (+0.1%) from 86.341%
21944637261

push

github

web-flow
Merge pull request #77 from kit-data-manager/improv_code_cleanup

Various code fixes and cleanups
Cleaner type hints
Cleaner inheritance
More None checks

parse functions (parse_setup, parse_run, parse) do not return raw parse result anymore (was never used anywhere anyway)

Code now passes a static code check such as pyright.

159 of 198 new or added lines in 30 files covered. (80.3%)

3 existing lines in 3 files now uncovered.

2425 of 2805 relevant lines covered (86.45%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.59
/src/parser/impl/TxtParser.py
1
import logging
1✔
2
from typing import Optional
1✔
3

4
from PIL import Image
1✔
5

6
from src.Preprocessor import Preprocessor
1✔
7
from src.model.ImageMD import ImageMD
1✔
8
from src.model.SchemaConcepts.SEM_Image import SEM_Image
1✔
9
from src.parser.ImageParser import ImageParser, ParserMode
1✔
10
from src.parser.mapping_util import map_a_dict
1✔
11
from src.resources.maps.mapping import textparser_tomo_tescan
1✔
12
from src.util import input_to_dict, configparser_keep_keystring
1✔
13
import configparser
1✔
14

15

16

17
#TODO: would this have any benefit from replacing with tifffile lib?
18

19
class TxtParser(ImageParser):
1✔
20

21
    internal_mapping = None
1✔
22
    def __init__(self, mode):
1✔
23
        if mode == ParserMode.TOMO:
1✔
24
            m1 = input_to_dict(textparser_tomo_tescan.read_text())
1✔
25
            self.internal_mapping = m1
1✔
26
        super().__init__(mode)
1✔
27

28
    @staticmethod
1✔
29
    def expected_input_format():
1✔
30
        return "text/plain"
1✔
31

32
    def parse(self, file_path, mapping):
1✔
33
        input_md = self._read_input_file(file_path)
1✔
34
        if not input_md:
1✔
35
            logging.warning("No metadata extractable from {}".format(file_path))
×
NEW
36
            return None
×
37

38
        if not mapping and not self.internal_mapping:
1✔
39
            logging.error("No mapping provided for image parsing. Aborting")
×
40
            exit(1)
×
41
        mapping_dict = mapping if mapping else self.internal_mapping
1✔
42
        image_md = map_a_dict(input_md, mapping_dict)
1✔
43
        #print("image_md: ", image_md)
44

45
        Preprocessor.normalize_all_units(image_md)
1✔
46
        Preprocessor.normalize_all_datetimes(image_md)
1✔
47

48
        if self.mode == ParserMode.TOMO:
1✔
49
            image_from_md = self._create_tomo_image(image_md, file_path)
×
50
        else:
51
            image_from_md = ImageMD(image_metadata=SEM_Image(**image_md), filePath="")
1✔
52

53
        #print("image_from_md: ", image_from_md)
54
        return image_from_md
1✔
55

56
    def _create_tomo_image(self, image_md, fp):
1✔
57

58
        image_md_format = {
×
59
            "acquisition_info": image_md["acquisition"],
60
            "dataset_metadata": image_md["acquisition"]["dataset"],
61
            "image_metadata": image_md["acquisition"]["dataset"]["images"],
62
            "filePath": fp
63
        }
64

65
        image_md_format["dataset_metadata"].pop("images")
×
66
        if image_md_format.get("image_metadata"):
×
67
            image_md_format["image_metadata"]["localPath"] = fp
×
68

69
        return ImageMD(**image_md_format)
×
70

71
    def _read_input_file(self, file_path) -> Optional[dict]:
1✔
72
        """
73
        Reading input may be done with a predefined tag or without. In the latter case we try to extract from all tags and use the joint dictionary for mapping.
74
        :param file_path: image file path
75
        :param tagID: tag to extract from, may be None
76
        :return: data from extracted tag(s) as dict
77
        """
78
        #print(f"I am trying to read a {file_path}")
79

80
        config = configparser.ConfigParser(allow_no_value=True, delimiters=(" "))
1✔
81
        config.optionxform = configparser_keep_keystring
1✔
82

83
        # Read the .txt file either as UTF-8 or a different byte format using errors="replace"
84
        with open(file_path, "r", encoding="utf-8", errors="replace") as file:
1✔
85
            md = file.read()
1✔
86

87
        return input_to_dict(md)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc