[Assorted-commits] SF.net SVN: assorted:[896] simple-zdb/trunk/src/zdb.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-07-18 08:34:26
|
Revision: 896 http://assorted.svn.sourceforge.net/assorted/?rev=896&view=rev Author: yangzhang Date: 2008-07-18 08:34:35 +0000 (Fri, 18 Jul 2008) Log Message: ----------- changed load to importdb() (wipes out what was there before!); prevent redundant saves by seeing if the db has changed since prev; fixed dump bug omitting concept; tweak exception handling Modified Paths: -------------- simple-zdb/trunk/src/zdb.py Modified: simple-zdb/trunk/src/zdb.py =================================================================== --- simple-zdb/trunk/src/zdb.py 2008-07-18 01:36:55 UTC (rev 895) +++ simple-zdb/trunk/src/zdb.py 2008-07-18 08:34:35 UTC (rev 896) @@ -2,8 +2,9 @@ from __future__ import with_statement -from cPickle import load, dump +from cPickle import load, dump, dumps from cStringIO import StringIO +from commons.log import info, config_logging from commons.exceps import handle_exceptions from commons.files import soft_makedirs from commons.startup import run_main @@ -14,7 +15,10 @@ from sys import stdin, stdout, stderr from time import time from unittest import TestCase, main as test_main +from functools import partial +info = partial( info, '' ) + dbdir = path( '~/.zdb/db-' ).expanduser() # @@ -24,10 +28,18 @@ class zdb_exception( Exception ): pass +def importdb( p ): + global db + with file( path( p ).expanduser() ) as f: + createdb() + for o in parse( f.read() ): db.addobj( o ) + class zdb( object ): 'The actual DB.' def __init__( self ): self.name2obj = {} + def __eq__( self, other ): + return type( self ) == type( other ) and self.name2obj == other.name2obj # User-facing functions (these are the console commands) def add( self, name, parents, props ): self.addobj( obj( name, parents, props ) ) @@ -37,6 +49,7 @@ del self.name2obj[ name ] def dump( self, path ): with file( path, 'w' ) as f: + self.name2obj[ 'concept' ].dump(f) for parent in sorted( db.name2obj.iterkeys() ): first = True for name, o in sorted( db.name2obj.iteritems() ): @@ -46,24 +59,7 @@ print >> f, '===', parent, '===' print >> f first = False - print >> f, o.name - for field, values in sorted( o.props.iteritems() ): - for value in values: - if '\n' in value: - print >> f, '%s::' % field - print >> f, value - print >> f, '::' - else: - print >> f, '%s: %s' % ( field, value ) - print >> f - def load( self, path ): - names = set() - with file( path ) as f: - for o in parse( f.read() ): - self.updateobj( o ) - if o.name in names: - raise zdb_exception( 'duplicate names detected in load input' ) - names.add( o ) + o.dump(f) # API functions def addobj( self, o ): if o.name in db.name2obj: raise zdb_exception('already exists: ' + o.name) @@ -75,7 +71,7 @@ try: for name, o in self.name2obj.iteritems(): assert name == o.name, 'name mismatch' - assert len( o.parents ) == 1 or name == 'concept' and o.parents == [], \ + assert name == 'concept' and o.parents == [] or len( o.parents ) == 1, \ 'all objs beside concept must have single parent (for now)' for p in o.parents: assert p in self.name2obj, 'parent doesnt exist: %s' % p @@ -91,7 +87,7 @@ def __init__( self, name, parents, props ): global db self.name = name - assert parents != None and parents != [] + assert parents != None and parents != [] or name == 'concept' self.parents = parents self.props = {} for p in props: self.props[p] = [] @@ -100,6 +96,17 @@ self.name == other.name and self.props == other.props and self.parents == other.parents ) + def dump( self, f ): + print >> f, self.name + for field, values in sorted( self.props.iteritems() ): + for value in values: + if '\n' in value: + print >> f, '%s::' % field + print >> f, value + print >> f, '::' + else: + print >> f, '%s: %s' % ( field, value ) + print >> f def addprop( self, field, value ): try: d = self.props[ field ] except: d = self.props[ field ] = [] @@ -114,34 +121,44 @@ def loaddb(): global db - dbs = dbfiles() - if dbs == []: + paths = dbfiles() + if paths == []: raise zdb_exception('no DB to load') else: - with file(dbs[-1]) as f: tstamp, db = load(f) + with file(paths[-1]) as f: tstamp, db = load(f) def savedb(): global db - dbs = dbfiles() - newdb = dbdir + str( dbnum(dbs[-1]) + 1 if len( dbs ) > 0 else 0 ) - soft_makedirs( newdb.dirname() ) + paths = dbfiles() + newpath = dbdir + str( dbnum(paths[-1]) + 1 if len( paths ) > 0 else 0 ) + soft_makedirs( newpath.dirname() ) db.check() - with file(newdb, 'w') as f: dump( ( time(), db ), f ) + # check if there have been any changes + dosave = True + if paths != []: + with file(paths[-1]) as f: tstamp, prevdb = load(f) + if prevdb == db: + info( 'no changes, not saving' ) + dosave = False + if dosave: + info( 'saving', newpath ) + with file(newpath, 'w') as f: dump( ( time(), db ), f ) # # zdb # -def createdb(): +def createdb( init = False ): 'Initialize a new DB and set the `db` global to it.' global db db = zdb() - db.add( 'concept', [], [ 'notes' ] ) - # some common types - db.add( 'person', [ 'concept' ], [ 'cell', 'email', 'aim', 'msn', 'google', 'residence' ] ) -# db.add( 'place', [ 'concept' ], [ 'yelp URL' ] ) -# db.add( 'event', [ 'concept' ], [ 'date/time' ] ) -# db.add( 'group', [ 'concept' ], [ 'cell' ] ) + if init: + db.add( 'concept', [], [ 'notes' ] ) + # some common types + db.add( 'person', [ 'concept' ], [ 'cell', 'email', 'aim', 'msn', 'google', 'residence' ] ) +# db.add( 'place', [ 'concept' ], [ 'yelp URL' ] ) +# db.add( 'event', [ 'concept' ], [ 'date/time' ] ) +# db.add( 'group', [ 'concept' ], [ 'cell' ] ) # # parser @@ -156,7 +173,7 @@ return f.readline().rstrip() try: f = StringIO(s.strip()) - parents = None + parents = [] while True: line = readline(f).strip() if line == '': break @@ -192,7 +209,7 @@ class parser_tests( TestCase ): def setUp( self ): - createdb() + createdb( True ) def test_simple( self ): parsed = list( parse( ''' Yang Zhang @@ -224,13 +241,15 @@ def main( argv ): global db + config_logging( do_console = True ) + try: loaddb() - except zdb_exception: createdb() + except zdb_exception: createdb( True ) while True: stdout.write( '>>> ' ) line = stdin.readline() - if line == '': break + if line == '': print; break words = line.split() if words != []: cmd, params = words[0], words[1:] @@ -240,13 +259,13 @@ elif cmd == 'del': db.rm( *params ) elif cmd == 'set': db.set( *params ) elif cmd == 'dump': db.dump( *params ) - elif cmd == 'load': db.load( *params ) + elif cmd == 'load': importdb( *params ) elif cmd == 'quit': break else: raise zdb_exception( 'bad command: ' + cmd ) - except BaseException, ex: + except Exception, ex: # print >> stderr, ex handle_exceptions() savedb() -run_main( handle_exceptions = True ) +run_main( handle_exceptions = False ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |