def _get_formatted_message(msg, level, app): """Returns a message formatted for logging. """ if msg is None: return _NULL_MSG else: return '{} [{}] :: {} :: {}'.format(compat.str(dt.datetime.utcnow()), level, app, compat.str(msg).strip())
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 = compat.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
def __contains__(self, key): """Instance membership predicate. """ key = compat.str(key).strip().lower() return key in self.all_names
def get_name(self, target): """Gets parsing relative name. """ if self.node is not None: return self.node.canonical_name elif self == target: return compat.str(self.name).strip().lower()
def assert_namespace(identifier, min_length=1, max_length=4): """Asserts a namespace. """ assert_string(identifier) parts = compat.str(identifier).split(':') assert len(parts) >= min_length and len(parts) <= max_length for part in parts: assert_string(part)
def format_io_name(name): """Formats a simple string. """ if name is not None: return compat.str(name) \ .strip() \ .replace('_', '-') \ .replace(' ', '-') \ .lower()
def format_canonical_name(name): """Formats a canonical name prior to accessing archive. """ if name is not None: return compat.str(name) \ .strip() \ .replace('_', '-') \ .replace(' ', '-') \ .lower()
def format_attribute_name(name): """Formats an attribute name prior to accessing either a managed collection or arbitrary data. """ if name is not None: return compat.str(name) \ .strip() \ .replace('_', '-') \ .replace(' ', '-') \ .lower()
def str_to_unicode(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 compat.str() if isinstance(val, compat.str): return val val = compat.str(val).decode(_UTF8).strip() if not len(val): return compat.str() return compat.str(val)
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 = compat.str(actual).strip() expected = compat.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)
def _to_encodable(obj, key_formatter=lambda k: k): """Converts data to encodeable representation. """ if isinstance(obj, _ENCODE_IGNOREABLE): return obj elif isinstance(obj, compat.basestring): return compat.str(obj) elif isinstance(obj, datetime.datetime): return '{}+00:00'.format(compat.str(obj)[:19]) elif isinstance(obj, compat.Mapping): return { compat.str(key_formatter(k)): _to_encodable(v) for k, v in iter(obj.items()) } elif isinstance(obj, compat.Iterable): return [_to_encodable(i) for i in obj]
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 = compat.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 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. as_json = compat.str(dict_to_json(obj)) return as_json
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 = compat.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)
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 compat.str(uuid.uuid4()).split('-')[0] term = random.choice(collection.terms) return getattr(term, field)
def format_string(val): """Formats a simple string. """ if val is not None: return compat.str(val).strip()