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)
def test_uncache(): """pyessv-tests: caching: uncache """ uncache(tu.AUTHORITY_NAME) assert get_cached(tu.AUTHORITY_NAME) is None
def archive(authority): """Archive authority to file system. """ cache(authority) for authority in get_cached(Authority): write(authority)
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))
def load(identifier=None, verbose=True): """Loads a vocabulary node from archive. :param str identifier: Vocabulary node identifier. :return: A vocabulary node. :rtype: pyessv.Node | None """ assert isinstance(identifier, (type(None), basestring)) if identifier is None: return set(get_cached(Authority)) identifier = identifier.strip() result = _load_by_namespace(identifier) if result is None and verbose: logger.log_warning( 'Cannot map identifier to a vocabulary entity: {}'.format( identifier)) return result
def test_cache(): """pyessv-tests: caching: cache """ assert isinstance(get_cached(tu.AUTHORITY_NAME), pyessv.Authority)