Page MenuHomec4science

convert.py
No OneTemporary

File Metadata

Created
Tue, May 7, 14:29

convert.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This program can be used to convert py objects to ontology and data in turtle format.
"""
# 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}}}
from colorama import Fore, Style
import getopt
import lxml.etree as ET
from os import sep, path, listdir
from os.path import isfile, isdir, dirname, basename
from progress.bar import Bar
import re
import sys
sys.path.append('svgscripts')
from datatypes.archival_manuscript import ArchivalManuscriptUnity
if dirname(__file__) not in sys.path:
sys.path.append(dirname(__file__))
from class_spec import SemanticClass
from config import check_config_files_exist, get_datatypes_dir, PROJECT_NAME, PROJECT_ONTOLOGY_FILE, PROJECT_URL
from py2ttl_data import Py2TTLDataConverter
from py2ttl_ontology import Py2TTLOntologyConverter
sys.path.append('shared_util')
from myxmlwriter import xml2dict
from main_util import get_manuscript_files_and_include_status
__author__ = "Christian Steiner"
__maintainer__ = __author__
__copyright__ = 'University of Basel'
__email__ = "christian.steiner@unibas.ch"
__status__ = "Development"
__license__ = "GPL v3"
__version__ = "0.0.1"
FILE_TYPE_XML_PROJECT = "xmlProjectFile"
def usage():
"""prints information on how to use the script
"""
print(main.__doc__)
def main(argv):
"""This program can be used to convert py objects to a owl:Ontology and rdf data in turtle format.
py2ttl/convert.py [OPTIONS] <manuscript.xml> [<manuscript2.xml> ...]
<manuscript.xml> xml file of type shared_util.myxmlwriter.FILE_TYPE_XML_MANUSCRIPT.
OPTIONS:
-h|--help: show help
-c|--create-or-update-pages create or update pages as seperate ttl files in dir 'ttl'
-i|--include-status=STATUS include pages with status = STATUS. STATUS is a ':' seperated string of status, e.g. 'OK:faksimile merged'.
-I|--Include-files-only create include files only with suffix INCLUDE_DATA.ttl.
:return: exit code (int)
"""
check_config_files_exist()
datatypes_dir = get_datatypes_dir()
include_only = False
create_or_update_pages = False
containsAttr = 'status'
source_ontology_file = PROJECT_ONTOLOGY_FILE
target_ontology_file = '.{0}{1}-ontology_autogenerated.ttl'.format(sep, PROJECT_NAME)
manuscript_file = None
page_status_list = [ 'OK', 'faksimile merged' ]
try:
opts, args = getopt.getopt(argv, "hci:I", ["help", "create-or-update-pages", "include-status=", "Include-files-only"])
except getopt.GetoptError:
usage()
return 2
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
return 0
elif opt in ('-i', '--include-status'):
page_status_list = arg.split(':')
elif opt in ('-c', '--create-or-update-pages'):
create_or_update_pages = True
elif opt in ('-I', '----Include-files-only'):
include_only = True
containsAttr = 'include'
if len(args) < 1 :
usage()
return 2
ontology_created = False
ontology_converter = Py2TTLOntologyConverter(project_ontology_file=source_ontology_file)
output = 2
status = ':'.join(page_status_list)\
if not include_only\
else 'OK'
for arg in get_manuscript_files_and_include_status(args, containsAttr, status):
if type(arg) == str:
manuscript_file, include_status = arg, None
else:
manuscript_file, include_status = arg[0], arg[1]
if not isfile(manuscript_file):
usage()
return 2
if not ontology_created:
print(Fore.CYAN + 'Create ontology from "{}" ...'.format(manuscript_file))
if ontology_converter.create_ontology(datatypes_dir, target_ontology_file) == 0:
print(Fore.GREEN + '[Ontology file {0} created]'.format(target_ontology_file))
ontology_created = True
else:
return 2
current_page_status_list = page_status_list\
if include_status is None\
else include_status.split(':')
print(Fore.CYAN + f'Create data from "{manuscript_file}" with status "{current_page_status_list}" ...')
data_converter = Py2TTLDataConverter(manuscript_file, mapping_dictionary=ontology_converter.uri_mapping4cls_and_properties)
output = data_converter.convert(page_status_list=current_page_status_list, create_or_update_changed_pages=create_or_update_pages)
return output
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))

Event Timeline