Page MenuHomec4science

main_util.py
No OneTemporary

File Metadata

Created
Thu, May 16, 20:09

main_util.py

import lxml.etree as ET
from os.path import isfile, isdir, dirname, basename
from svgpathtools import svg2paths2, svg_to_paths
import sys
sys.path.append('svgscripts')
from datatypes.path import Path
from datatypes.transkriptionField import TranskriptionField
from datatypes.transkription_position import TranskriptionPosition
FILE_TYPE_XML_PROJECT = "xmlProjectFile"
def create_function_dictionary(list_of_keys, target_function, function_dictionary=None) -> dict:
"""Create a function_dictionary
"""
if function_dictionary is None:
function_dictionary = {}
for key in list_of_keys:
function_dictionary.update({key: target_function})
return function_dictionary
def get_manuscript_files(args: list) ->list:
"""Return a list of manuscript files. If first element is of type FILE_TYPE_XML_PROJECT read from
xml file and return as list of filenames.
"""
if len(args) == 1\
and args[0].endswith('.xml')\
and ET.parse(args[0]).getroot().find('metadata/type').text == FILE_TYPE_XML_PROJECT:
return ET.parse(args[0]).xpath('//manuscript[contains(@status, "OK")]/@file')
return args
def get_manuscript_files_and_include_status(args: list) ->list:
"""Return a list tuples of manuscript files and optional include status. If first element is of type FILE_TYPE_XML_PROJECT read from
xml file and return as list of tuples of filename (@files) and include status for manuscript pages (@include).
"""
if len(args) == 1\
and args[0].endswith('.xml')\
and ET.parse(args[0]).getroot().find('metadata/type').text == FILE_TYPE_XML_PROJECT:
return [ (node.get('file'),node.get('include')) for node in ET.parse(args[0]).xpath('//manuscript[contains(@status, "OK")]')]
return args
def extract_paths_on_tf(page, transkription_field=None, new_style_prefix='tln', outsiders=None, outsider_attributes=None) ->list:
"""Extract all paths on transkription_field.
:return: a list of datatypes.path.Path
"""
if page.source is not None and isfile(page.source):
if transkription_field is None:
transkription_field = TranskriptionField(page.source, multipage_index=page.multipage_index)
paths, attributes = svg_to_paths.svg2paths(page.source)
allpaths_on_tf = []
for index, path in enumerate(paths):
attribute = attributes[index]
if len(path) > 0\
and path != transkription_field.path\
and path.bbox()[0] >= transkription_field.xmin\
and path.bbox()[1] <= transkription_field.xmax\
and path.bbox()[2] >= transkription_field.ymin\
and path.bbox()[3] <= transkription_field.ymax:
style_class = attribute.get('class')
if style_class is None and attribute.get('style') is not None:
style_class = create_new_style(page, attribute.get('style'), new_style_prefix=new_style_prefix)
allpaths_on_tf.append(Path.create_cls(id=index, path=path, style_class=style_class, page=page))
elif outsiders is not None\
and len(path) > 0\
and path != transkription_field.path:
style_class = attribute.get('class')
if style_class is None and attribute.get('style') is not None:
style_class = create_new_style(page, attribute.get('style'), new_style_prefix=new_style_prefix)
outsiders.append(Path.create_cls(id=index, path=path, style_class=style_class, page=page))
outsider_attributes.append(attribute)
return allpaths_on_tf
else:
return []
def create_new_style(page, style_attribute_string, new_style_prefix='tln') ->str:
"""Create new style, update page and return new style_class.
"""
style_dict = {}
style_class = None
for key_content in style_attribute_string.split(';'):
if ':' in key_content:
key, content = tuple(key_content.split(':'))
style_dict.update({ key: content})
if style_dict in page.style_dict.values():
style_class = list(page.style_dict.keys())[list(page.style_dict.values()).index(style_dict)]
else:
new_style_index = len([ k for k in page.style_dict.keys() if k.startswith(new_style_prefix) ])
style_class = f'{new_style_prefix}{new_style_index}'
page.style_dict.update({style_class: style_dict })
page.add_style(sonderzeichen_list=page.sonderzeichen_list, letterspacing_list=page.letterspacing_list,\
style_dict=page.style_dict)
return style_class
def get_paths_near_position(tp: TranskriptionPosition, paths: list, xmin=0, ymin=0, do_not_include_d_attributes=None) ->list:
"""Given a transkription position and a list of svgscripts.datatypes.path.Path,
return a list of paths near this position.
"""
tp_x = tp.left + (tp.width/2) + xmin
tp_y = tp.top + (tp.height/2) + ymin
do_not_include_d_attributes = do_not_include_d_attributes if do_not_include_d_attributes is not None else []
return [ path.d_attribute for path in Path.get_nearest_paths(paths, tp_x, tp_y) if path.d_attribute not in do_not_include_d_attributes ]

Event Timeline