Page MenuHomec4science

lineNumber.py
No OneTemporary

File Metadata

Created
Fri, May 10, 14:12

lineNumber.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This class can be used to represent a line number.
"""
# Copyright (C) University of Basel 2019 {{{1
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/> 1}}}
__author__ = "Christian Steiner"
__maintainer__ = __author__
__copyright__ = 'University of Basel'
__email__ = "christian.steiner@unibas.ch"
__status__ = "Development"
__license__ = "GPL v3"
__version__ = "0.0.1"
import re
from lxml import etree as ET
from os.path import isfile
from .class_spec import SemanticClass
from .matrix import Matrix
class LineNumber(SemanticClass):
"""
This class represents a line number.
Args:
file_name (str): name of the xml file to be instantiated.
"""
XML_TAG = 'line-number'
def __init__(self, id=0, bottom=0.0, top=0.0, raw_text_node=None, transkription_field=None, xml_text_node=None):
self.id = id
self.bottom = bottom
self.top = top
if xml_text_node is not None:
self.id = int(xml_text_node.get('id'))
self.bottom = float(xml_text_node.get('bottom'))
self.top = float(xml_text_node.get('top'))
if raw_text_node is not None and transkription_field is not None:
matrix = Matrix(raw_text_node.get('transform'), transkription_field=transkription_field)
self.bottom = matrix.getY()
self.id = int(raw_text_node.text) if raw_text_node.text is not None\
else int(''.join([x.text for x in raw_text_node.findall('.//tspan', raw_text_node.nsmap)]))
@classmethod
def get_semantic_dictionary(cls):
""" Creates and returns a semantic dictionary as specified by SemanticClass.
"""
dictionary = {}
class_dict = cls.get_class_dictionary()
properties = {\
'id': (int, 1, '{}/@id'.format(LineNumber.XML_TAG)),\
'bottom': (float, 1, '{}/@bottom'.format(LineNumber.XML_TAG)),\
'top': (float, 1, '{}/@top'.format(LineNumber.XML_TAG))\
}
dictionary.update({'class': class_dict})
dictionary.update({'properties': properties})
return dictionary
@staticmethod
def IS_A_LINE_NUMBER(raw_text_node):
"""Returns whether svg node contains a line number.
"""
if raw_text_node.text is not None:
return bool(re.search(r'^[0-9]+$', raw_text_node.text))
elif len(raw_text_node.findall('.//tspan', raw_text_node.nsmap)) > 0:
text = ''.join([x.text for x in raw_text_node.findall('.//tspan', raw_text_node.nsmap)])
return bool(re.search(r'^[0-9]+$', text))
return False
def setTop(self, top):
"""Sets top position of line number.
"""
self.top = top
def attach_object_to_tree(self, target_tree):
"""Attach object to tree.
"""
obj_node = target_tree.getroot().xpath('//' + LineNumber.XML_TAG + '[@id="%s"]' % self.id)[0] \
if(len(target_tree.getroot().xpath('//' + LineNumber.XML_TAG + '[@id="%s"]' % self.id)) > 0) \
else ET.SubElement(target_tree.getroot(), LineNumber.XML_TAG)
for key in self.__dict__.keys():
obj_node.set(key.replace('_','-'), str(round(self.__dict__[key], 3)))

Event Timeline