Page MenuHomec4science

reconstructed_konvolut.py
No OneTemporary

File Metadata

Created
Tue, May 7, 11:45

reconstructed_konvolut.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This class can be used to represent a reconstruction of an original manuscript (e.g. a workbook or notebook).
"""
# 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 abc
from lxml import etree as ET
from os.path import isfile
import requests
import sys
from .description import Description
from .faksimile_image import FaksimileImage
from .manuscript import ManuscriptUnity
from .page import Page, FILE_TYPE_XML_MANUSCRIPT, FILE_TYPE_SVG_WORD_POSITION
sys.path.append('shared_util')
from myxmlwriter import parse_xml_of_type, write_pretty, xml_has_type
class NonExistentPage(Page):
"""This class represents a page that does not exist as part of the KGW edition.
@label non existent page
"""
NIETZSCHE_SOURCES_URL = 'http://www.nietzschesource.org/DFGAapi/api/fe/facsimile/'
def __init__(self, number=None, faksimile_image=None, status=None):
self.number = number
self.status = status
self.faksimile_image = faksimile_image
@classmethod
def create_cls(cls, page_node, faksimile_image=None):
"""
Create an instance of NonExistentPage from a page_node
:return: NonExistentPage
"""
number = page_node.get('title') + '_' + page_node.get('number')\
if bool(page_node.get('title'))\
else page_node.get('number')
return cls(number=number, status=page_node.get('status'), faksimile_image=faksimile_image)
def get_name_and_id(self):
"""Return an identification for object as 2-tuple.
"""
return type(self).__name__, self.number.replace(' ', '_')
@classmethod
def get_semantic_dictionary(cls):
""" Creates and returns a semantic dictionary as specified by SemanticClass.
"""
dictionary = super(NonExistentPage,cls).get_semantic_dictionary()
dictionary[cls.PROPERTIES_KEY].update(cls.create_semantic_property_dictionary('status', str))
return cls.return_dictionary_after_updating_super_classes(dictionary)
class ReconstructedKonvolut(ManuscriptUnity):
"""
This class represents a reconstruction of an original manuscript (e.g. a workbook or notebook).
@label reconstruction of an origianl manuscript
Args:
title title for identification of the reconstruction
manuscript_type type of manuscript: 'Arbeitsheft' or 'Notizheft'
manuscript_tree lxml.ElementTree
"""
XML_TAG = 'reconstructed-konvolut'
TYPE_DICTIONARY = { 'R_n': 'Notizheft', 'R_w': 'Arbeitsheft' }
UNITTESTING = False
def __init__(self, title='', manuscript_type='', manuscript_tree=None):
super(ReconstructedKonvolut,self).__init__(title=title, manuscript_type=manuscript_type,manuscript_tree=manuscript_tree)
@classmethod
def create_cls(cls, xml_manuscript_file, page_status_list=None, page_xpath=''):
"""Create an instance of ReconstructedKonvolut from a xml file of type FILE_TYPE_XML_MANUSCRIPT.
:return: ReconstructedKonvolut
"""
manuscript = super(ReconstructedKonvolut,cls).create_cls(xml_manuscript_file)
manuscript_tree = manuscript.manuscript_tree
if page_xpath == '':
page_status = ''
if page_status_list is not None\
and type(page_status_list) is list\
and len(page_status_list) > 0:
page_status = '[' + ' and '.join([ f'contains(@status, "{status}")' for status in page_status_list ]) + ']'
page_xpath = f'//pages/page{page_status}/@output'
included_page_list = [ page_source\
for page_source in manuscript_tree.xpath(page_xpath)\
if isfile(page_source) and xml_has_type(FILE_TYPE_SVG_WORD_POSITION, xml_source_file=page_source) ]
for page_node in manuscript_tree.xpath('//pages/page'):
if bool(page_node.get('output'))\
and isfile(page_node.get('output'))\
and xml_has_type(FILE_TYPE_SVG_WORD_POSITION, xml_source_file=page_node.get('output')):
manuscript.pages.append(Page.create_cls(\
page_node.get('output'), create_dummy_page=(page_node.get('output') not in included_page_list)))
else:
faksimile_image = get_or_update_faksimile(xml_manuscript_file, page_node)
manuscript.pages.append(NonExistentPage.create_cls(page_node, faksimile_image))
manuscript.description = Description.create_cls_from_node(manuscript_tree.xpath(Description.XML_TAG)[0])\
if len(manuscript_tree.xpath(Description.XML_TAG)) > 0\
else None
return manuscript
def get_or_update_faksimile(xml_source_file, page_node) ->FaksimileImage:
"""Return the faksimile image of the non existent page.
"""
faksimile_image = None
if len(page_node.xpath(f'./{FaksimileImage.XML_TAG}')) > 0:
faksimile_image = FaksimileImage(node=page_node.xpath(f'./{FaksimileImage.XML_TAG}')[0])
elif bool(page_node.get('alias')):
url = NonExistentPage.NIETZSCHE_SOURCES_URL + page_node.get('alias')
faksimile_dict = None
try:
r = requests.get(url)
faksimile_dict = r.json()
except Exception:
print(f'URL does not work: {url}')
if faksimile_dict is not None and len(faksimile_dict) > 0:
width = faksimile_dict['imageWidth']
height = faksimile_dict['imageHeight']
file_name = page_node.get('alias') + '.jpg'
URL = FaksimileImage.NIETZSCHE_SOURCES_URL + page_node.get('alias')
faksimile_image = FaksimileImage(file_name=file_name, URL=URL, height=height, width=width)
faksimile_image.attach_object_to_tree(page_node)
write_pretty(xml_element_tree=page_node.getroottree(), file_name=xml_source_file, script_name=__file__,\
file_type=FILE_TYPE_XML_MANUSCRIPT, backup=True)
return faksimile_image

Event Timeline