Skip to content

Commit ba88aee

Browse files
author
Robert Sachunsky
committed
patch ocrd_page_generateds for automatic inheritance
1 parent 881020d commit ba88aee

File tree

3 files changed

+262
-22
lines changed

3 files changed

+262
-22
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ generate-page: repo/assets
100100
sed -i 's/_nsprefix_ = None/_nsprefix_ = "pc"/' $(GDS_PAGE)
101101
# hack to ensure child nodes also have pc: prefix...
102102
sed -i 's/.*_nsprefix_ = child_.prefix$$//' $(GDS_PAGE)
103+
# hack to get #698: auto-inheritance of attributes and TextStyle
104+
patch -p1 < ocrd_models/ocrd_page_generateds.build.inherited.patch
103105

104106
#
105107
# Repos

ocrd_models/ocrd_models/ocrd_page_generateds.py

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,40 @@ def find_attr_value_(attr_name, node):
773773
value = attrs.get('{%s}%s' % (namespace, name, ))
774774
return value
775775

776+
def find_attr_value_ancestors_(attr_name, node):
777+
for parent in node.iterancestors():
778+
value = find_attr_value_(attr_name, parent)
779+
if value is not None:
780+
return value
781+
return None
782+
783+
def find_elem_(elem_name, node):
784+
value = node.find(elem_name)
785+
if value is not None:
786+
return value
787+
elem_parts = elem_name.split(':')
788+
if len(elem_parts) == 1:
789+
prefix = node.prefix
790+
name = elem_name
791+
elif len(elem_parts) == 2:
792+
prefix, name = elem_parts
793+
namespace = node.nsmap.get(prefix)
794+
if namespace is not None:
795+
value = node.find('{%s}%s' % (namespace, name))
796+
return value
797+
798+
def find_elem_ancestors_(elem_name, node):
799+
for parent in node.iterancestors():
800+
value = find_elem_(elem_name, parent)
801+
if value is not None:
802+
return value
803+
return None
804+
805+
def prepend_(iterable, elem):
806+
if elem is not None:
807+
yield elem
808+
for elem in iterable:
809+
yield elem
776810

777811
def encode_str_2_3(instr):
778812
return instr
@@ -3889,7 +3923,7 @@ def build(self, node, gds_collector_=None):
38893923
already_processed = set()
38903924
self.ns_prefix_ = node.prefix
38913925
self.buildAttributes(node, node.attrib, already_processed)
3892-
for child in node:
3926+
for child in prepend_(node, find_elem_ancestors_('TextStyle', node)):
38933927
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
38943928
self.buildChildren(child, node, nodeName_, gds_collector_=gds_collector_)
38953929
return self
@@ -3898,27 +3932,27 @@ def buildAttributes(self, node, attrs, already_processed):
38983932
if value is not None and 'id' not in already_processed:
38993933
already_processed.add('id')
39003934
self.id = value
3901-
value = find_attr_value_('primaryLanguage', node)
3935+
value = find_attr_value_('primaryLanguage', node) or find_attr_value_ancestors_('primaryLanguage', node)
39023936
if value is not None and 'primaryLanguage' not in already_processed:
39033937
already_processed.add('primaryLanguage')
39043938
self.primaryLanguage = value
39053939
self.validate_LanguageSimpleType(self.primaryLanguage) # validate type LanguageSimpleType
3906-
value = find_attr_value_('primaryScript', node)
3940+
value = find_attr_value_('primaryScript', node) or find_attr_value_ancestors_('primaryScript', node)
39073941
if value is not None and 'primaryScript' not in already_processed:
39083942
already_processed.add('primaryScript')
39093943
self.primaryScript = value
39103944
self.validate_ScriptSimpleType(self.primaryScript) # validate type ScriptSimpleType
3911-
value = find_attr_value_('secondaryScript', node)
3945+
value = find_attr_value_('secondaryScript', node) or find_attr_value_ancestors_('secondaryScript', node)
39123946
if value is not None and 'secondaryScript' not in already_processed:
39133947
already_processed.add('secondaryScript')
39143948
self.secondaryScript = value
39153949
self.validate_ScriptSimpleType(self.secondaryScript) # validate type ScriptSimpleType
3916-
value = find_attr_value_('readingDirection', node)
3950+
value = find_attr_value_('readingDirection', node) or find_attr_value_ancestors_('readingDirection', node)
39173951
if value is not None and 'readingDirection' not in already_processed:
39183952
already_processed.add('readingDirection')
39193953
self.readingDirection = value
39203954
self.validate_ReadingDirectionSimpleType(self.readingDirection) # validate type ReadingDirectionSimpleType
3921-
value = find_attr_value_('production', node)
3955+
value = find_attr_value_('production', node) or find_attr_value_ancestors_('production', node)
39223956
if value is not None and 'production' not in already_processed:
39233957
already_processed.add('production')
39243958
self.production = value
@@ -4390,7 +4424,7 @@ def build(self, node, gds_collector_=None):
43904424
already_processed = set()
43914425
self.ns_prefix_ = node.prefix
43924426
self.buildAttributes(node, node.attrib, already_processed)
4393-
for child in node:
4427+
for child in prepend_(node, find_elem_ancestors_('TextStyle', node)):
43944428
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
43954429
self.buildChildren(child, node, nodeName_, gds_collector_=gds_collector_)
43964430
return self
@@ -4399,27 +4433,27 @@ def buildAttributes(self, node, attrs, already_processed):
43994433
if value is not None and 'id' not in already_processed:
44004434
already_processed.add('id')
44014435
self.id = value
4402-
value = find_attr_value_('language', node)
4436+
value = find_attr_value_('language', node) or find_attr_value_ancestors_('primaryLanguage', node)
44034437
if value is not None and 'language' not in already_processed:
44044438
already_processed.add('language')
44054439
self.language = value
44064440
self.validate_LanguageSimpleType(self.language) # validate type LanguageSimpleType
4407-
value = find_attr_value_('primaryScript', node)
4441+
value = find_attr_value_('primaryScript', node) or find_attr_value_ancestors_('primaryScript', node)
44084442
if value is not None and 'primaryScript' not in already_processed:
44094443
already_processed.add('primaryScript')
44104444
self.primaryScript = value
44114445
self.validate_ScriptSimpleType(self.primaryScript) # validate type ScriptSimpleType
4412-
value = find_attr_value_('secondaryScript', node)
4446+
value = find_attr_value_('secondaryScript', node) or find_attr_value_ancestors_('secondaryScript', node)
44134447
if value is not None and 'secondaryScript' not in already_processed:
44144448
already_processed.add('secondaryScript')
44154449
self.secondaryScript = value
44164450
self.validate_ScriptSimpleType(self.secondaryScript) # validate type ScriptSimpleType
4417-
value = find_attr_value_('readingDirection', node)
4451+
value = find_attr_value_('readingDirection', node) or find_attr_value_ancestors_('readingDirection', node)
44184452
if value is not None and 'readingDirection' not in already_processed:
44194453
already_processed.add('readingDirection')
44204454
self.readingDirection = value
44214455
self.validate_ReadingDirectionSimpleType(self.readingDirection) # validate type ReadingDirectionSimpleType
4422-
value = find_attr_value_('production', node)
4456+
value = find_attr_value_('production', node) or find_attr_value_ancestors_('production', node)
44234457
if value is not None and 'production' not in already_processed:
44244458
already_processed.add('production')
44254459
self.production = value
@@ -4831,7 +4865,7 @@ def build(self, node, gds_collector_=None):
48314865
already_processed = set()
48324866
self.ns_prefix_ = node.prefix
48334867
self.buildAttributes(node, node.attrib, already_processed)
4834-
for child in node:
4868+
for child in prepend_(node, find_elem_ancestors_('TextStyle', node)):
48354869
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
48364870
self.buildChildren(child, node, nodeName_, gds_collector_=gds_collector_)
48374871
return self
@@ -4858,12 +4892,12 @@ def buildAttributes(self, node, attrs, already_processed):
48584892
self.symbol = False
48594893
else:
48604894
raise_parse_error(node, 'Bad boolean attribute')
4861-
value = find_attr_value_('script', node)
4895+
value = find_attr_value_('script', node) or find_attr_value_ancestors_('primaryScript', node)
48624896
if value is not None and 'script' not in already_processed:
48634897
already_processed.add('script')
48644898
self.script = value
48654899
self.validate_ScriptSimpleType(self.script) # validate type ScriptSimpleType
4866-
value = find_attr_value_('production', node)
4900+
value = find_attr_value_('production', node) or find_attr_value_ancestors_('production', node)
48674901
if value is not None and 'production' not in already_processed:
48684902
already_processed.add('production')
48694903
self.production = value
@@ -13915,7 +13949,7 @@ def build(self, node, gds_collector_=None):
1391513949
already_processed = set()
1391613950
self.ns_prefix_ = node.prefix
1391713951
self.buildAttributes(node, node.attrib, already_processed)
13918-
for child in node:
13952+
for child in prepend_(node, find_elem_ancestors_('TextStyle', node)):
1391913953
nodeName_ = Tag_pattern_.match(child.tag).groups()[-1]
1392013954
self.buildChildren(child, node, nodeName_, gds_collector_=gds_collector_)
1392113955
return self
@@ -13934,12 +13968,12 @@ def buildAttributes(self, node, attrs, already_processed):
1393413968
if value is not None and 'leading' not in already_processed:
1393513969
already_processed.add('leading')
1393613970
self.leading = self.gds_parse_integer(value, node, 'leading')
13937-
value = find_attr_value_('readingDirection', node)
13971+
value = find_attr_value_('readingDirection', node) or find_attr_value_ancestors_('readingDirection', node)
1393813972
if value is not None and 'readingDirection' not in already_processed:
1393913973
already_processed.add('readingDirection')
1394013974
self.readingDirection = value
1394113975
self.validate_ReadingDirectionSimpleType(self.readingDirection) # validate type ReadingDirectionSimpleType
13942-
value = find_attr_value_('textLineOrder', node)
13976+
value = find_attr_value_('textLineOrder', node) or find_attr_value_ancestors_('textLineOrder', node)
1394313977
if value is not None and 'textLineOrder' not in already_processed:
1394413978
already_processed.add('textLineOrder')
1394513979
self.textLineOrder = value
@@ -13963,22 +13997,22 @@ def buildAttributes(self, node, attrs, already_processed):
1396313997
already_processed.add('align')
1396413998
self.align = value
1396513999
self.validate_AlignSimpleType(self.align) # validate type AlignSimpleType
13966-
value = find_attr_value_('primaryLanguage', node)
14000+
value = find_attr_value_('primaryLanguage', node) or find_attr_value_ancestors_('primaryLanguage', node)
1396714001
if value is not None and 'primaryLanguage' not in already_processed:
1396814002
already_processed.add('primaryLanguage')
1396914003
self.primaryLanguage = value
1397014004
self.validate_LanguageSimpleType(self.primaryLanguage) # validate type LanguageSimpleType
13971-
value = find_attr_value_('secondaryLanguage', node)
14005+
value = find_attr_value_('secondaryLanguage', node) or find_attr_value_ancestors_('secondaryLanguage', node)
1397214006
if value is not None and 'secondaryLanguage' not in already_processed:
1397314007
already_processed.add('secondaryLanguage')
1397414008
self.secondaryLanguage = value
1397514009
self.validate_LanguageSimpleType(self.secondaryLanguage) # validate type LanguageSimpleType
13976-
value = find_attr_value_('primaryScript', node)
14010+
value = find_attr_value_('primaryScript', node) or find_attr_value_ancestors_('primaryScript', node)
1397714011
if value is not None and 'primaryScript' not in already_processed:
1397814012
already_processed.add('primaryScript')
1397914013
self.primaryScript = value
1398014014
self.validate_ScriptSimpleType(self.primaryScript) # validate type ScriptSimpleType
13981-
value = find_attr_value_('secondaryScript', node)
14015+
value = find_attr_value_('secondaryScript', node) or find_attr_value_ancestors_('secondaryScript', node)
1398214016
if value is not None and 'secondaryScript' not in already_processed:
1398314017
already_processed.add('secondaryScript')
1398414018
self.secondaryScript = value

0 commit comments

Comments
 (0)