Beispiel #1
0
 def setup_table (self, name, data, key=None):
     """Setup a metakit view (table) for generic table description (see superclass rdatabase)."""
     debug('setup_table(name=%(name)s,data=%(data)s,key=%(key)s)'%locals(),1)
     getstring = name + "["
     # We want to make our "key" the first item in the database
     if key:
         key_index = [x[0] for x in data].index(key)
         data = [data[key_index]] + data[0:key_index] + data[key_index+1:]
     for col,typ,flags in data:
         if 'AUTOINCREMENT' in flags:
             debug('Setup autoincrement for %s'%name,3)
             row = self.fetch_one(self.increment_vw,**{'view':name,
                                                       'field':col}
                                  )
             debug('Looked up autoincrement row',3)
             if not row:
                 debug('Add new autoincrement row',3)
                 self.increment_vw.append(view=name,field=col,n=1)
         debug('Building metakit getstring %s'%getstring,3)
         getstring += "%s:%s,"%(col,self.type_to_metakit_type(typ))
     if name=='recipe':
         # Hack to allow sorting to work...
         getstring = getstring+'categoryname:S,'
     getstring = getstring[0:-1] + "]"
     debug('Metakit: getting view: %s'%getstring,5)
     vw = self.db.getas(getstring)
     debug('Got view!',5)
     if key:
         if data[key_index][1]=='int': #if typ of key is int
             debug('Make ordered',3)
             vw = vw.ordered()
             debug('Made ordered',3)
         else:
             #debug('Make hash',3)
             rhsh = self.db.getas("__%s_hash__[_H:I,_R:I]"%name)
             vw = vw.hash(rhsh,1)
             #debug('Made hash!',3)
     # Make sure our increment fields are right...
     self.vw_to_name[vw]=name
     debug('Investigate increment rows',3)
     increment_rows = self.increment_vw.select(view=name)
     if increment_rows:
         #for field,row in self.increment_dict[name].items():
         for dbrow in self.increment_vw.select(view=name):                
             field = dbrow.field
             debug("look at row for field:%s"%field,3)
             svw=vw.sort(getattr(vw,field))
             tot = len(svw)
             if tot>1:
                 if tot>getattr(svw[-1],field):
                     print """WTF: increment dicts are foobared. If you see this message, please
                     submit a bug report with the terminal output included.
                     """
                     metakit.dump(svw)
                 else:
                     # Setting increment row's n to the highest number in our DB
                     dbrow.n = getattr(svw[-1],field)
     debug('setup_table done!',2)
     return vw
Beispiel #2
0
def show(vw):
  for v in [vw[0], vw[0].sub[0], vw[0].sub[0].sub[0]]:
    print v.s, len(v.sub)
  print vw[0].sub[0].sub[0].sub.structure()
  assert len(vw[0].sub[0].sub[0].sub) == 0
  try:
    metakit.dump(vw[0].sub[0].sub[0].sub[0].sub)
  except IndexError:
    pass
Beispiel #3
0
def show(vw):
    for v in [vw[0], vw[0].sub[0], vw[0].sub[0].sub[0]]:
        print v.s, len(v.sub)
    print vw[0].sub[0].sub[0].sub.structure()
    assert len(vw[0].sub[0].sub[0].sub) == 0
    try:
        metakit.dump(vw[0].sub[0].sub[0].sub[0].sub)
    except IndexError:
        pass
Beispiel #4
0
def checklen(**args):
    alen = len(arr)
    vlen = len(v)
    if alen != vlen:
        fail('append', args, 'view length mismatch', actual=vlen, expected=alen)
        try:
            print 'ARRAY CONTENTS:'
            for arow in arr: print arow
            metakit.dump(v, 'VIEW CONTENTS:')
        except: pass
        raise TestFailed('unexpected number of rows in view, aborting; run in verbose mode for details')
Beispiel #5
0
 def setup_table(self, name, data, key=None):
     """Setup a metakit view (table) for generic table description (see superclass rdatabase)."""
     debug(
         'setup_table(name=%(name)s,data=%(data)s,key=%(key)s)' % locals(),
         1)
     getstring = name + "["
     # We want to make our "key" the first item in the database
     if key:
         key_index = [x[0] for x in data].index(key)
         data = [data[key_index]] + data[0:key_index] + data[key_index + 1:]
     for col, typ, flags in data:
         if 'AUTOINCREMENT' in flags:
             row = self.fetch_one(self.increment_vw, **{
                 'view': name,
                 'field': col
             })
             if not row:
                 self.increment_vw.append(view=name, field=col, n=1)
                 row = self.increment_vw[-1]
             if not self.increment_dict.has_key(name):
                 self.increment_dict[name] = {}
             self.increment_dict[name][col] = row
         getstring += "%s:%s," % (col, self.type_to_metakit_type(typ))
     if name == 'recipe':
         getstring = getstring + 'categoryname:S,'
     getstring = getstring[0:-1] + "]"
     debug('Metakit: getting view: %s' % getstring, 5)
     vw = self.db.getas(getstring)
     debug('Got metakit database', 5)
     if key:
         rhsh = self.db.getas("__%s_hash__[_H:I,_R:I]" % name)
         vw = vw.hash(rhsh, 1)
     # Make sure our increment fields are right...
     if self.increment_dict.has_key(name):
         self.vw_to_name[vw] = name
         for field, row in self.increment_dict[name].items():
             try:
                 svw = vw.sort(vw.id)
                 if len(svw) > svw[-1].id:
                     print """WTF: increment dicts are foobared. If you see this message, please
                     submit a bug report with the terminal output included.
                     """
                     metakit.dump(svw)
                 self.increment_dict[name][field].n = svw[-1].id
             except IndexError:
                 pass
     return vw
Beispiel #6
0
 def setup_table (self, name, data, key=None):
     """Setup a metakit view (table) for generic table description (see superclass rdatabase)."""
     debug('setup_table(name=%(name)s,data=%(data)s,key=%(key)s)'%locals(),1)
     getstring = name + "["
     # We want to make our "key" the first item in the database
     if key:
         key_index = [x[0] for x in data].index(key)
         data = [data[key_index]] + data[0:key_index] + data[key_index+1:]
     for col,typ,flags in data:
         if 'AUTOINCREMENT' in flags:
             row = self.fetch_one(self.increment_vw,**{'view':name,
                                                  'field':col}
                               )
             if not row:
                 self.increment_vw.append(view=name,field=col,n=1)
                 row = self.increment_vw[-1]
             if not self.increment_dict.has_key(name):
                 self.increment_dict[name]={}
             self.increment_dict[name][col]=row
         getstring += "%s:%s,"%(col,self.type_to_metakit_type(typ))
     if name=='recipe':
         getstring = getstring+'categoryname:S,'
     getstring = getstring[0:-1] + "]"
     debug('Metakit: getting view: %s'%getstring,5)
     vw = self.db.getas(getstring)
     debug('Got metakit database',5)
     if key:
         rhsh = self.db.getas("__%s_hash__[_H:I,_R:I]"%name)
         vw = vw.hash(rhsh,1)
     # Make sure our increment fields are right...
     if self.increment_dict.has_key(name):
         self.vw_to_name[vw]=name
         for field,row in self.increment_dict[name].items():
             try:
                 svw=vw.sort(vw.id)
                 if len(svw)>svw[-1].id:
                     print """WTF: increment dicts are foobared. If you see this message, please
                     submit a bug report with the terminal output included.
                     """
                     metakit.dump(svw)
                 self.increment_dict[name][field].n = svw[-1].id
             except IndexError:
                 pass        
     return vw
Beispiel #7
0
def checklen(**args):
    alen = len(arr)
    vlen = len(v)
    if alen != vlen:
        fail('append',
             args,
             'view length mismatch',
             actual=vlen,
             expected=alen)
        try:
            print 'ARRAY CONTENTS:'
            for arow in arr:
                print arow
            metakit.dump(v, 'VIEW CONTENTS:')
        except:
            pass
        raise TestFailed(
            'unexpected number of rows in view, aborting; run in verbose mode for details'
        )
Beispiel #8
0
 def increment_field (self, table, field):
     if type(table)!=str:
         try:
             table = self.vw_to_name[table]
         except:
             try:
                 table = self.vw_to_name[table.__view__]
             except:
                 print "I don't know about the table ",table,'(',field,')'
                 raise
     row = self.fetch_one(self.increment_vw,
                          **{'view':table,
                             'field':field})
     if not row:
         print 'Here are the guts of increment_vw:'
         metakit.dump(self.increment_vw)
         raise Exception("Very odd: we find no row for table: %s, field: %s" % (table, field))
     row.n += 1
     return row.n
Beispiel #9
0
dd = [{
    'a': 'one',
    'b': 'un',
    'c': 1
}, {
    'a': 'two',
    'b': 'deux',
    'c': 2
}, {
    'a': 'three',
    'b': 'trois',
    'c': 3
}]

dt = [('one', 'un', 1), ('two', 'deux', 2), ('three', 'trois', 3)]

pl = [
    metakit.property('S', 'a'),
    metakit.property('S', 'b'),
    metakit.property('I', 'c')
]

vc = metakit.wrap(dc, pl)
vd = metakit.wrap(dd, pl)
vt = metakit.wrap(dt, pl, 1)

metakit.dump(vc, 'class objects:')
metakit.dump(vd, 'dictionary elements:')
metakit.dump(vt, 'tuples (by position):')
Beispiel #10
0
 def dump_view(self):
     metakit.dump(self.v, 'VIEW CONTENTS:')
Beispiel #11
0
# Small demo of v1.pair(v2)
#
# Expected output:
#  s    i
#  -----  -
#  zero 0
#  one  1
#  two  2
#  three  3
#  four 4
#  five 5
#  -----  -
#  Total: 6 rows

import metakit

db = metakit.storage()
v1 = db.getas("one[s:S]")
v2 = db.getas("two[i:I]")

for v in ['zero','one','two','three','four','five']:
  v1.append(s=v)
for v in range(6):
  v2.append(i=v)

metakit.dump(v1.pair(v2))
Beispiel #12
0
 def dump_view(self):
     metakit.dump(self.v, 'VIEW CONTENTS:')
Beispiel #13
0
# Small demo of v1.remapwith(v2)
#
# Expected output:
#  s    
#  -----
#  one  
#  three
#  five 
#  two  
#  four 
#  one  
#  three
#  five 
#  -----
#  Total: 8 rows

import metakit

db = metakit.storage()
v1 = db.getas("counts[s:S]")
v2 = db.getas("map[i:I]")

for v in ['zero','one','two','three','four','five']:
  v1.append(s=v)
for v in [1,3,5,2,4,1,3,5]:
  v2.append(i=v)

metakit.dump(v1.remapwith(v2))
Beispiel #14
0
import metakit

class C:
    def __init__(self, a, b, c):
    	self.a, self.b, self.c = a, b, c

dc = [	C ('one', 'un', 1),
	C ('two', 'deux', 2),
	C ('three', 'trois', 3)  ]

dd = [	{'a':'one', 'b':'un', 'c':1}, 
	{'a':'two', 'b':'deux', 'c':2}, 
	{'a':'three', 'b':'trois', 'c':3}  ] 

dt = [	('one', 'un', 1), 
	('two', 'deux', 2), 
	('three', 'trois', 3)  ] 

pl = [	metakit.property('S','a'),
	metakit.property('S','b'),
	metakit.property('I','c')  ]

vc = metakit.wrap(dc, pl)
vd = metakit.wrap(dd, pl)
vt = metakit.wrap(dt, pl, 1)

metakit.dump(vc, 'class objects:')
metakit.dump(vd, 'dictionary elements:')
metakit.dump(vt, 'tuples (by position):')
Beispiel #15
0
insert(intf=0, longf=278)
insert(intf=0, longf=-213)
insert(intf=0, longf=95.0)
insert(intf=0, longf=27.3)
reject(ValueError, intf=0, longf=float(2 * MAXLONGLONG))
reject(ValueError, intf=0, longf=float(2 * MINLONGLONG))
reject(TypeError, intf=0, longf=str(MAXLONGLONG))
reject(TypeError, intf=0, longf=str(MINLONGLONG))
reject(TypeError, intf=0, longf='-21.39')
reject(TypeError, intf=0, longf=str(MAXULONGLONG))

# XXX should repeat with assignment instead of appending
# XXX test v.select()

if verbose:
    metakit.dump(v, 'VIEW CONTENTS:')

# compare view with array
for arow, vrow in zip(arr, v):
    failed = False
    for f in arow.keys():
        try:
    	    vf = getattr(vrow, f)
            af = arow[f]
            if af == vf:
                continue
            # Perform the same implicit coercion as Mk4py should
            if type(af) != type(vf):
                try:
                    af = type(vf)(af)
                    if af == vf:
Beispiel #16
0
insert(intf=0, longf=278)
insert(intf=0, longf=-213)
insert(intf=0, longf=95.0)
insert(intf=0, longf=27.3)
reject(ValueError, intf=0, longf=float(2 * MAXLONGLONG))
reject(ValueError, intf=0, longf=float(2 * MINLONGLONG))
reject(TypeError, intf=0, longf=str(MAXLONGLONG))
reject(TypeError, intf=0, longf=str(MINLONGLONG))
reject(TypeError, intf=0, longf='-21.39')
reject(TypeError, intf=0, longf=str(MAXULONGLONG))

# XXX should repeat with assignment instead of appending
# XXX test v.select()

if verbose:
    metakit.dump(v, 'VIEW CONTENTS:')

# compare view with array
for arow, vrow in zip(arr, v):
    failed = False
    for f in arow.keys():
        try:
            vf = getattr(vrow, f)
            af = arow[f]
            if af == vf:
                continue
        # Perform the same implicit coercion as Mk4py should
            if type(af) != type(vf):
                try:
                    af = type(vf)(af)
                    if af == vf: