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
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())
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)
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
def __contains__(self, key): """Instance membership predicate. """ key = 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 str(self.name).strip().lower()
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]
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()
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)
def format_io_name(name): """Formats a simple string. """ if name is not None: return str(name).strip() \ .replace('_', '-') \ .replace(' ', '-') \ .lower()
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)
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
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)
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 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))
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)
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 _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
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)
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)
def format_string(val): """Formats a simple string. """ if val is not None: return str(val).strip()