def load(self, uuid): """Loads `uuid` from current storage and return it as a new instance.""" uuid = dlite.get_uuid(uuid) q = sql.SQL('SELECT meta FROM uuidtable WHERE uuid = %s') self.cur.execute(q, [uuid]) metaid, = self.cur.fetchone() q = sql.SQL('SELECT * FROM {} WHERE uuid = %s').format( sql.Identifier(metaid)) self.cur.execute(q, [uuid]) tokens = self.cur.fetchone() uuid_, uri, metaid_, dims = tokens[:4] values = tokens[4:] assert uuid_ == uuid assert metaid_ == metaid # Make sure we have metadata object correcponding to metaid try: with dlite.err(): meta = dlite.get_instance(metaid) except RuntimeError: dlite.errclr() meta = self.load(metaid) inst = dlite.Instance.create_from_metaid(metaid, dims, uri) for i, p in enumerate(inst.meta['properties']): inst.set_property(p.name, values[i]) # The uuid will be wrong for data instances, so override it if not inst.is_metameta: d = inst.asdict() d['uuid'] = uuid inst = instance_from_dict(d) return inst
def find_label(self, inst): """Returns label for class instance `inst` already added to the collection.""" if hasattr(inst, 'uuid'): uuid = inst.uuid else: uuid = dlite.get_uuid(inst) rel = self.coll.find_first(p='_has-uuid', o=uuid) if not rel: raise ValueError('no class instance with UUID: %s' % uuid) return rel.s
def load(self, uuid): """Loads `uuid` from current storage and return it as a new instance.""" uuid = dlite.get_uuid(uuid) q = sql.SQL('SELECT meta FROM uuidtable WHERE uuid = %s') self.cur.execute(q, [uuid]) metaid, = self.cur.fetchone() q = sql.SQL('SELECT * FROM {} WHERE uuid = %s').format( sql.Identifier(metaid)) self.cur.execute(q, [uuid]) tokens = self.cur.fetchone() uuid_, uri, metaid_, dims = tokens[:4] values = tokens[4:] assert uuid_ == uuid assert metaid_ == metaid inst = dlite.Instance(metaid, dims, uri) for i, p in enumerate(inst.meta['properties']): inst.set_property(p.name, values[i]) return inst
def get_uuid(self, uri=None): """Returns a UUID corresponding to `uri`. If `uri` is None, a random UUID is returned.""" return dlite.get_uuid(uri)
#!/usr/bin/env python # -*- coding: utf-8 -*- import dlite assert dlite.get_uuid_version() == 4 assert dlite.get_uuid_version('abc') == 5 assert dlite.get_uuid_version('6cb8e707-0fc5-5f55-88d4-d4fed43e64a8') == 0 assert dlite.get_uuid('abc') == '6cb8e707-0fc5-5f55-88d4-d4fed43e64a8' assert dlite.get_uuid('6cb8e707-0fc5-5f55-88d4-d4fed43e64a8') == ( '6cb8e707-0fc5-5f55-88d4-d4fed43e64a8') assert dlite.join_meta_uri('name', 'version', 'ns') == 'ns/version/name' assert dlite.split_meta_uri('ns/version/name') == ['name', 'version', 'ns'] assert dlite.join_url('driver', 'loc', 'mode=r', 'fragment') == ( 'driver://loc?mode=r#fragment') assert dlite.join_url('driver', 'loc', 'mode=r') == 'driver://loc?mode=r' assert dlite.join_url('driver', 'loc') == 'driver://loc' assert dlite.join_url('driver', 'loc', fragment='frag') == 'driver://loc#frag' assert dlite.split_url('driver://loc?mode=r#fragment') == [ 'driver', 'loc', 'mode=r', 'fragment'] assert dlite.split_url('driver://loc?mode=r&verbose=1') == [ 'driver', 'loc', 'mode=r&verbose=1', ''] assert dlite.split_url('driver://loc#fragment') == [ 'driver', 'loc', '', 'fragment'] assert dlite.split_url('loc#fragment') == [ '', 'loc', '', 'fragment']