Example #1
0
 def add_name(nm, nid_, name_):
     'Adds a name. If nid == -1 a new nid will be assigned. Returns nid'
     logdbg('Adding nid=' + str(nid_) + ' name=' + name_)
     nid = nid_
     name = name_.strip()
     if name == '':
         logerr('Cannot add an empty name!')
     if nid < 0:
         # Generate new nid if not specified
         nid = nm.next_nid
     else:
         #If NID already exists and has an entry,
         #do not increment, and do nothing
         #This is essentially like doing a rename
         #it doesn't actually change anything
         if (nid < len(nm.nid2_nx) and nm.nid2_nx[nid] > 0):
             logwarn('NID ' + str(nid) + 'already exists')
             nx = nm.nid2_nx[nid]
             return -1
     if name in nm.name2_nx.keys():
         conflict_nx = nm.name2_nx[name]
         conflict_nid = nm.nx2_nid[conflict_nx]
         conflict_msg = 'Name %s already exists in database!\n' % name + \
                        'NID=%d; NEXT_NX=%d\n' % (nid, nm.next_nx) + \
                        'CONFLICT_NID=%d\n' % conflict_nid
         if nid_ == -1:
             logwarn(conflict_msg)
         elif nid_ > -1:
             logerr(conflict_msg)
         nid = conflict_nid
         nx = conflict_nx
         return nid
     #Manage Memory
     nx = nm.next_nx
     logdbg(' * nx = ' + str(nx))
     if nx >= len(nm.nx2_nid):
         nm.name_alloc((len(nm.nx2_nid) + 1) * 2 + 1)
     #Add to flat table
     nm.nx2_nid[nx] = nid
     nm.nx2_name[nx] = name
     if len(name) > nm.max_namelen:
         nm.max_namelen = len(name)
         nm.max_name = name
     #X Reverse indexing
     if len(nm.nid2_nx) <= nid:
         idAlloc = (nid - len(nm.nid2_nx)) + 1
         nm.nid2_nx = append(nm.nid2_nx, zeros(idAlloc, dtype=uint32))
     logdbg(' * nid2_nx[' + str(nid) + ']=' + str(nx))
     nm.nid2_nx[nid] = nx
     nm.name2_nx[name] = nx
     #Increment
     nm.next_nx = nm.next_nx + 1
     nm.next_nid = max(nm.next_nid + 1, nid + 1)
     nm.max_nx = max(nm.max_nx, nx)
     nm.max_nid = max(nm.max_nid, nid)
     nm.num_n = nm.num_n + 1
     logdbg('Added nid=' + str(nid) + ' name=' + name)
     return nid
Example #2
0
 def  add_name(nm, nid_, name_):
     'Adds a name. If nid == -1 a new nid will be assigned. Returns nid'
     logdbg('Adding nid='+str(nid_)+' name='+name_)
     nid = nid_
     name = name_.strip()
     if name == '':
         logerr('Cannot add an empty name!')
     if nid < 0:
         # Generate new nid if not specified
         nid = nm.next_nid
     else:
         #If NID already exists and has an entry,
         #do not increment, and do nothing
         #This is essentially like doing a rename
         #it doesn't actually change anything
         if (nid < len(nm.nid2_nx) and nm.nid2_nx[nid] > 0):
             logwarn('NID '+str(nid)+'already exists')
             nx = nm.nid2_nx[nid]
             return -1
     if name in nm.name2_nx.keys():
         conflict_nx = nm.name2_nx[name]
         conflict_nid = nm.nx2_nid[conflict_nx]
         conflict_msg = 'Name %s already exists in database!\n' % name + \
                        'NID=%d; NEXT_NX=%d\n' % (nid, nm.next_nx) + \
                        'CONFLICT_NID=%d\n' % conflict_nid 
         if nid_ == -1:
             logwarn(conflict_msg)
         elif nid_ > -1:
             logerr(conflict_msg)
         nid = conflict_nid
         nx  = conflict_nx
         return nid
     #Manage Memory
     nx = nm.next_nx
     logdbg(' * nx = '+str(nx))
     if nx >= len(nm.nx2_nid):
         nm.name_alloc((len(nm.nx2_nid)+1)*2+1)
     #Add to flat table
     nm.nx2_nid[nx]   = nid
     nm.nx2_name[nx]  = name
     if len(name) > nm.max_namelen:
         nm.max_namelen = len(name)
         nm.max_name = name
     #X Reverse indexing
     if len(nm.nid2_nx) <= nid:
         idAlloc = (nid - len(nm.nid2_nx)) + 1
         nm.nid2_nx = append(nm.nid2_nx, zeros(idAlloc,dtype=uint32)) 
     logdbg( ' * nid2_nx['+str(nid)+']='+str(nx) )
     nm.nid2_nx[nid]   = nx
     nm.name2_nx[name] = nx
     #Increment
     nm.next_nx  = nm.next_nx + 1
     nm.next_nid = max(nm.next_nid + 1, nid + 1)
     nm.max_nx   = max(nm.max_nx , nx)
     nm.max_nid  = max(nm.max_nid, nid)
     nm.num_n    = nm.num_n + 1
     logdbg('Added nid='+str(nid)+' name='+name)
     return nid
Example #3
0
 def add_img(gm, gid=None, gname=None, aif=False, src_img=''):
     logdbg('Adding Image: gid=%r, gname=%r, aif=%r, src_img=%r' % (gid, gname, aif, src_img))
     if src_img != '': #This is an new image
         (dir_part, nameext_part)  = os.path.split(src_img)
         (name_part, ext_part) = os.path.splitext(nameext_part)
         if not ext_part.lower() in gm.valid_img_extensions:
             logerr('Invalid Image: %s' % src_img)
         if gname is None:
           gname   = name_part + ext_part
         db_img  = os.path.join(gm.hs.iom.get_img_dpath(), gname)
         if os.path.abspath(src_img) == os.path.abspath(db_img):
             logmsg('Readding existing dbimg:'+src_img)
         else:
             logmsg('Copying '+src_img+' to '+db_img)
             copyfile(src_img, db_img)
     db_img  = os.path.join(gm.hs.iom.get_img_dpath(), gname)
     if not os.path.exists(db_img):
         # Try to add an extension if it wasn't given
         ext_fallback_list = ['.jpg','.jpeg','.JPG','.JPEG','.png','.tif']
         fb_sucess_bit = False
         (db_img_noext, old_ext)  = os.path.splitext(db_img)
         (gname_noext, old_ext2) = os.path.splitext(gname)
         for ext_fb in ext_fallback_list:
             db_img_fb = db_img_noext + ext_fb
             if os.path.exists(db_img_fb):
                 db_img = db_img_fb
                 gname = gname_noext+ext_fb
                 fb_sucess_bit = True; break
         if not fb_sucess_bit:
             logwarn('Trying to add a nonexistant image: '+db_img)
             return
     if gname in gm.gname2_gid.keys():
         logdbg('Trying to add a GNAME that is already managed: '+gname)
         return
     if gid is None or gid < 1:
         gid = gm.next_gid
     else:
         if (gid < len(gm.gid2_gx)-1 and gm.gid2_gx[gid] > 0):
             logdbg('Trying to add a GID that is already managed: '+str(gid))
             return gid
     #Check key values before memory managment
     #Manage Memory
     gx = gm.next_gx
     if gx >= len(gm.gx2_gid):
         gm.img_alloc((len(gm.gx2_gid)+1)*2+1)
     #Flat indexing
     gm.gx2_gid[gx]   = gid
     gm.gx2_gname[gx] = gname
     if len(gname) > gm.max_gnamelen:
         gm.max_gnamelen = len(gname)
         gm.max_gname = gname
     
     gm.gx2_aif_bit[gx]   = aif
     #X Reverse indexing
     if len(gm.gid2_gx) <= gid:
         gid_extend = (gid - len(gm.gid2_gx)) + 1
         gm.gid2_gx = append(gm.gid2_gx, zeros(gid_extend,dtype=uint32))
     gm.gid2_gx[gid] = gx
     gm.gname2_gid[gname] = gid
     #Increment
     gm.next_gx = gm.next_gx + 1
     gm.next_gid = max(gm.next_gid+1, gid+1)
     gm.num_g = gm.num_g + 1
     gm.max_gx  = max(gm.max_gx,  gx)
     gm.max_gid = max(gm.max_gid, gx)
     return gid
Example #4
0
    def add_img(gm, gid=None, gname=None, aif=False, src_img=''):
        logdbg('Adding Image: gid=%r, gname=%r, aif=%r, src_img=%r' %
               (gid, gname, aif, src_img))
        if src_img != '':  #This is an new image
            (dir_part, nameext_part) = os.path.split(src_img)
            (name_part, ext_part) = os.path.splitext(nameext_part)
            if not ext_part.lower() in gm.valid_img_extensions:
                logerr('Invalid Image: %s' % src_img)
            if gname is None:
                gname = name_part + ext_part
            db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname)
            if os.path.abspath(src_img) == os.path.abspath(db_img):
                logmsg('Readding existing dbimg:' + src_img)
            else:
                logmsg('Copying ' + src_img + ' to ' + db_img)
                copyfile(src_img, db_img)
        db_img = os.path.join(gm.hs.iom.get_img_dpath(), gname)
        if not os.path.exists(db_img):
            # Try to add an extension if it wasn't given
            ext_fallback_list = [
                '.jpg', '.jpeg', '.JPG', '.JPEG', '.png', '.tif'
            ]
            fb_sucess_bit = False
            (db_img_noext, old_ext) = os.path.splitext(db_img)
            (gname_noext, old_ext2) = os.path.splitext(gname)
            for ext_fb in ext_fallback_list:
                db_img_fb = db_img_noext + ext_fb
                if os.path.exists(db_img_fb):
                    db_img = db_img_fb
                    gname = gname_noext + ext_fb
                    fb_sucess_bit = True
                    break
            if not fb_sucess_bit:
                logwarn('Trying to add a nonexistant image: ' + db_img)
                return
        if gname in gm.gname2_gid.keys():
            logdbg('Trying to add a GNAME that is already managed: ' + gname)
            return
        if gid is None or gid < 1:
            gid = gm.next_gid
        else:
            if (gid < len(gm.gid2_gx) - 1 and gm.gid2_gx[gid] > 0):
                logdbg('Trying to add a GID that is already managed: ' +
                       str(gid))
                return gid
        #Check key values before memory managment
        #Manage Memory
        gx = gm.next_gx
        if gx >= len(gm.gx2_gid):
            gm.img_alloc((len(gm.gx2_gid) + 1) * 2 + 1)
        #Flat indexing
        gm.gx2_gid[gx] = gid
        gm.gx2_gname[gx] = gname
        if len(gname) > gm.max_gnamelen:
            gm.max_gnamelen = len(gname)
            gm.max_gname = gname

        gm.gx2_aif_bit[gx] = aif
        #X Reverse indexing
        if len(gm.gid2_gx) <= gid:
            gid_extend = (gid - len(gm.gid2_gx)) + 1
            gm.gid2_gx = append(gm.gid2_gx, zeros(gid_extend, dtype=uint32))
        gm.gid2_gx[gid] = gx
        gm.gname2_gid[gname] = gid
        #Increment
        gm.next_gx = gm.next_gx + 1
        gm.next_gid = max(gm.next_gid + 1, gid + 1)
        gm.num_g = gm.num_g + 1
        gm.max_gx = max(gm.max_gx, gx)
        gm.max_gid = max(gm.max_gid, gx)
        return gid