Ejemplo n.º 1
0
def get_string(length=31, existing=None):
    """Returns a random string for testing purposes.

    """
    result = str(uuid.uuid1())[:length]
    while existing == result:
        result = str(uuid.uuid1())[:length]
    return result
Ejemplo n.º 2
0
def _get_formatted_message(msg, level, app):
    """Returns a message formatted for logging.

    """
    if msg is None:
        return _NULL_MSG
    else:
        return '{} [{}] :: ESDOC-{} :: {}'.format(
            str(arrow.get())[0:-6], level, app,
            str(msg).strip())
Ejemplo n.º 3
0
    def get_comparator(key):
        """Returns an item from managed collection.

        """
        if isinstance(key, uuid.UUID):
            return lambda i: i.uid
        else:
            key = str(key).strip().lower()
            try:
                uuid.UUID(key)
            except ValueError:
                return lambda i: i.canonical_name
            else:
                return lambda i: str(i.uid)
Ejemplo n.º 4
0
    def set_node(self, node):
        """Sets node returned from archive search.

        """
        if node is None:
            raise ParsingError(self.typekey, self.name)

        # Confirm match based upon the level of parsing strictness perform test.
        matched = False
        if self.strictness == PARSING_STRICTNESS_0:
            if self.name == node.canonical_name:
                matched = True

        elif self.strictness == PARSING_STRICTNESS_1:
            if self.name == node.raw_name:
                matched = True

        elif self.strictness == PARSING_STRICTNESS_2:
            if self.name in {node.canonical_name, node.raw_name}:
                matched = True

        elif self.strictness == PARSING_STRICTNESS_3:
            if self.name in {node.canonical_name, node.raw_name}.union(set(node.alternative_names)):
                matched = True

        elif self.strictness == PARSING_STRICTNESS_4:
            name = str(self.name).strip().lower()
            if name in [i.lower() for i in node.all_names]:
                matched = True

        # Raise parsing error if appropriate.
        if matched == False:
            raise ParsingError(self.typekey, self.name)

        self.node = node
Ejemplo n.º 5
0
    def __contains__(self, key):
        """Instance membership predicate.

        """
        key = str(key).strip().lower()

        return key in self.all_names
Ejemplo n.º 6
0
    def get_name(self, target):
        """Gets parsing relative name.

        """
        if self.node is not None:
            return self.node.canonical_name
        elif self == target:
            return str(self.name).strip().lower()
Ejemplo n.º 7
0
def _to_encodable(obj, key_formatter=lambda k: k):
    """Converts data to encodeable representation.

    """
    if isinstance(obj, _ENCODE_IGNOREABLE):
        return obj

    elif isinstance(obj, _ENCODE_STRING):
        return str(obj)

    elif isinstance(obj, collections.Mapping):
        return {
            str(key_formatter(k)): _to_encodable(v)
            for k, v in iter(obj.items())
        }

    elif isinstance(obj, collections.Iterable):
        return [_to_encodable(i) for i in obj]
Ejemplo n.º 8
0
def format_canonical_name(name):
    """Formats a canonical name prior to accessing archive.

    """
    if name is not None:
        return str(name).strip() \
                        .replace('_', '-') \
                        .replace(' ', '-') \
                        .lower()
Ejemplo n.º 9
0
def assert_namespace(identifier, min_length=1, max_length=4):
    """Asserts a namespace.

    """
    assert_string(identifier)
    parts = str(identifier).split(':')
    assert len(parts) >= min_length and len(parts) <= max_length
    for part in parts:
        assert_string(part)
Ejemplo n.º 10
0
def format_io_name(name):
    """Formats a simple string.

    """
    if name is not None:
        return str(name).strip() \
                        .replace('_', '-') \
                        .replace(' ', '-') \
                        .lower()
Ejemplo n.º 11
0
def _decode_blob(val):
    """Converts input to a string literal.

    :param object val: value to be converted to a string literal.

    :returns: A string literal.
    :rtype: str

    """
    if val is None:
        return str()
    if isinstance(val, str):
        return val

    val = str(val).decode(_UTF8).strip()
    if not len(val):
        return str()

    return str(val)
Ejemplo n.º 12
0
def _decode_term(obj):
    """Decodes a term from a dictionary.

    """
    instance = _decode_node(obj, Term)
    instance.associations = obj.get('associations', [])
    instance.status = obj['status']
    if instance.parent:
        instance.parent = uuid.UUID(str(obj['parent']))

    return instance
Ejemplo n.º 13
0
def assert_str(actual, expected, startswith=False):
    """Asserts a string.

    :param str actual: Actual string value.
    :param str expected: Expected string value.

    :param startswith: Flag indicating whether to perform startswith test.
    :type startswith: bool

    """
    # Format.
    actual = str(actual).strip()
    expected = str(expected).strip()

    # Assert.
    if startswith == False:
        assert actual == expected, \
               'String mismatch : actual = {0} :: expected = {1}'.format(actual, expected)
    else:
        assert actual.startswith(expected) == True, \
               'String startswith mismatch : actual = {0} :: expected = {1}'.format(actual, expected)
Ejemplo n.º 14
0
def _load_by_namespace(identifier):
    """Loads a vocabulary node from archive by trying to match it's namespace.

    :param str identifier: Vocabulary node namespace.

    :returns: First matching vocabulary node.
    :rtype: pyessv.Node | None

    """
    # Skip if identifier is not a namespace.
    ns = str(identifier).split(':')
    if len(ns) == 0:
        return get_cached(Authority)
    if len(ns) > 4:
        return None

    # Unpack.
    authority = scope = collection = term = None
    if len(ns) == 1:
        authority = ns[0]
    elif len(ns) == 2:
        authority, scope = ns
    elif len(ns) == 3:
        authority, scope, collection = ns
    elif len(ns) == 4:
        authority, scope, collection, term = ns

    # Walk nodes returning deepest match.
    for a in get_cached(Authority):
        if not _is_matched(a, authority):
            continue
        if scope is None:
            return a
        # ... scopes
        for s in a:
            if not _is_matched(s, scope):
                continue
            if collection is None:
                return s
            # ... collections
            for c in s:
                if not _is_matched(c, collection):
                    continue
                if term is None:
                    return c
                # ... terms (concrete)
                for t in c:
                    if _is_matched(t, term):
                        return t
                # ... terms (virtual)
                if c.is_matched(term):
                    return create_term(c, term)
Ejemplo n.º 15
0
def encode(instance):
    """Encodes an instance of a domain model class as a JSON text blob.

    :param pyessv.Node instance: A domain model class instance to be encoded as a JSON text blob.

    :returns: Instance encoded as a JSON text blob.
    :rtype: str

    """
    # Convert to dictionary.
    obj = dict_encoder.encode(instance)

    # Return JSON.
    return str(dict_to_json(obj))
Ejemplo n.º 16
0
def cache(node):
    """Caches a vocabulary node.

    :param pyeesv.Node: Node to be cached.

    """
    _DATA[node.namespace] = node
    _DATA[str(node.uid)] = node
    try:
        iter(node)
    except TypeError:
        return
    else:
        for subnode in node:
            cache(subnode)
Ejemplo n.º 17
0
def _load_by_uid(identifier):
    """Loads a vocabulary node from archive by trying to match it's unique identifier.

    :param str identifier: Vocabulary node unique identifier.

    :returns: First matching vocabulary node.
    :rtype: pyessv.Node | None

    """
    try:
        uuid.UUID(identifier)
    except ValueError:
        pass
    else:
        return get_cached(str(identifier))
Ejemplo n.º 18
0
def _decode_node(obj, typeof):
    """Decodes a node instance from a dictionary representation.

    """
    instance = typeof()
    instance.alternative_names = obj.get('alternative_names', [])
    instance.create_date = arrow.get(obj['create_date']).datetime
    instance.data = obj.get('data', dict())
    instance.description = obj.get('description')
    instance.label = obj.get('label', obj['canonical_name'])
    instance.canonical_name = obj['canonical_name']
    instance.raw_name = obj.get('raw_name', obj['canonical_name'])
    instance.uid = uuid.UUID(str(obj['uid']))
    instance.url = obj.get('url')

    return instance
Ejemplo n.º 19
0
def parse(
    namespace,
    strictness=PARSING_STRICTNESS_2,
    field='canonical_name'
    ):
    """Parses a namespace within a vocabulary hierachy.

    :param str namespace: Vocabulary namespace, e.g. wcrp.
    :param int strictness: Strictness level to apply when applying lookup rules.
    :param str field: Term field to return.

    """
    assert strictness in PARSING_STRICTNESS_SET, 'Invalid parsing strictness'
    assert field in PARSING_NODE_FIELDS, 'Invalid field'

    # Set namespace
    ns = str(namespace).strip().split(':')
    assert len(ns) >= 1 and len(ns) <= 4, 'Invalid namespace'
    ns = ns + [None for i in range(4 - len(ns))]

    # Destructure.
    authority, scope, collection, term = ns

    # Set parsing targets.
    targets = [
        _NodeInfo('authority', authority, strictness),
        _NodeInfo('scope', scope, strictness),
        _NodeInfo('collection', collection, strictness),
        _NodeInfo('term', term, strictness)
    ]
    targets = [i for i in targets if i.name is not None]

    # Load parsed nodes.
    for target in targets:
        namespace = [i.get_name(target) for i in targets]
        namespace = [i for i in namespace if i is not None]
        namespace = ":".join(namespace)
        node = load(namespace)
        target.set_node(node)

    return getattr(target.node, field)
Ejemplo n.º 20
0
def load_random(namespace, field='canonical_name'):
    """Returns a random term.

    :param str namespace: Namespace of collection from which a term will be loaded.

    :returns: A random term's canonical name.
    :rtype: str

    """
    collection = load(namespace)
    if collection is None:
        raise ValueError('Collection not found: {}'.format(namespace))
    if field not in PARSING_NODE_FIELDS:
        raise ValueError('Invalid field name')

    if collection.is_virtual:
        return str(uuid.uuid4()).split('-')[0]

    term = random.choice(collection.terms)

    return getattr(term, field)
Ejemplo n.º 21
0
def format_string(val):
    """Formats a simple string.

    """
    if val is not None:
        return str(val).strip()