Data Types

class odml.dtypes.DType

An enumeration.

boolean = 'boolean'
date = 'date'
datetime = 'datetime'
float = 'float'
int = 'int'
person = 'person'
string = 'string'
text = 'text'
time = 'time'
url = 'url'
odml.dtypes.bool_get(string)
odml.dtypes.bool_set(string)
odml.dtypes.boolean_get(string)
odml.dtypes.boolean_set(string)
odml.dtypes.date_get(string)
odml.dtypes.date_set(string)
odml.dtypes.datetime_get(string)
odml.dtypes.datetime_set(string)
odml.dtypes.default_values(dtype)
odml.dtypes.float_get(string)
odml.dtypes.get(string, dtype=None)

Convert string to the corresponding dtype

odml.dtypes.infer_dtype(value)
odml.dtypes.int_get(string)
odml.dtypes.self

Provides functionality for validation of the data-types specified for odML

odml.dtypes.set(value, dtype=None)

Serialize a value of type dtype to a unicode string

odml.dtypes.str_get(string)
odml.dtypes.str_set(string)
odml.dtypes.string_get(string)
odml.dtypes.string_set(string)
odml.dtypes.time_get(string)
odml.dtypes.time_set(string)
odml.dtypes.tuple_get(string, count=None)

Parse a tuple string like “(1024;768)” and return strings of the elements

odml.dtypes.tuple_set(value)
odml.dtypes.valid_type(dtype)

Checks if dtype is a valid odML value data type.

Tools

Several tools are provided with the odml.tools package.

DictParser

Dict parser converts the content of a dictionary into a proper and verified odML document.

class odml.tools.dict_parser.DictReader(show_warnings=True)

A reader to parse dictionaries with odML content into a proper odML document.

is_valid_attribute(attr, fmt)
parse_properties(props_list)
parse_sections(section_list)
to_odml(parsed_doc)
class odml.tools.dict_parser.DictWriter

A writer to parse an odML document to a Python dictionary object equivalent.

static get_properties(props_list)
get_sections(section_list)
to_dict(odml_document)

ODMLParser

A generic odML parsing module.

Parses odML files and documents.

class odml.tools.odmlparser.JSONDateTimeSerializer(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
default(o)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
encode(o)

Return a JSON string representation of a Python data structure.

>>> from json.encoder import JSONEncoder
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'
item_separator = ', '
iterencode(o, _one_shot=False)

Encode the given object and yield each string representation as available.

For example:

for chunk in JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)
key_separator = ': '
class odml.tools.odmlparser.ODMLReader(parser='XML', show_warnings=True)

A reader to parse odML files or strings into odml documents, based on the given data exchange format, like XML, YAML, JSON or RDF.

Usage:
yaml_odml_doc = ODMLReader(parser=’YAML’).from_file(“odml_doc.yaml”) json_odml_doc = ODMLReader(parser=’JSON’).from_file(“odml_doc.json”)
from_file(file, doc_format=None)
from_string(string, doc_format=None)
class odml.tools.odmlparser.ODMLWriter(parser='XML')

A generic odML document writer, for XML, YAML and JSON.

Usage:
xml_writer = ODMLWriter(parser=’XML’) xml_writer.write_file(odml_document, filepath)
to_string(odml_document)
write_file(odml_document, filename)
odml.tools.odmlparser.UnicodeLoaderConstructor(loader, node)
odml.tools.odmlparser.YAMLTimeSerializer(dumper, data)

RDFConverter

The RDF converter module provides conversion of odML documents to RDF and the conversion of odML flavored RDF to odML documents.

class odml.tools.rdf_converter.RDFReader(filename=None, doc_format=None)

A reader to parse odML RDF files or strings into odML documents.

Usage:
file = RDFReader().from_file(“/path_to_input_rdf”, “rdf_format”) file = RDFReader().from_string(“rdf file as string”, “rdf_format”) RDFReader().write_file(“/input_path”, “rdf_format”, “/output_path”)
from_file(filename, doc_format)

from_file loads an odML RDF file and converts all odML documents from this RDF graph into individual odML documents.

Parameters:
  • filename – Path of the input odML RDF file.
  • doc_format – RDF format of the input odML RDF file.
Returns:

list of converted odML documents

from_string(file, doc_format)

from_string loads an odML RDF file or string object and converts all odML documents from this RDF graph into individual odML documents.

Parameters:
  • file – Path of the input odML RDF file or an RDF graph string object.
  • doc_format – RDF format of the input odML RDF graph.
Returns:

list of converted odML documents

parse_document(doc_uri)

parse_document parses an odML RDF Document node into an odML Document.

Parameters:doc_uri – RDF URI of an odML Document node within an RDF graph.
Returns:dict containing an odML Document
parse_property(prop_uri)

parse_property parses an odML RDF Property node into an odML Property.

Parameters:prop_uri – RDF URI of an odML Property node within an RDF graph.
Returns:dict containing an odML Property
parse_section(sec_uri)

parse_section parses an odML RDF Section node into an odML Section.

Parameters:sec_uri – RDF URI of an odML Section node within an RDF graph.
Returns:dict containing an odML Section
to_odml()

to_odml converts all odML documents from a common RDF graph into individual odML documents.

Returns:list of converted odML documents
class odml.tools.rdf_converter.RDFWriter(odml_documents)

A writer to parse odML files into RDF documents.

Usage:
RDFWriter(odml_docs).get_rdf_str(‘turtle’) RDFWriter(odml_docs).write_file(“/output_path”, “rdf_format”)
convert_to_rdf()

convert_to_rdf converts all odML documents to RDF, connects them via a common “Hub” RDF node and returns the created RDF graph.

Returns:An RDF graph.
get_rdf_str(rdf_format='turtle')

Convert the current odML content of the parser to a common RDF graph and return the graph as a string object in the specified RDF format.

Parameters:rdf_format – RDF output format. Default format is ‘turtle’. Available formats: ‘xml’, ‘n3’, ‘turtle’, ‘nt’, ‘pretty-xml’, ‘trix’, ‘trig’, ‘nquads’, ‘json-ld’.
Returns:string object
save_document(doc, curr_node=None)

Add the current odML Document to the RDF graph and handle all child elements recursively.

Parameters:
  • doc – An odml Document that should be added to the RDF graph.
  • curr_node – An RDF node that is used to append the current odml element to the Hub node of the current RDF graph.
save_odml_list(parent_node, rdf_predicate, odml_list)

save_odml_list adds all odml elements in a list to the current parent node and handles all child items via save_element.

Parameters:
  • parent_node – current parent node in the RDF graph.
  • rdf_predicate – RDF predicate used to add all odml entities to the parent node.
  • odml_list – list of odml entities.
save_odml_values(parent_node, rdf_predicate, values)

save_odml_values adds an RDF seq node to the parent RDF node and creates a value leaf node for every odml value.

Parameters:
  • parent_node – current parent node in the RDF graph.
  • rdf_predicate – RDF predicate used to add the Seq node to the current parent node.
  • values – list of odml values.
save_property(prop, curr_node)

Add the current odML Property to the RDF graph and handle all child elements.

Parameters:
  • prop – An odml Section that should be added to the RDF graph.
  • curr_node – An RDF node that is used to append the current odml element to the current RDF graph.
save_repository_node(parent_node, rdf_predicate, leaf_value)

save_repository_node adds a node with a given repository url to the current graphs terminology node. If the current graph does not yet contain a terminology node, it creates one and attaches the current node to it.

Parameters:
  • parent_node – current parent node in the RDF graph.
  • rdf_predicate – RDF predicate used to add the terminology to the parent node.
  • leaf_value – Value that will be added to the RDF graph.
save_section(sec, curr_node)

Add the current odML Section to the RDF graph and handle all child elements recursively.

Parameters:
  • sec – An odml Section that should be added to the RDF graph.
  • curr_node – An RDF node that is used to append the current odml element to the current RDF graph.
write_file(filename, rdf_format='turtle')

Convert the current odML content of the parser to a common RDF graph and write the resulting graph to an output file using the provided RDF output format.

Parameters:
  • filename
  • rdf_format – RDF output format. Default format is ‘turtle’. Available formats: ‘xml’, ‘n3’, ‘turtle’, ‘nt’, ‘pretty-xml’, ‘trix’, ‘trig’, ‘nquads’, ‘json-ld’.
odml.tools.rdf_converter.load_rdf_subclasses()

load_rdf_subclasses loads odml section types to RDF Section subclass types mappings from a file and returns the mapping as a dictionary. Will return an empty dictionary, if the Subclasses file cannot be loaded.

Returns:Dictionary of the form {‘Section type’: ‘RDF class type’}

XMLParser

The XML parsing module. Parses odML files. Can be invoked standalone:

python -m odml.tools.xmlparser file.odml
class odml.tools.xmlparser.XMLReader(ignore_errors=False, show_warnings=True, filename=None)

A reader to parse xml-files or strings into odml data structures Usage:

>>> doc = XMLReader().from_file("file.odml")
check_mandatory_arguments(data, ArgClass, tag_name, node)
error(msg, elem)
from_file(xml_file)

parse the datastream from a file like object xml_file and return an odml data structure

from_string(string)
is_valid_argument(tag_name, ArgClass, parent_node, child=None)
parse_element(node)
parse_odML(root, fmt)
parse_property(root, fmt)
parse_section(root, fmt)
parse_tag(root, fmt, insert_children=True)

Parse an odml node based on the format description fmt and instantiate the corresponding object. :param root: lxml.etree node containing an odML object or object tree. :param fmt: odML class corresponding to the content of the root node. :param insert_children: Bool value. When True, child elements of the root node

will be parsed to their odML equivalents and appended to the odML document. When False, child elements of the root node will be ignored.
warn(msg, elem)
class odml.tools.xmlparser.XMLWriter(odml_document)

Creates XML nodes storing the information of an odML Document

header = '<?xml version="1.0" encoding="UTF-8"?>\n<?xml-stylesheet type="text/xsl" href="odmlDocument.xsl"?>\n'
static save_element(e)

returns an xml node for the odML object e

write_file(filename, local_style=False, custom_template=None)

write_file saves the XMLWriters odML document to an XML file. :param filename: location and name where the file will be written to. :param local_style: Optional boolean. By default an odML XML document is saved

with a default header containing an external stylesheet for viewing with a local or remote server. Set up for local viewing with the ‘odmlview’ command line script. When set to True, the saved XML file will contain a default XSL stylesheet to render the XML file in a web-browser. Note that Chrome requires the ‘.odml’ extension to be registered as “application/xml” in the Mime-type database.
Parameters:custom_template – Optional string. Provide a custom XSL template to render the odML XML file in a web-browser. Please note, that the custom XSL template must not be a full XSL stylesheet, but has to start and end with the tag: ‘<xsl:template match=”odML”>[custom]</xsl:template>’.
odml.tools.xmlparser.from_csv(value_string)
odml.tools.xmlparser.load(filename)

shortcut function for XMLReader().from_file(filename)

odml.tools.xmlparser.to_csv(val)

Convenience converters

Several convenience converters are provided with the odml.tools.converters package.

FormatConverter

class odml.tools.converters.format_converter.FormatConverter
classmethod convert(args=None)

Enable usage of the argparse for calling convert_dir(…) Example:

  1. >> python -m odml.tools.converters.format_converter ./..path../input_dir v1_1 -out ./..path../output_dir -r

    Convert files from the path <./..path../input_dir> to .xml odml version 1.1, writes them into <./..path../output_dir> including subdirectories and its files from the input path.

  2. >> python -m odml.tools.converters.format_converter ./..path../input_dir odml

    Converts files from path <./..path../input_dir> to .odml, writes them into <./..path../input_dir_odml> not including subdirectories.

classmethod convert_dir(input_dir, output_dir, parse_subdirs, res_format)

Convert files from given input directory to the specified res_format. :param input_dir: Path to input directory :param output_dir: Path for output directory. If None, new directory will be created on the same level as input :param parse_subdirs: If True enable converting files from subdirectories :param res_format: Format of output files.

Possible choices: “v1_1” (converts to version 1.1 from version 1 xml)
“odml” (converts to .odml from version 1.1 .xml files) “turtle”, “nt” etc. (converts to rdf formats from version 1.1 .odml files) (see full list of rdf serializers in CONVERSION_FORMATS)

VersionConverter

class odml.tools.converters.version_converter.VersionConverter(filename)

Class for converting odml xml files from version 1.0 to 1.1

convert(backend='XML')

This method returns the content of the provided file object converted to odML version 1.1 as a string object which is directly consumable by the odml.tools.ODMLReader.

write_to_file(filename, backend='XML')

This method converts the content of the provided converter file object to odML version 1.1 and writes the results to filename. :param filename: Output file. :param backend: Format of the source file, default is XML.

Command line scripts

Several cli convenience scripts are automatically installed and are available from the command line.

odML conversion script

odML to RDF script

odML view (browse odml files locally)