Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Parser and converter refactoring I #220

Merged
merged 31 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a79c674
[format] Add get odml format name method
mpsonntag Jan 22, 2018
46b50b1
[format] Add get odml format argument items method
mpsonntag Jan 22, 2018
7ba6456
[format] Add get odml fmt arg keys method
mpsonntag Jan 22, 2018
7726c0f
[odmlparser] Use format map getter
mpsonntag Jan 22, 2018
4aaca5a
[format] Add get odml format map keys method
mpsonntag Jan 22, 2018
cc33deb
[format] Add get rdf map keys and items methods
mpsonntag Jan 22, 2018
f72df22
[xmlparser] Rem obsolete custom format attribute
mpsonntag Jan 23, 2018
796a200
[xmlparser] Remove dead code
mpsonntag Jan 23, 2018
17cc3a0
[xmlparser] Remove xml attribute parsing code
mpsonntag Jan 23, 2018
5926afc
[xmlparser] Remove unused value code
mpsonntag Jan 23, 2018
df6ce65
[base] Add and use get format method
mpsonntag Jan 23, 2018
0d19117
[property] Cleanup ToDo's
mpsonntag Jan 23, 2018
9b34fbf
[dict_parser] Add file
mpsonntag Jan 24, 2018
49384cb
[odmlparser] Use dict_parser for json and yaml
mpsonntag Jan 24, 2018
e5e0ca1
[rdf_converter] Switch ODMLReader to DictReader
mpsonntag Jan 24, 2018
a63e117
[parser_utils] Add file
mpsonntag Jan 24, 2018
36ccc2e
[parsers] Consistently use ParserException
mpsonntag Jan 24, 2018
55b7b4f
[parsers] Consistently use SUPPORTED_PARSERS
mpsonntag Jan 24, 2018
f07b53a
[odmlparser] Resolve RDFReader cyclic import error
mpsonntag Jan 24, 2018
9f11b65
[odmlparser] Resolve Validation cyclic import err
mpsonntag Jan 24, 2018
4be10cf
[dict_parser] Add DictWriter class
mpsonntag Jan 24, 2018
f908007
[odmlparser] Use DictWriter class
mpsonntag Jan 24, 2018
0a039c8
[odmlparser] Add no RDF ODMLWriter warning
mpsonntag Jan 24, 2018
8723343
[odmlparser] Refactor ODMLWriter
mpsonntag Jan 24, 2018
1c31307
[odmlparser] Remove unused ODMLReader code
mpsonntag Jan 24, 2018
06f995c
Cleanup imports
mpsonntag Jan 24, 2018
bd1c6f4
[rdf_converter] Remove RDFReader write method
mpsonntag Jan 24, 2018
53f689c
[rdf_converter] Quick fix broken RDFWriter
mpsonntag Jan 24, 2018
f8a3ee9
[rdf_converter] Use 'with' to write file
mpsonntag Jan 24, 2018
56ee257
[format] Use proper properties
mpsonntag Jan 25, 2018
ed5d31c
[jsonparser] Remove unused file
mpsonntag Jan 25, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion odml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .dtypes import DType
from .fileio import load, save, display
from .info import VERSION
from .tools.odmlparser import allowed_parsers as parsers
from .tools.parser_utils import SUPPORTED_PARSERS as PARSERS

__version__ = VERSION

Expand Down
8 changes: 6 additions & 2 deletions odml/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"""

import posixpath
from odml import terminology
from odml.tools.doc_inherit import inherit_docstring, allow_inherit_docstring

from . import terminology
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring


class _baseobj(object):
Expand All @@ -15,6 +16,9 @@ class _baseobj(object):
class baseobject(_baseobj):
_format = None

def format(self):
return self._format

@property
def document(self):
""" Returns the Document object in which this object is contained """
Expand Down
10 changes: 5 additions & 5 deletions odml/doc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8
import uuid

import odml.base as base
import odml.dtypes as dtypes
import odml.format as format
import odml.terminology as terminology
from odml.tools.doc_inherit import inherit_docstring, allow_inherit_docstring
from . import base
from . import dtypes
from . import format
from . import terminology
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring


class Document(base._baseobj):
Expand Down
4 changes: 1 addition & 3 deletions odml/fileio.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
from .tools.odmlparser import ODMLReader, ODMLWriter, allowed_parsers

PARSERS = allowed_parsers
from .tools.odmlparser import ODMLReader, ODMLWriter


def load(filename, backend="xml"):
Expand Down
33 changes: 33 additions & 0 deletions odml/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,53 @@


class Format(object):
_name = ""
_args = {}
_map = {}
_rev_map = None
_rdf_map = {}
_rdf_type = None
_ns = Namespace("https://g-node.org/projects/odml-rdf#")

@property
def name(self):
"""Returns the name of the current odML format"""
return self._name

@property
def arguments(self):
"""Returns all items in the current odML format argument dict"""
return self._args.items()

@property
def arguments_keys(self):
"""Returns all keys of the current odML format argument dict"""
return self._args.keys()

def map(self, name):
""" Maps an odml name to a python name """
return self._map.get(name, name)

@property
def map_keys(self):
"""Returns all keys of the current odML format map dict"""
return self._map.keys()

def rdf_map(self, name):
""" Maps a python name to a odml rdf namespace """
return self._rdf_map.get(name, name)

@property
def rdf_map_keys(self):
"""Returns all keys of the current odML format RDF map dict"""
return self._rdf_map.keys()

@property
def rdf_map_items(self):
"""Returns all items of the current odML format RDF map dict"""
return self._rdf_map.items()

@property
def rdf_type(self):
""" Return rdf type of an object """
return self._rdf_type
Expand Down
31 changes: 17 additions & 14 deletions odml/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import uuid

import odml.base as base
import odml.dtypes as dtypes
import odml.format as frmt
from odml.tools.doc_inherit import inherit_docstring, allow_inherit_docstring
from . import base
from . import dtypes
from . import format as frmt
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring


class Property(base._baseobj):
Expand All @@ -19,8 +19,8 @@ class BaseProperty(base.baseobject, Property):

def __init__(self, name, value=None, parent=None, unit=None,
uncertainty=None, reference=None, definition=None,
dependency=None, dependency_value=None, dtype=None, value_origin=None, id=None):
#TODO add description to :param value_origin
dependency=None, dependency_value=None, dtype=None,
value_origin=None, id=None):
"""
Create a new Property with a single value. The method will try to infer
the value's dtype from the type of the value if not explicitly stated.
Expand All @@ -46,7 +46,7 @@ def __init__(self, name, value=None, parent=None, unit=None,
:param dependency_value: Dependency on a certain value.
:param dtype: the data type of the values stored in the property,
if dtype is not given, the type is deduced from the values
:param value_origin:
:param value_origin: Reference where the value originated from e.g. a file name.
"""
# TODO validate arguments
try:
Expand Down Expand Up @@ -90,16 +90,19 @@ def __repr__(self):
def dtype(self):
"""
The data type of the value
If the data type is changed, it is tried, to convert the value to the
new type.
If this doesn't work, the change is refused.
This behaviour can be overridden by directly accessing the *_dtype*
attribute and adjusting the *data* attribute manually.
"""
return self._dtype

@dtype.setter
def dtype(self, new_type):
"""
If the data type of a property value is changed, it is tried
to convert the value to the new type.
If this doesn't work, the change is refused.

This behaviour can be overridden by directly accessing the *_dtype*
attribute and adjusting the *data* attribute manually.
"""
# check if this is a valid type
if not dtypes.valid_type(new_type):
raise AttributeError("'%s' is not a valid type." % new_type)
Expand Down Expand Up @@ -138,15 +141,15 @@ def parent(self, new_parent):
"odml.Property.parent: passed value is not of consistent type!"
"odml.Section expected")

def _validate_parent(self, new_parent):
@staticmethod
def _validate_parent(new_parent):
from odml.section import BaseSection
if isinstance(new_parent, BaseSection):
return True
return False

@property
def value(self):
# FIXME check the usage of value in the xmlwriter, jsonwriter
return self._value

def value_str(self, index=0):
Expand Down
12 changes: 6 additions & 6 deletions odml/section.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# -*- coding: utf-8
import uuid

import odml.base as base
import odml.format as format
import odml.terminology as terminology
from odml.doc import BaseDocument
from . import base
from . import format
from . import terminology
from .doc import BaseDocument
# this is supposedly ok, as we only use it for an isinstance check
from odml.property import Property
from .property import Property
# it MUST however not be used to create any Property objects
from odml.tools.doc_inherit import inherit_docstring, allow_inherit_docstring
from .tools.doc_inherit import inherit_docstring, allow_inherit_docstring


class Section(base._baseobj):
Expand Down
4 changes: 3 additions & 1 deletion odml/terminology.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
except ImportError:
import urllib2

from .tools.parser_utils import ParserException


REPOSITORY = 'http://portal.g-node.org/odml/terminologies/v1.1/terminologies.xml'

Expand Down Expand Up @@ -82,7 +84,7 @@ def _load(self, url):
term = odml.tools.xmlparser.XMLReader(
filename=url, ignore_errors=True).from_file(fp)
term.finalize()
except odml.tools.xmlparser.ParserException as e:
except ParserException as e:
print("Failed to load %s due to parser errors" % url)
print(' "%s"' % e)
term = None
Expand Down
1 change: 0 additions & 1 deletion odml/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from odml.tools import dumper
from odml.tools import format_converter
from odml.tools import jsonparser
from odml.tools import odmlparser
from odml.tools import rdf_converter
from odml.tools import version_converter
Expand Down
Loading