Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Update PR from my fork with the "builders" feature. #13

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Binary file added my.pkl
Binary file not shown.
16 changes: 13 additions & 3 deletions pyessv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

"""
__title__ = 'pyessv'
__version__ = '0.7.0.0'
__version__ = '0.7.0.1'
__author__ = 'ES-DOC'
__license__ = 'GPL'
__copyright__ = 'Copyright 2017 ES-DOC'
Expand Down Expand Up @@ -62,18 +62,28 @@
from pyessv._governance import reset

from pyessv._initializer import init
from pyessv._initializer import load_cv

from pyessv._loader import load_random
from pyessv._loader import load
from pyessv._loader import all_scopes

from pyessv._model import Authority
from pyessv._model import Collection
from pyessv._model import Scope
from pyessv._model import Term

from pyessv._builders import build_dataset_identifier
from pyessv._builders import build_directory
from pyessv._builders import build_filename

from pyessv._parser import parse
from pyessv._parsers import parse_dataset_identifer
from pyessv._parsers import parse_dataset_identifers
from pyessv._parsers import parse_dataset_identifier
from pyessv._parsers import parse_dataset_identifiers
from pyessv._parsers import parse_directory
from pyessv._parsers import parse_directories
from pyessv._parsers import parse_filename
from pyessv._parsers import parse_filenames

from pyessv._utils.logger import log
from pyessv._utils.logger import log_error
Expand Down
91 changes: 91 additions & 0 deletions pyessv/_builder_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# -*- coding: utf-8 -*-

"""
.. module:: pyessv._model._builder_template.py
:copyright: Copyright "December 01, 2016", IPSL
:license: GPL/CeCIL
:platform: Unix, Windows
:synopsis: A vocabulary constrained template builder, e.g. a dataset identifier.

.. moduleauthor:: Mark Conway-Greenslade <[email protected]>


"""
from pyessv._model import Collection, Term
from pyessv._exceptions import TemplateParsingError, TemplateValueError
from pyessv._constants import BUILDER_FIELDS
from pyessv._utils.compat import basestring, str


class TemplateBuilder(object):
"""A vocabulary template builder.

"""
def __init__(self, template, collections, strictness, separator='.'):
"""Instance constructor.

:param str template: Identifier template.
:param tuple collections: pyessv collection identifiers.
:param int strictness: Strictness level to apply when applying name matching rules.
:param str seprarator: Separator to apply when parsing.

"""
from pyessv._loader import load

self.separator = separator
self.template_parts = template.split(separator)
self.template = template
self.strictness = strictness

# Inject pyessv collections into template.
collection_idx = 0
for idx, part in enumerate(self.template_parts):
if part == '{}':
self.template_parts[idx] = load(collections[collection_idx])
collection_idx += 1

def build(self, terms, att='label', alt_name=0):
"""Build template instance from a list of pyessv terms.

:returns: Template instance string.

"""
assert isinstance(alt_name, int), 'Invalid alternative name index'
assert att in BUILDER_FIELDS, 'Invalid name'

# Instantiate string parts.
string_parts = list()

# Iterate template.
for template_part in self.template_parts:

# Append constant match.
if isinstance(template_part, basestring):
string_parts.append(template_part)
continue

# Append term match.
collection = template_part
term = None
for term in terms:
if term.collection.namespace == collection.namespace:
break

# Append term from associations.
if not term:
for term in [association for association in t.associations for t in terms]:
if term.collection.namespace == collection.namespace:
break

# Verify collection is found among terms.
if not term:
raise TemplateValueError('Collection not found among terms :: {}'.format(collection))

# Get term field.
if att == 'alternative_names':
string_parts.append(getattr(term, att)[alt_name])
else:
string_parts.append(getattr(term, att))


return self.separator.join(string_parts)
17 changes: 17 additions & 0 deletions pyessv/_builders/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-

"""
.. module:: pyessv._builders.__init__.py
:copyright: Copyright "December 01, 2016", IPSL
:license: GPL/CeCIL
:platform: Unix, Windows
:synopsis: Expression builders.

.. moduleauthor:: Mark Conway-Greenslade <[email protected]>


"""

from pyessv._builders.dataset_id import build_dataset_identifier
from pyessv._builders.directory import build_directory
from pyessv._builders.filename import build_filename
77 changes: 77 additions & 0 deletions pyessv/_builders/dataset_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-

"""
.. module:: pyessv._builders.dataset_id.py
:copyright: Copyright "December 01, 2016", IPSL
:license: GPL/CeCIL
:platform: Unix, Windows
:synopsis: Encapsulates building of an ESGF dataset identifier.

.. moduleauthor:: Mark Conway-Greenslade <[email protected]>

"""
from pyessv._model.term import Term
from pyessv import all_scopes
from pyessv._constants import PARSING_STRICTNESS_1
from pyessv._factory import create_template_builder
from pyessv._utils.compat import basestring

# Instantiated template
_TEMPLATE = None

# Instantiated template collections
_COLLECTIONS = None

# Instantiated project.
_PROJECT = None

# Instantiated builder.
_BUILDER = None


def build_dataset_identifier(project, terms):
"""Builds a dataset identifier.

:param str project: Project code.
:param set terms: Dataset identifier terms.

:returns: Dataset identifier.
:rtype: str

"""
assert isinstance(project, basestring), 'Invalid project'
assert isinstance(terms, set), 'Invalid terms'

global _PROJECT, _BUILDER, _TEMPLATE, _COLLECTIONS

if _PROJECT != project:

# Get scope corresponding to the project code.
scopes = all_scopes()
assert project in [scope.name for scope in scopes], 'Unsupported project'
scope = [scope for scope in scopes if scope.name == project][0]

assert 'dataset_id' in scope.data.keys(), 'Dataset ID parser not found'
assert 'template' in scope.data['dataset_id'].keys(), 'Dataset ID parser template not found'
assert 'collections' in scope.data['dataset_id'].keys(), 'Dataset ID parser template collections not found'

# Get template from data scope.
_TEMPLATE = scope.data['dataset_id']['template']
assert isinstance(_TEMPLATE, basestring), 'Invalid template'

# Get template collections from data scope.
_COLLECTIONS = list()
for name in scope.data['dataset_id']['collections']:
_COLLECTIONS.append([collection.namespace for collection in scope.collections if collection.name == name.replace('_','-')][0])
assert _COLLECTIONS, 'Invalid collections'

# Instantiate parser JIT.
_BUILDER = create_template_builder(_TEMPLATE, tuple(_COLLECTIONS), PARSING_STRICTNESS_1)

# Cached project.
_PROJECT = project

for term in terms:
assert isinstance(term, Term), 'Invalid term :: {}'.format(term)

return _BUILDER.build(terms)
77 changes: 77 additions & 0 deletions pyessv/_builders/directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-

"""
.. module:: pyessv._builders.dataset_id.py
:copyright: Copyright "December 01, 2016", IPSL
:license: GPL/CeCIL
:platform: Unix, Windows
:synopsis: Encapsulates building of an ESGF dataset identifier.

.. moduleauthor:: Mark Conway-Greenslade <[email protected]>

"""
from pyessv._model.term import Term
from pyessv import all_scopes
from pyessv._constants import PARSING_STRICTNESS_1
from pyessv._factory import create_template_builder
from pyessv._utils.compat import basestring

# Instantiated template
_TEMPLATE = None

# Instantiated template collections
_COLLECTIONS = None

# Instantiated project.
_PROJECT = None

# Instantiated builder.
_BUILDER = None


def build_directory(project, terms):
"""Builds a directory.

:param str project: Project code.
:param set terms: Directory terms.

:returns: Directory string.
:rtype: str

"""
assert isinstance(project, basestring), 'Invalid project'
assert isinstance(terms, set), 'Invalid terms'

global _PROJECT, _BUILDER, _TEMPLATE, _COLLECTIONS

if _PROJECT != project:

# Get scope corresponding to the project code.
scopes = all_scopes()
assert project in [scope.name for scope in scopes], 'Unsupported project'
scope = [scope for scope in scopes if scope.name == project][0]

assert 'directory_structure' in scope.data.keys(), 'Directory parser not found'
assert 'template' in scope.data['directory_structure'].keys(), 'Directory parser template not found'
assert 'collections' in scope.data['directory_structure'].keys(), 'Directory parser template collections not found'

# Get template from data scope.
_TEMPLATE = scope.data['directory_structure']['template']
assert isinstance(_TEMPLATE, basestring), 'Invalid template'

# Get template collections from data scope.
_COLLECTIONS = list()
for name in scope.data['directory_structure']['collections']:
_COLLECTIONS.append([collection.namespace for collection in scope.collections if collection.name == name.replace('_','-')][0])
assert _COLLECTIONS, 'Invalid collections'

# Instantiate parser JIT.
_BUILDER = create_template_builder(_TEMPLATE, tuple(_COLLECTIONS), PARSING_STRICTNESS_1, separator='/')

# Cached project.
_PROJECT = project

for term in terms:
assert isinstance(term, Term), 'Invalid term :: {}'.format(term)

return _BUILDER.build(terms)
77 changes: 77 additions & 0 deletions pyessv/_builders/filename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-

"""
.. module:: pyessv._builders.dataset_id.py
:copyright: Copyright "December 01, 2016", IPSL
:license: GPL/CeCIL
:platform: Unix, Windows
:synopsis: Encapsulates building of an ESGF dataset identifier.

.. moduleauthor:: Mark Conway-Greenslade <[email protected]>

"""
from pyessv._model.term import Term
from pyessv import all_scopes
from pyessv._constants import PARSING_STRICTNESS_1
from pyessv._factory import create_template_builder
from pyessv._utils.compat import basestring

# Instantiated template
_TEMPLATE = None

# Instantiated template collections
_COLLECTIONS = None

# Instantiated project.
_PROJECT = None

# Instantiated builder.
_BUILDER = None


def build_filename(project, terms):
"""Builds a filename.

:param str project: Project code.
:param set terms: Filename terms.

:returns: Filename string.
:rtype: str

"""
assert isinstance(project, basestring), 'Invalid project'
assert isinstance(terms, set), 'Invalid terms'

global _PROJECT, _BUILDER, _TEMPLATE, _COLLECTIONS

if _PROJECT != project:

# Get scope corresponding to the project code.
scopes = all_scopes()
assert project in [scope.name for scope in scopes], 'Unsupported project'
scope = [scope for scope in scopes if scope.name == project][0]

assert 'filename' in scope.data.keys(), 'Filename parser not found'
assert 'template' in scope.data['filename'].keys(), 'Filename parser template not found'
assert 'collections' in scope.data['filename'].keys(), 'Filename parser template collections not found'

# Get template from data scope.
_TEMPLATE = scope.data['filename']['template']
assert isinstance(_TEMPLATE, basestring), 'Invalid template'

# Get template collections from data scope.
_COLLECTIONS = list()
for name in scope.data['filename']['collections']:
_COLLECTIONS.append([collection.namespace for collection in scope.collections if collection.name == name.replace('_','-')][0])
assert _COLLECTIONS, 'Invalid collections'

# Instantiate parser JIT.
_BUILDER = create_template_builder(_TEMPLATE, tuple(_COLLECTIONS), PARSING_STRICTNESS_1, separator='_')

# Cached project.
_PROJECT = project

for term in terms:
assert isinstance(term, Term), 'Invalid term :: {}'.format(term)

return _BUILDER.build(terms)
2 changes: 1 addition & 1 deletion pyessv/_codecs/json_codec/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pyessv._utils import convert
from pyessv._utils.compat import json
from pyessv._utils.compat import numeric_types
from pyessv._utils.compat import str
from pyessv._utils.compat import str, basestring



Expand Down
Loading