segtformats 0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ """
2
+ Manipulating segmentation formats (Alto, Page, JSON)
3
+
4
+ Command-line utilities
5
+
6
+ - `alto_to_page_xml`: Alto → Page conversion.
7
+ - `alto_to_json`: Page → JSON conversion.
8
+ - `page_xml_to_json`: Page → JSON conversion.
9
+ - `anyseg_to_json`: Detect input format and convert to JSON.
10
+ - `json_to_page_xml`: JSON → Page conversion.
11
+ - `json_to_json`: Various transformations on JSON metadata, including repairs (region boundaries, line-to-region assignments).
12
+ - `anyseg_to_ascii`: Render segmentation metadata on the terminal.
13
+
14
+ Help on any command with:
15
+
16
+ ```
17
+ python3 -m segtformats.<command> -h
18
+ ```
19
+
20
+ How to use the library functions:
21
+
22
+ ```
23
+ from segtformats import segtformats as sgf
24
+ help( sgf )
25
+ ```
26
+
27
+ """
28
+ import logging
29
+
30
+ logging_format="%(asctime)s - %(levelname)s: %(filename)s: %(funcName)s - %(message)s"
31
+ logging_levels = {0: logging.ERROR, 1: logging.WARNING, 2: logging.INFO, 3: logging.DEBUG }
32
+ logging.basicConfig( level=logging.INFO, format=logging_format, force=True )
33
+ logger = logging.getLogger(__name__)
34
+
35
+ def set_logging_level( verbosity ):
36
+ if verbosity != 2:
37
+ logging.basicConfig( level=logging_levels[verbosity], format=logging_format, force=True )
38
+
39
+
@@ -0,0 +1,2 @@
1
+ import sys
2
+ help(sys.modules['segtformats'])
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Alto -> JSON conversion.
4
+ """
5
+
6
+ import sys
7
+ import json
8
+ import re
9
+ from pathlib import Path
10
+
11
+ import fargv
12
+ fargv.util.verbosity=1 # for --help to display option descriptions
13
+ from fargv import FargvPositional, FargvChoice
14
+ from jsonschema import validate
15
+
16
+ from . import segtformats as sgf
17
+ from . import set_logging_level, logger
18
+
19
+
20
+ if __name__ == '__main__':
21
+ main()
22
+
23
+ def main():
24
+
25
+ p = {
26
+ 'file_paths': FargvPositional(default=[]),
27
+ 'out': FargvChoice(['','auto'], description="Set to 'auto' for output to filename <input stem>.<output_suffix>; leave empty for standard output."),
28
+ 'input_suffix': '.xml',
29
+ 'output_suffix': '.json',
30
+ 'overwrite_existing': (False, "Overwrite an existing file."),
31
+ "comment": ('',"A text string to be added to the <Comments> elt."),
32
+ "verbosity": FargvChoice(['2','0','1','3'], description="Verbosity levels: 0 (quiet), 1 (WARNING), 2 (INFO-default), 3 (DEBUG)"),
33
+ "repair": (False, "Repair a faulty dictionary (reassign lines to regions, fix region bounding boxes"),
34
+ "validate": (False, "Validate against a JSON schema."),
35
+ }
36
+
37
+ args, _ = fargv.parse( p )
38
+
39
+ set_logging_level( int(args.verbosity) )
40
+
41
+ for file_path in args.file_paths:
42
+
43
+ logger.debug(file_path)
44
+
45
+ segdict = sgf.alto_to_segmentation_dict( file_path )
46
+
47
+ if args.repair:
48
+ segdict = sgf.json_doctor( segdict, verbose=args.verbose )
49
+
50
+ # Raise an exception if invalid
51
+ if args.validate:
52
+ sgf.json_validate( segdict )
53
+
54
+ segdict_str = json.dumps( segdict, indent=2 )
55
+ if not args.out:
56
+ print( segdict_str )
57
+ continue
58
+
59
+ out_path = ''
60
+ if args.out == 'auto':
61
+ if not re.search( r'{}$'.format(args.input_suffix), Path(file_path).name):
62
+ logger.warning(f"Input file path '{Path(file_path).name}' does not match input suffix '{args.input_suffix}': output aborted.")
63
+ continue
64
+ out_path = re.sub(r'{}$'.format( args.input_suffix ), args.output_suffix, file_path )
65
+ logger.debug(f"Output file = {out_path}")
66
+
67
+
68
+ if not args.overwrite_existing and Path(out_path.exists():
69
+ logger.info("File {} exists: skipping.".format( out_path ))
70
+ continue
71
+ with open(out_path, 'w') as json_outf:
72
+ json_outf.write( segdict_str )
73
+ return 0
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ ALTO → Page conversion tool with embedded XSL stylesheet.
4
+ """
5
+
6
+ import sys
7
+ import re
8
+ from pathlib import Path
9
+
10
+ import fargv
11
+ fargv.util.verbosity=1 # for --help to display option descriptions
12
+ from fargv import FargvPositional, FargvChoice
13
+ from . import segtformats as sgf
14
+ from . import set_logging_level, logger
15
+
16
+ if __name__ == '__main__':
17
+ main()
18
+
19
+ def main():
20
+
21
+ p = {
22
+ 'file_paths': FargvPositional(default=[], description="Input file (ALTO)."),
23
+ 'out': FargvChoice(['','auto'], description="Set to 'auto' for output to filename <input stem>.<output_suffix>; leave empty for standard output."),
24
+ 'input_suffix': ('.xml', "Input file suffix."),
25
+ 'output_suffix': ('.xml', "Output file suffix."),
26
+ 'overwrite_existing': (False, "Overwrite an existing output file."),
27
+ "verbosity": FargvChoice(['2','0','1','3'], description="Verbosity levels: 0 (quiet), 1 (WARNING), 2 (INFO-default), 3 (DEBUG)"),
28
+ }
29
+
30
+ args, _ = fargv.parse( p )
31
+
32
+ set_logging_level( int(args.verbosity) )
33
+
34
+ for file_path in args.file_paths:
35
+
36
+ if not args.out:
37
+ sgf.alto_to_page_xml( file_path )
38
+ continue
39
+ out_path = ''
40
+ if args.out == 'auto':
41
+ if not re.search( r'{}$'.format(args.input_suffix), Path(file_path).name):
42
+ logger.warning(f"Input file path '{Path(file_path).name}' does not match input suffix '{args.input_suffix}': output aborted.")
43
+ continue
44
+ out_path = re.sub(r'{}$'.format( args.input_suffix ), args.output_suffix, file_path )
45
+ logger.debug(f"Output file = {out_path}")
46
+
47
+ if not args.overwrite_existing and Path(out_path).exists():
48
+ logger.info("File {} exists: skipping.".format( out_path ))
49
+ continue
50
+ sgf.alto_to_page_xml( file_path, pagexml_filename=out_path )
51
+
52
+ return 0
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env python3
2
+ #
3
+ # Repair segmentation meta-data:
4
+ # [ Alto, Page ] → [ JSON, Page ]
5
+ #
6
+ #
7
+
8
+ import sys
9
+ from pathlib import Path
10
+ import json
11
+ import re
12
+
13
+ import fargv
14
+ fargv.util.verbosity=1 # for --help to display option descriptions
15
+ from fargv import FargvChoice, FargvPositional
16
+
17
+ from . import segtformats as sgf
18
+ from . import set_logging_level, logger
19
+
20
+ if __name__ == '__main__':
21
+ main()
22
+
23
+ def main():
24
+
25
+ p = {
26
+ "file_paths": FargvPositional(default=[], description="A JSON line segmentation file (e.g <prefix>.lines.pred.json)."),
27
+ 'out': FargvChoice(['','auto'], description="Set to 'auto' for output to filename <input stem>.<output_suffix>; leave empty for standard output."),
28
+ "output_suffix": ('',"If empty, output file's suffix is determined by output format (.json or .xml)"),
29
+ "input_suffix": ('',"If empty, input file's suffix is determined by detected input format (.json or .xml)"),
30
+ "overwrite_existing": (False, "Do not overwrite existing output file"),
31
+ "repair": (False, "If True, fix semantic errors in the segmentation (line-to-region assignment, region boundaries)."),
32
+ "diagnose": (True, "If True, detect potential issue with the segmentation data, with a dry-run of json_doctor."),
33
+ "validate": False,
34
+ "output_format": FargvChoice(['auto', 'json', 'page'], description="Output format; if empty, same as detected input format.)"),
35
+ "verbosity": FargvChoice(['2','0','1','3'], description="Verbosity levels: 0 (quiet), 1 (WARNING), 2 (INFO-default), 3 (DEBUG)"),
36
+ }
37
+ args, _ = fargv.parse( p )
38
+
39
+ set_logging_level( int(args.verbosity) )
40
+
41
+ format_to_suffix = { sgf.SegFormat.PAGE: '.xml', sgf.SegFormat.ALTO: '.xml', sgf.SegFormat.JSON: '.json' }
42
+ for file_path in args.file_paths:
43
+
44
+ logger.info(f"{file_path}:")
45
+
46
+ # we need an access to the segmentation format
47
+ segdict, segmentation_format = sgf.anyseg_to_dict( file_path )
48
+
49
+ if not segdict:
50
+ print(f"Could not parse a dictionary from {segfile_path}. Skipping.")
51
+ continue
52
+
53
+ if args.diagnose and not args.repair:
54
+ segdict = sgf.json_doctor( segdict, dry_run=True )
55
+ logger.info("Diagnosis only: set --repair for repairing the file.")
56
+ continue
57
+
58
+ if args.repair:
59
+ segdict = sgf.json_doctor( segdict )
60
+
61
+ if args.validate:
62
+ if not sgf.json_validate( segdict ):
63
+ continue
64
+
65
+ segdict_str = json.dumps( segdict, indent=2 )
66
+ if not args.out:
67
+ print( segdict_str )
68
+ continue
69
+
70
+ output_format = None
71
+ # What is the output format?
72
+ if args.output_format == 'page':
73
+ output_format = sgf.SegFormat.PAGE
74
+ elif args.output_format == 'json':
75
+ output_format = sgf.SegFormat.JSON
76
+ elif args.output_format == 'auto':
77
+ output_format = segmentation_format
78
+ # What is the output file name?
79
+ output_suffix = args.output_suffix if args.output_suffix else format_to_suffix[ output_format ]
80
+ input_suffix = args.input_suffix if args.input_suffix else format_to_suffix[ segmentation_format ]
81
+
82
+ out_path = ''
83
+ if args.out == 'auto':
84
+ if not re.search( r'{}$'.format( input_suffix ), Path( file_path ).name):
85
+ logger.warning(f"Input file path '{Path(file_path).name}' does not match input suffix '{input_suffix}': output aborted.")
86
+ continue
87
+ out_path = re.sub(r'{}'.format(input_suffix), output_suffix, file_path )
88
+ logger.debug(f"Output file = {out_path}")
89
+
90
+ if not args.overwrite_existing and Path(out_path).exists():
91
+ logger.info( "File {} exists): skipping.".format(out_path))
92
+ continue
93
+ if output_format == sgf.SegFormat.JSON:
94
+ with open(out_path, 'w') as json_outf:
95
+ json_outf.write( segdict_str )
96
+ elif output_format == sgf.SegFormat.PAGE:
97
+ sgf.page_xml_from_segmentation_dict( segdict, output_file=out_path )
98
+
99
+ logger.info(f"→ {out_path}")
100
+
101
+ return 0
102
+
@@ -0,0 +1,148 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ ASCII rendition of segmentation data files.
4
+
5
+ Eg.::
6
+
7
+ ┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┌────────────────┐┄┄┄┄┄┄┼
8
+ ┆ │r:2a93943e │ ┆
9
+ ┆ ┌────────────└──l:8┌────────────────┐┆
10
+ ┆ │r:ef1657c4 ││r:ed8f3022 │┆
11
+ ┆ │ l:1e355b0a... ││ l:8590f3bf... │┆
12
+ ┆ │ l:1e1c59e1.... ││ l:5c1a7591... │┆
13
+ ┆ │ l:aa302e79... ││ l:50a89193... │┆
14
+ ┆ │ l:0bcc2987... ││ l:ab831781... │┆
15
+ ┆ │ l:f45a1fa8... ││ l:b808a65a... │┆
16
+ ┆ │ l:1d001e4b... ││ l:640bb0ff....│┆
17
+ ┆ │ l:e30c7a81.. │┌────┐0a959ab... │┆
18
+ ┆ │ l:7bb2bb1f.... ││r:8fa3e8012b... │┆
19
+ ┆ │ l:85832788... ││ l:│7383a84... │┆
20
+ ┆ ┌──────┐d5625... │└────┘bd94110.. │┆
21
+ ┆ │r:f77b369130.. ││ l:2b0c22bb... │┆
22
+ ┆ │ l:a6│ecf48... ││ l:1dac6035... │┆
23
+ ┆ │ l:6f│ff3a2... ││ l:acb1c73e... │┆
24
+ ┆ └──────┘60b9c... ││ l:5393c17b... │┆
25
+ ┆ │ l:299e800c... ││ l:4e171c9c... │┆
26
+ ┆ │ l:508f7f75... ││ l:d1ffc59c... │┆
27
+ ┆ │ l:496470a0... ││ l:5c56470d... │┆
28
+ ┆ │ l:0ba0e73a... ││ l:8a93cee6 │┆
29
+ ┆ │ l:30b7e6ef... ││ l:6d2db756 │┆
30
+ ┆ │ l:e66fbbbb... ││ l:b48006e0... │┆
31
+ ┆ │ ... ││ ... │┆
32
+ ┆ └─────────────────┘└────────────────┘┆
33
+ ┆ ┆
34
+ ┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┼
35
+
36
+
37
+ """
38
+ import os
39
+ import sys
40
+ import tty
41
+ import termios
42
+ from pathlib import Path
43
+
44
+
45
+ import fargv
46
+ fargv.util.verbosity=1 # for --help to display option descriptions
47
+ from fargv import FargvChoice, FargvVariadic
48
+
49
+ from . import segtformats as sgf
50
+
51
+ if __name__ == '__main__':
52
+ main()
53
+
54
+ def main():
55
+
56
+ p = {
57
+ "file_paths": FargvVariadic([], description="Input file (JSON, Page, Alto)."),
58
+ "lines": FargvChoice(['1','2','0'], description="0=lines omitted, 1=lines within region limits, 2=lines within canvas limits."),
59
+ "scale": (1.0, "Factor to be applied to the default scale."),
60
+ "repair": (False, "Try repairing a faulty segmentation before rendition (file is not modified)."),
61
+ "diagnose": (False, "Run a diagnosis, scanning for potential issues."),
62
+ "text": (False, "Display text, if present."),
63
+ }
64
+
65
+ bold_start, bold_end = '\033[1m', '\033[0m'
66
+ help_msg = f"""
67
+ Key bindings:
68
+
69
+ {bold_start}n{bold_end}: {bold_start}n{bold_end}ext file.
70
+ {bold_start}p{bold_end}: {bold_start}p{bold_end}revious file.
71
+ {bold_start}l{bold_end}: cycle through {bold_start}l{bold_end}ine display modes (1=region, 2=canvas, 0=none).
72
+ {bold_start}d{bold_end}: {bold_start}d{bold_end}iagnose segmentation.
73
+ {bold_start}r{bold_end}: {bold_start}r{bold_end}epair segmentation before rendering.
74
+ {bold_start}q{bold_end}: {bold_start}q{bold_end}uit application (or exit this help screen).
75
+ {bold_start}t{bold_end}: display {bold_start}t{bold_end}ext.
76
+ {bold_start}z{bold_end}: cycle through {bold_start}z{bold_end}oom levels.
77
+ {bold_start}h{bold_end} or {bold_start}?{bold_end}: this {bold_start}h{bold_end}elp.
78
+ """
79
+
80
+
81
+ args, _ = fargv.parse( p )
82
+ if not args.file_paths:
83
+ print("Input file name expected! Abort.")
84
+ return 1
85
+
86
+ file_count=0
87
+ lines = int(args.lines)
88
+ zoom = 1
89
+ zoom_to_scale = [ args.scale * factor for factor in (.5, .75, 1.0, 1.25, 1.5) ]
90
+ repair = args.repair
91
+ text = args.text
92
+ help_screen, diagnosis_screen = False, False
93
+
94
+ try:
95
+ # from line-oriented term (default) → char-oriented input
96
+ setting = termios.tcgetattr(sys.stdin.fileno())
97
+ tty.setcbreak(sys.stdin)
98
+
99
+ while True:
100
+ print('\x1b[2J')
101
+ if help_screen or diagnosis_screen:
102
+ if diagnosis_screen:
103
+ sgf.json_doctor( sgf.anyseg_to_dict( args.file_paths[file_count] ), dry_run=True )
104
+ else:
105
+ print(help_msg)
106
+ _=sys.stdin.read(1)
107
+ help_screen, diagnosis_screen = False, False
108
+ continue
109
+
110
+ seg_rendition = sgf.anyseg_to_ascii( args.file_paths[file_count], lines=lines, scale_hw=zoom_to_scale[zoom], repair=repair, text=text )
111
+ seg_rendition_width = len(seg_rendition.split('\n')[-1])
112
+ pagination=f"{file_count+1}/{len(args.file_paths)}" + (' [repaired]' if repair else '')
113
+ footer_content=f"File {pagination}: {Path( args.file_paths[file_count] ).name}"
114
+ footer = [' '] * seg_rendition_width
115
+ footer[seg_rendition_width-( len(footer_content) + 4 ):seg_rendition_width-4]=footer_content
116
+ print( seg_rendition )
117
+ print(''.join(footer) )
118
+
119
+ # Key bindings
120
+ key=sys.stdin.read(1)
121
+ if key == 'q':
122
+ break
123
+ elif key == 'n':
124
+ file_count= (file_count+ 1) % len(args.file_paths)
125
+ lines = int(args.lines)
126
+ repair = args.repair
127
+ elif key == 'p':
128
+ file_count= (file_count- 1) % len(args.file_paths)
129
+ lines = int(args.lines)
130
+ repair = args.repair
131
+ elif key == 'l':
132
+ lines = (lines + 1) % 3
133
+ elif key == 'z':
134
+ zoom = (zoom + 1) % 5
135
+ elif key == 'r':
136
+ repair = not repair
137
+ elif key == 'd':
138
+ diagnosis_screen = True
139
+ elif key == 't':
140
+ text = not text
141
+ elif key in ['h', '?']:
142
+ help_screen = True
143
+ finally:
144
+ # no matter what, restore default terminal settings
145
+ termios.tcsetattr( sys.stdin, termios.TCSADRAIN, setting )
146
+
147
+
148
+ return 0
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env python3
2
+ #
3
+ # Convert segmentation meta-data:
4
+ # [ Alto, Page ] → JSON
5
+ #
6
+ #
7
+
8
+ import sys
9
+ from pathlib import Path
10
+ import json
11
+ import re
12
+
13
+ import fargv
14
+ fargv.util.verbosity=1 # for --help to display option descriptions
15
+ from fargv import FargvChoice, FargvPositional
16
+
17
+ from . import segtformats as sgf
18
+ from . import set_logging_level, logger
19
+
20
+
21
+ if __name__ == '__main__':
22
+ main()
23
+
24
+ def main():
25
+
26
+ p = {
27
+ "file_paths": FargvPositional(default=[], description="A JSON line segmentation file (e.g <prefix>.lines.pred.json)."),
28
+ 'out': FargvChoice(['','auto'], description="Set to 'auto' for output to filename <input stem>.<output_suffix>; leave empty for standard output."),
29
+ 'output_suffix': '.json',
30
+ "input_suffix": ('',"If empty, input file's suffix is determined by detected input format (.json or .xml)"),
31
+ "overwrite_existing": (False, "Do not overwrite existing output file"),
32
+ "repair": (False, "If True, fix semantic errors in the segmentation (line-to-region assignment, region boundaries)."),
33
+ "validate": False,
34
+ "output_format": FargvChoice(['json', 'stdout'], description="Output format"),
35
+ "verbosity": FargvChoice(['2','0','1','3'], description="Verbosity levels: 0 (quiet), 1 (WARNING), 2 (INFO-default), 3 (DEBUG)"),
36
+ }
37
+
38
+ args, _ = fargv.parse( p )
39
+
40
+ set_logging_level( int(args.verbosity) )
41
+
42
+ format_to_suffix = { sgf.SegFormat.PAGE: '.xml', sgf.SegFormat.ALTO: '.xml', sgf.SegFormat.JSON: '.json' }
43
+ for file_path in args.file_paths:
44
+
45
+ status_string=f"{file_path}: "
46
+
47
+ segdict, _ = sgf.anyseg_to_dict( file_path )
48
+ if not segdict:
49
+ logger.info( status_string + '____' )
50
+ continue
51
+ status_string += 'C'
52
+
53
+ if args.repair:
54
+ segdict = sgf.json_doctor( segdict )
55
+ status_string += 'R'
56
+
57
+ if args.validate:
58
+ if not sgf.json_validate( segdict ):
59
+ logger.info( status_string + '__' )
60
+ continue
61
+ status_string += 'V'
62
+
63
+ segdict_str = json.dumps( segdict, indent=2 )
64
+
65
+ if not args.out:
66
+ print( segdict_str )
67
+ continue
68
+
69
+ out_path = ''
70
+ if args.out == 'auto':
71
+ if not re.search( r'{}$'.format(args.input_suffix), Path(file_path).name):
72
+ logger.warning(f"Input file path '{Path(file_path).name}' does not match input suffix '{args.input_suffix}': output aborted.")
73
+ continue
74
+ out_path = Path(re.sub(r'{}$'.format( args.input_suffix ), args.output_suffix, file_path ))
75
+ logger.debug(f"Output file = {out_path}")
76
+
77
+
78
+ if not args.overwrite_existing and Path(out_path).exists():
79
+ logger.info( status_string + "_ (file exists)".format( out_path ))
80
+ continue
81
+ with open(out_path, 'w') as json_outf:
82
+ json_outf.write( segdict_str )
83
+ logger.info( status_string + f"W → {out_path}")
84
+
85
+ return 0
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ JSON -> JSON conversion
4
+
5
+ Read a JSON segmentation file, with a choice of options:
6
+
7
+ + repair a faulty file
8
+ + remove transcription data
9
+ + add a comment
10
+ """
11
+
12
+ import sys
13
+ import json
14
+ import re
15
+ from datetime import datetime
16
+ from pathlib import Path
17
+
18
+ import fargv
19
+ fargv.util.verbosity=1 # for --help to display option descriptions
20
+ from fargv import FargvPositional, FargvChoice
21
+
22
+ from . import segtformats as sgf
23
+ from . import set_logging_level, logger
24
+
25
+
26
+ if __name__ == '__main__':
27
+ main()
28
+
29
+ def main():
30
+
31
+ p = {
32
+ 'file_paths': FargvPositional(default=[], description="Input file (JSON)."),
33
+ 'out': FargvChoice(['','auto'], description="Set to 'auto' for output to filename <input stem>.<output_suffix>; leave empty for standard output."),
34
+ 'input_suffix': '.lines.pred.json',
35
+ 'output_suffix': '.json',
36
+ 'overwrite_existing': (False, "Overwrite an existing output file."),
37
+ 'drop_transcription': (False, "Remove line transcription, if it exists."),
38
+ 'repair': (True, "Repair a faulty dictionary: re-assign lines to their proper regions; expand regions to include every pixel of the line polygon."),
39
+ 'diagnose': (False, "Run a diagnosis, scanning for potential issues."),
40
+ #'delete_line_features': FargvPositional(default=[], description="Line items to be removed (use with caution!)"),
41
+ 'verbosity': FargvChoice(['2','0','1','3'], description="Verbosity levels: 0 (quiet), 1 (WARNING), 2 (INFO-default), 3 (DEBUG)"),
42
+ "comment": ('',"A text string to be added to the <Comments> elt."),
43
+ }
44
+
45
+ args, _ = fargv.parse( p )
46
+
47
+ set_logging_level( int(args.verbosity) )
48
+
49
+ for file_path in args.file_paths:
50
+ json_path = Path( file_path )
51
+
52
+ if sgf.get_format( json_path ) != sgf.SegFormat.JSON:
53
+ logger.warning(f"{file_path} is not a valid JSON file: skipping.")
54
+ continue
55
+
56
+ segdict = None
57
+ with open( json_path, 'r') as json_if:
58
+ segdict = json.load( json_if )
59
+
60
+ if args.diagnose:
61
+ logger.info("File diagnosis")
62
+ sgf.json_doctor( segdict, dry_run=True )
63
+ continue
64
+
65
+ if args.repair:
66
+ logger.info("Repairing file")
67
+ segdict = sgf.json_doctor( segdict )
68
+
69
+ line_dicts = sgf.line_dicts_from_segmentation_dict( segdict )
70
+
71
+ # delete unwanted features
72
+ # if args.delete_line_features:
73
+ # for line in line_dicts:
74
+ # for key in args.delete_line_features:
75
+ # if key not in line:
76
+ # continue
77
+ # del line[key]
78
+
79
+ # remove transcriptions
80
+ if args.drop_transcription:
81
+ for line in line_dicts:
82
+ if 'text' in line:
83
+ del line['text']
84
+
85
+ # insert metadata at the top
86
+ regions = segdict['regions']
87
+ del segdict['regions']
88
+ segdict['metadata'].update( {'created': str(datetime.now().isoformat('T','seconds')), 'creator': __file__ })
89
+
90
+ if args.comment:
91
+ segdict['metadata']['comments']=args.comment
92
+ segdict['regions']=regions
93
+
94
+ # output
95
+
96
+ segdict_str = json.dumps( segdict, indent=2 )
97
+ if not args.out:
98
+ print( segdict_str )
99
+ continue
100
+
101
+ out_path = ''
102
+ if args.out == 'auto':
103
+ if not re.search( r'{}$'.format(args.input_suffix), Path(file_path).name):
104
+ logger.warning(f"Input file path '{Path(file_path).name}' does not match input suffix '{args.input_suffix}': output aborted.")
105
+ continue
106
+ out_path = re.sub(r'{}$'.format( args.input_suffix ), args.output_suffix, file_path )
107
+ logger.debug(f"Output file = {out_path}")
108
+
109
+ if not args.overwrite_existing and Path(out_path).exists():
110
+ logger.info("File {} exists: skipping.".format( out_path ))
111
+ continue
112
+ with open(out_path, 'w') as json_outf:
113
+ logger.info(f"W: {Path(file_path).name} → {Path(out_path).name}")
114
+ json_outf.write( segdict_str )
115
+ return 0
116
+