示例#1
0
文件: utillib.py 项目: robot-1/h5pyd
def create_links(gsrc, gdes, ctx):
    # add soft and external links
    srcid_desobj_map = ctx["srcid_desobj_map"]
    if ctx["verbose"]:
        print("create_links: {}".format(gsrc.name))
    for title in gsrc:
        msg = "got link: {}".format(title)
        if ctx["verbose"]:
            print(msg)
        logging.info(msg)
        lnk = gsrc.get(title, getlink=True)
        link_classname = lnk.__class__.__name__
        if link_classname == "HardLink":
            logging.debug("Got hardlink: {} gsrc: {} gdes: {}".format(title, gsrc, gdes))
            if title not in gdes:
                msg = "creating link {} with title: {}".format(gdes, title)
                if ctx["verbose"]:
                    print(msg)
                logging.info(msg)
                src_obj_id = gsrc[title].id
                src_obj_id_hash = src_obj_id.__hash__()
                logging.debug("got src_obj_id hash: {}".format(src_obj_id_hash))
                if src_obj_id_hash in srcid_desobj_map:
                    des_obj = srcid_desobj_map[src_obj_id_hash]
                    logging.debug("creating hardlink to {}".format(des_obj.id.id))
                    gdes[title] = des_obj
                else:
                    # TBD - in hdf5 1.10 it seems that two references to the same object
                    # can return different id's.  This will cause HDF5 files with
                    # multilinks to not load correctly
                    msg = "could not find map item to src id: {}".format(src_obj_id_hash)
                    logging.warn(msg)
                    if ctx["verbose"]:
                        print("WARNING: " + msg)
        elif link_classname == "SoftLink":
            msg = "creating SoftLink({}) with title: {}".format(lnk.path, title)
            if ctx["verbose"]:
                print(msg)
            logging.info(msg)
            if is_h5py(gdes):
                soft_link = h5py.SoftLink(lnk.path)
            else:
                soft_link = h5pyd.SoftLink(lnk.path)
            gdes[title] = soft_link
        elif link_classname == "ExternalLink":
            msg = "creating ExternalLink({}, {}) with title: {}".format(lnk.filename, lnk.path, title)
            if ctx["verbose"]:
                print(msg)
            logging.info(msg)
            if is_h5py(gdes):
                ext_link = h5py.ExternalLink(lnk.filename, lnk.path)
            else:
                ext_link = h5pyd.ExternalLink(lnk.filename, lnk.path)
            gdes[title] = ext_link
        else:
            msg = "Unexpected link type: {}".format(lnk.__class__.__name__)
            logging.warning(msg)
            if ctx["verbose"]:
                print(msg)
    logging.info("flush fout")
示例#2
0
def create_links(gsrc, gdes, ctx):
    # add soft and external links
    srcid_desobj_map = ctx["srcid_desobj_map"]
    if ctx["verbose"]:
        print("create_links: {}".format(gsrc.name))
    for title in gsrc:
        if ctx["verbose"]:
            print("got link: {}".format(title))
        lnk = gsrc.get(title, getlink=True)
        link_classname = lnk.__class__.__name__
        if link_classname == "HardLink":
            logging.debug("Got hardlink: {} gsrc: {} gdes: {}".format(title, gsrc, gdes))
            if title not in gdes:
                msg = "creating multilink {} with title: {}".format(gdes, title)
                if ctx["verbose"]:
                    print(msg)
                logging.info(msg)
                src_obj_id = gsrc[title].id
                src_obj_id_hash = src_obj_id.__hash__()
                logging.debug("got src_obj_id hash: {}".format(src_obj_id_hash))
                if src_obj_id_hash in srcid_desobj_map:
                    des_obj = srcid_desobj_map[src_obj_id_hash]
                    logging.debug("creating hardlink to {}".format(des_obj.id.id))
                    gdes[title] = des_obj
                else:
                    msg = "could not find map item to src id: {}".format(src_obj_id_hash)
                    logging.warn(msg)
                    if ctx["verbose"]:
                        print("WARNING: " + msg)
        elif link_classname == "SoftLink":
            msg = "creating SoftLink({}) with title: {}".format(lnk.path, title)
            if ctx["verbose"]:
                print(msg)
            logging.info(msg)
            if is_h5py(gdes):
                soft_link = h5py.SoftLink(lnk.path)
            else:
                soft_link = h5pyd.SoftLink(lnk.path)
            gdes[title] = soft_link
        elif link_classname == "ExternalLink":
            msg = "creating ExternalLink({}, {}) with title: {}".format(lnk.filename, lnk.path, title)
            if ctx["verbose"]:
                print(msg)
            logging.info(msg)
            if is_h5py(gdes):
                ext_link = h5py.ExternalLink(lnk.filename, lnk.path)
            else:
                ext_link = h5pyd.ExternalLink(lnk.filename, lnk.path)
            gdes[title] = ext_link
        else:
            msg = "Unexpected link type: {}".format(lnk.__class__.__name__)
            logging.warning(msg)
            if ctx["verbose"]:
                print(msg)
示例#3
0
def create_group(fd, gobj, verbose=False):
    msg = "creating group {}".format(gobj.name)
    logging.info(msg)
    if verbose:
        print(msg)
    grp = fd.create_group(gobj.name)

    # create attributes
    for ga in gobj.attrs:
        copy_attribute(grp, ga, gobj)

    # create any soft/external links
    for title in gobj:
        lnk = gobj.get(title, getlink=True)
        link_classname = lnk.__class__.__name__
        if link_classname == "HardLink":
            logging.debug("Got hardlink: {}".format(title))
            # TBD: handle the case where multiple hardlinks point to same object
        elif link_classname == "SoftLink":
            msg = "creating SoftLink({}) with title: {}".format(
                lnk.path, title)
            if verbose:
                print(msg)
            logging.info(msg)
            if is_h5py(fd):
                soft_link = h5py.SoftLink(lnk.path)
            else:
                soft_link = h5pyd.SoftLink(lnk.path)
            grp[title] = soft_link
        elif link_classname == "ExternalLink":
            msg = "creating ExternalLink({}, {}) with title: {}".format(
                lnk.filename, lnk.path, title)
            if verbose:
                print(msg)
            logging.info(msg)
            if is_h5py(fd):
                ext_link = h5py.ExternalLink(lnk.filename, lnk.path)
            else:
                ext_link = h5pyd.ExternalLink(lnk.filename, lnk.path)
            grp[title] = ext_link
        else:
            msg = "Unexpected link type: {}".format(lnk.__class__.__name__)
            logging.warning(msg)
            if verbose:
                print(msg)
示例#4
0
def create_links(gsrc, gdes, ctx):
    # add soft and external links
    if ctx["verbose"]:
        print("create_links: {}".format(gsrc.name))
    for title in gsrc:
        if ctx["verbose"]:
            print("got link: {}".format(title))
        lnk = gsrc.get(title, getlink=True)
        link_classname = lnk.__class__.__name__
        if link_classname == "HardLink":
            logging.debug("Got hardlink: {}".format(title))
            # TBD: handle the case where multiple hardlinks point to same object
        elif link_classname == "SoftLink":
            msg = "creating SoftLink({}) with title: {}".format(
                lnk.path, title)
            if ctx["verbose"]:
                print(msg)
            logging.info(msg)
            if is_h5py(gdes):
                soft_link = h5py.SoftLink(lnk.path)
            else:
                soft_link = h5pyd.SoftLink(lnk.path)
            gdes[title] = soft_link
        elif link_classname == "ExternalLink":
            msg = "creating ExternalLink({}, {}) with title: {}".format(
                lnk.filename, lnk.path, title)
            if ctx["verbose"]:
                print(msg)
            logging.info(msg)
            if is_h5py(gdes):
                ext_link = h5py.ExternalLink(lnk.filename, lnk.path)
            else:
                ext_link = h5pyd.ExternalLink(lnk.filename, lnk.path)
            gdes[title] = ext_link
        else:
            msg = "Unexpected link type: {}".format(lnk.__class__.__name__)
            logging.warning(msg)
            if ctx["verbose"]:
                print(msg)
示例#5
0
 def test_create(self):
     filename = "new_group." + self.base_domain
     f = h5py.File(filename, 'w')
     self.assertTrue(f.id.id is not None)
     self.assertTrue('/' in f)      
     r = f['/']
     
     self.assertEqual(len(r), 0)
     self.assertTrue(isinstance(r, h5py.Group))
     self.assertTrue(r.name, '/')
     self.assertEqual(len(r.attrs.keys()), 0)
     
     self.assertFalse('g1' in r)
      
     r.create_group('g1')
     self.assertTrue('g1' in r)
     r.create_group('g2')
     self.assertEqual(len(r), 2)
     keys = []
     # iterate through keys
     for k in r:
         keys.append(k)
         
     self.assertEqual(len(keys), 2)
     self.assertTrue('g1' in keys)
     self.assertTrue('g2' in keys)
     
     self.assertTrue('g1' in r)
     self.assertTrue('/g1' in r)
     g1 = r.get('/g1')
     self.assertTrue(g1.id.id)
     self.assertEqual(g1.name, '/g1')
     
     g1_class = r.get('g1', getclass=True)
     self.assertEqual(g1_class, h5py.Group)
     
     # try creating a group that already exists
     try:
         r.create_group('g1')
         self.assertTrue(False)
     except ValueError:
         pass # expected
         
     r.create_group('g3')
     self.assertEqual(len(r), 3)
     del r['g3']
     self.assertEqual(len(r), 2)
     
     r.require_group('g4')
     self.assertEqual(len(r), 3)
     r.require_group('g2') 
     self.assertEqual(len(r), 3)
     
     g1_1 = r.create_group("g1/g1.1")
     self.assertEqual(len(r), 3)
     self.assertEqual(len(g1), 1)
     self.assertEqual(len(g1_1), 0)    
     
     # create a hardlink
     r['g1.1'] = g1_1
     self.assertEqual(len(r), 4)
     
     # create a softlink
     r['mysoftlink'] = h5py.SoftLink('/g1/g1.1')
     self.assertEqual(len(r), 5)
     #print r.get_link_json("/mysoftlink")
     slink = r['mysoftlink']
     self.assertEqual(slink.id, g1_1.id)
     
     
     # create a external hardlink
     r['myexternallink'] = h5py.ExternalLink("somefile", "somepath")
     
     # test getclass
     g1_class = r.get('g1', getclass=True)
     self.assertEqual(g1_class, h5py.Group)
     linkee_class = r.get('mysoftlink', getclass=True)
     self.assertEqual(linkee_class, h5py.Group)
     link_class = r.get('mysoftlink', getclass=True, getlink=True)
     self.assertEqual(link_class, h5py.SoftLink)
     softlink = r.get('mysoftlink', getlink=True)
     self.assertEqual(softlink.path, '/g1/g1.1')
     
     try:
         linkee_class = r.get('myexternallink', getclass=True)
         self.assertTrue(False)
     except IOError:
         pass # expected - not implemented
     
     link_class = r.get('myexternallink', getclass=True, getlink=True)
     self.assertEqual(link_class, h5py.ExternalLink)
     external_link = r.get('myexternallink', getlink=True)
     self.assertEqual(external_link.path, 'somepath')
     
     del r['mysoftlink']
     self.assertEqual(len(r), 5)
     
     del r['myexternallink']
     self.assertEqual(len(r), 4)
     
     f.close()
     self.assertEqual(f.id.id, 0)