Passed
Pull Request — master (#576)
by Konstantin
02:07
created

ocrd_page_parse.parseString()   B

Complexity

Conditions 6

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 33
rs 8.3466
c 0
b 0
f 0
cc 6
nop 4
1
def parse(inFileName, silence=False, print_warnings=True, filename=None):
2
    global CapturedNsmap_
3
    if not filename:
4
        filename = inFileName
5
    gds_collector = GdsCollector_(filename=filename)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable GdsCollector_ does not seem to be defined.
Loading history...
6
    parser = None
7
    doc = parsexml_(inFileName, parser)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable parsexml_ does not seem to be defined.
Loading history...
8
    rootNode = doc.getroot()
9
    rootTag, rootClass = get_root_tag(rootNode)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable get_root_tag does not seem to be defined.
Loading history...
10
    if rootClass is None:
11
        rootTag = 'PcGts'
12
        rootClass = PcGts
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable PcGts does not seem to be defined.
Loading history...
13
    rootObj = rootClass.factory()
14
    rootObj.build(rootNode, gds_collector_=gds_collector)
15
    CapturedNsmap_, namespacedefs = get_required_ns_prefix_defs(rootNode)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable get_required_ns_prefix_defs does not seem to be defined.
Loading history...
16
    if not SaveElementTreeNode:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SaveElementTreeNode does not seem to be defined.
Loading history...
17
        doc = None
18
        rootNode = None
19
    if not silence:
20
        sys.stdout.write('<?xml version="1.0" ?>\n')
21
        rootObj.export(
22
            sys.stdout, 0, name_=rootTag,
23
            namespacedef_=namespacedefs,
24
            pretty_print=True)
25
    if print_warnings and len(gds_collector.get_messages()) > 0:
26
        separator = ('-' * 50) + '\n'
27
        sys.stderr.write(separator)
28
        sys.stderr.write('----- Warnings -- count: {} -----\n'.format(
29
            len(gds_collector.get_messages()), ))
30
        gds_collector.write_messages(sys.stderr)
31
        sys.stderr.write(separator)
32
    return rootObj
33
34
def parseString(inString, silence=False, print_warnings=True, filename=None):
35
    '''Parse a string, create the object tree, and export it.
36
37
    Arguments:
38
    - inString -- A string.  This XML fragment should not start
39
      with an XML declaration containing an encoding.
40
    - silence -- A boolean.  If False, export the object.
41
    Returns -- The root object in the tree.
42
    '''
43
    parser = None
44
    rootNode= parsexmlstring_(inString, parser)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable parsexmlstring_ does not seem to be defined.
Loading history...
45
    gds_collector = GdsCollector_(filename=filename)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable GdsCollector_ does not seem to be defined.
Loading history...
46
    rootTag, rootClass = get_root_tag(rootNode)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable get_root_tag does not seem to be defined.
Loading history...
47
    if rootClass is None:
48
        rootTag = 'PcGts'
49
        rootClass = PcGts
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable PcGts does not seem to be defined.
Loading history...
50
    rootObj = rootClass.factory()
51
    rootObj.build(rootNode, gds_collector_=gds_collector)
52
    if not SaveElementTreeNode:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable SaveElementTreeNode does not seem to be defined.
Loading history...
53
        rootNode = None
54
    if not silence:
55
        sys.stdout.write('<?xml version="1.0" ?>\n')
56
        rootObj.export(
57
            sys.stdout, 0, name_=rootTag,
58
            namespacedef_='xmlns:pc="http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15"')
59
    if print_warnings and len(gds_collector.get_messages()) > 0:
60
        separator = ('-' * 50) + '\n'
61
        sys.stderr.write(separator)
62
        sys.stderr.write('----- Warnings -- count: {} -----\n'.format(
63
            len(gds_collector.get_messages()), ))
64
        gds_collector.write_messages(sys.stderr)
65
        sys.stderr.write(separator)
66
    return rootObj
67
68