[Assorted-commits] SF.net SVN: assorted:[938] simple-zdb/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-08-10 22:43:22
|
Revision: 938 http://assorted.svn.sourceforge.net/assorted/?rev=938&view=rev Author: yangzhang Date: 2008-08-10 22:43:31 +0000 (Sun, 10 Aug 2008) Log Message: ----------- - added custom checks - moved the CUI frontend to cmdline to get a core zdb package - added phonesync (another frontend) to analyze nokia exported csv files - added a google calendar client; this should eventually become another frontend Modified Paths: -------------- simple-zdb/trunk/src/zdb.py Added Paths: ----------- simple-zdb/trunk/src/cal.py simple-zdb/trunk/src/cmdline.py simple-zdb/trunk/src/phonesync.py Added: simple-zdb/trunk/src/cal.py =================================================================== --- simple-zdb/trunk/src/cal.py (rev 0) +++ simple-zdb/trunk/src/cal.py 2008-08-10 22:43:31 UTC (rev 938) @@ -0,0 +1,88 @@ +#!/usr/bin/env python + +from __future__ import with_statement +from atom import Content, Title, Summary +from path import path +from commons.startup import run_main +from time import gmtime, strftime, time +from gdata.calendar.service import CalendarService +from gdata.calendar import ( + CalendarEventEntry, + CalendarListEntry, + Color, + Hidden, + Timezone, + When, + Where, +) + +def main( argv ): + + print_calendars = True + print_events = True + + # authentication + + with file( path( '~/.google.auth' ).expanduser() ) as f: + user, pw = f.read().split('\n')[:2] + + cal = CalendarService( email = user, password = pw, source = 'zdb' ) + cal.ProgrammaticLogin() + + # get feed of user-owned calendars + + cs = cal.GetOwnCalendarsFeed() + + # print own calendars + + if print_calendars: + print cs.title.text + for i,c in enumerate(cs.entry): + print ' %2d. %s' % (i, c.title.text,) + + # create zdb calendar if necessary + + if 'zdb' not in ( c.title.text for c in cs.entry ): + cle = CalendarListEntry() + cle.title = Title( text = 'zdb' ) + cle.summary = Summary( text = 'Google Calendar frontend to zdb' ) + cle.where = Where( value_string = '' ) + cle.color = Color( value = '#2952A3' ) + cle.timezone = Timezone( value = 'America/New_York' ) + cle.hidden = Hidden( value = 'false' ) + zdbcal = cal.InsertCalendar( new_calendar = cle ) + # XXX THE UNTAKEN PATH + else: + zdbcal = ( c for c in cs.entry if c.title.text == 'zdb' ).next() + + # get feed of events on zdb calendar + + zdburi = ( l.href for l in zdbcal.link if l.rel == 'alternate' ).next() + es = cal.GetCalendarEventFeed( uri = zdburi ) + + # print events on zdb calendar + + if print_events: + print 'Events for calendar: %s' % (es.title.text,) + for i,e in enumerate(es.entry): + print ' %2d. %s (%s - %s)' % (i, e.title.text, e.when[0].start_time, e.when[0].end_time) + + return es + + # parse events + + for e in es.entry: + pass + + # create event on default calendar + + if False: + event = CalendarEventEntry() + event.title = Title( text = 'testing' ) + event.content = Content( text = 'my content' ) + event.when.append( When( + start_time = strftime('%Y-%m-%dT%H:%M:%S.000Z', gmtime()), + end_time = strftime('%Y-%m-%dT%H:%M:%S.000Z', gmtime(time() + 3600)) ) ) + new_event = cal.InsertEvent( event, zdburi ) + +run_main() Property changes on: simple-zdb/trunk/src/cal.py ___________________________________________________________________ Added: svn:executable + * Added: simple-zdb/trunk/src/cmdline.py =================================================================== --- simple-zdb/trunk/src/cmdline.py (rev 0) +++ simple-zdb/trunk/src/cmdline.py 2008-08-10 22:43:31 UTC (rev 938) @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +from __future__ import with_statement +from commons.exceps import handle_exceptions +from commons.log import config_logging +from commons.startup import run_main +from sys import stdin, stdout +from zdb import * + +def main( argv ): + config_logging( do_console = True ) + + try: loaddb() + except zdb_exception: createdb( True ) + + while True: + stdout.write( '>>> ' ) + line = stdin.readline() + if line == '': print; break + words = line.split() + if words != []: + cmd, params = words[0], words[1:] + try: + if False: pass + elif cmd == 'add': thedb().add( *params ) + elif cmd == 'del': thedb().rm( *params ) + elif cmd == 'set': thedb().set( *params ) + elif cmd == 'dump': thedb().dump( *params ) + elif cmd == 'load': importdb( *params ) + elif cmd == 'quit': break + else: raise zdb_exception( 'bad command: ' + cmd ) + except Exception, ex: + # print >> stderr, ex + handle_exceptions() + + savedb() + +run_main( handle_exceptions = False ) Property changes on: simple-zdb/trunk/src/cmdline.py ___________________________________________________________________ Added: svn:executable + * Added: simple-zdb/trunk/src/phonesync.py =================================================================== --- simple-zdb/trunk/src/phonesync.py (rev 0) +++ simple-zdb/trunk/src/phonesync.py 2008-08-10 22:43:31 UTC (rev 938) @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +from __future__ import with_statement +from commons.log import config_logging +from commons.startup import run_main +from csv import reader +from itertools import chain, imap +from re import compile +from sys import stdin, stdout +from zdb import * + +pat = compile( r'[\s-]+' ) + +def simplify( x ): + return pat.sub( '', x.lower() ) + +def findperson( first, last ): + first, last = map( simplify, [ first, last ] ) + for k in thedb().name2obj.iterkeys(): + words = map( simplify, k.split() ) + if ( first == words[0] and + ( last == words[-1] or + len( last ) == 0 or + len( last ) == 1 and last[0] == words[-1][0] ) ): + yield k + +def main( argv ): + config_logging( do_console = True ) + + try: loaddb() + except zdb_exception: createdb( True ) + + lines = stdin.read().decode('utf16').strip().split('\n') + + for row in list( reader( lines ) )[1:]: + phonevals = filter( lambda x: x != '', row[4:] ) + if row[1] == '' and row[3] == '': + print 'cannot handle: %s' % ( row, ) + else: + matches = list( findperson( row[1], row[3] ) ) + if len( matches ) > 1: + raise zdb_exception( 'more than one match for %s %s: %s' % + ( row[1], row[3], ', '.join(matches) ) ) + elif matches == []: + print 'no match found for', row[1], row[3] + print 'values:', ', '.join( phonevals ) + print + elif len( matches ) == 1: + o = thedb().get( matches[0] ) + zdbvals = set( imap( simplify, chain( *o.props.itervalues() ) ) ) + failed = False + for cell in phonevals: + if simplify(cell) not in zdbvals: + failed = True + break + if failed: + print 'phonevals:', ', '.join( phonevals ) + print 'zdbvals:', ', '.join( zdbvals ) + o.dump( stdout ) + print + else: assert False + +run_main() Property changes on: simple-zdb/trunk/src/phonesync.py ___________________________________________________________________ Added: svn:executable + * Modified: simple-zdb/trunk/src/zdb.py =================================================================== --- simple-zdb/trunk/src/zdb.py 2008-08-10 21:41:42 UTC (rev 937) +++ simple-zdb/trunk/src/zdb.py 2008-08-10 22:43:31 UTC (rev 938) @@ -4,17 +4,16 @@ 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.log import info from commons.files import soft_makedirs from commons.startup import run_main from commons.structs import free_struct from os import rename from path import path from re import match -from sys import stdin, stdout, stderr +from sys import stderr from time import time -from unittest import TestCase, main as test_main +from unittest import TestCase from functools import partial info = partial( info, '' ) @@ -43,6 +42,8 @@ # User-facing functions (these are the console commands) def add( self, name, parents, props ): self.addobj( obj( name, parents, props ) ) + def get( self, name ): + return self.name2obj[ name ] def set( self, name, field, value ): self.name2obj[ name ].addprop( field, value ) def rm( self, name ): @@ -77,6 +78,21 @@ assert p in self.name2obj, 'parent doesnt exist: %s' % p # TODO make sure there are no cycles in the parent graph and that # everything terminates at root 'concept' + + # perform custom checks + def get(k): return o.props.get(k, []) + if 'person' in o.parents: + for spousename in get( 'spouse' ): + spouse = self.name2obj[ spousename ] + assert name in spouse.props.get( 'spouse', [] ) and spouse.parents == [ 'person' ] + elif 'event' in o.parents: + for placename in get( 'place' ): + place = self.name2obj[ placename ] + assert place.parents == [ 'place' ] + for personname in get( 'people' ): + person = self.name2obj[ personname ] + assert person.parents == [ 'person' ] + except AssertionError, ex: print >> stderr, o.name raise @@ -160,6 +176,10 @@ # db.add( 'event', [ 'concept' ], [ 'date/time' ] ) # db.add( 'group', [ 'concept' ], [ 'cell' ] ) +def thedb(): + global db + return db + # # parser # @@ -233,39 +253,3 @@ expected[1].addprop( 'email', 'mj...@ne...' ) expected[1].addprop( 'notes', '- loves kids' ) self.assertEqual( parsed, expected ) - -# -# main -# - -def main( argv ): - global db - - config_logging( do_console = True ) - - try: loaddb() - except zdb_exception: createdb( True ) - - while True: - stdout.write( '>>> ' ) - line = stdin.readline() - if line == '': print; break - words = line.split() - if words != []: - cmd, params = words[0], words[1:] - try: - if False: pass - elif cmd == 'add': db.add( *params ) - elif cmd == 'del': db.rm( *params ) - elif cmd == 'set': db.set( *params ) - elif cmd == 'dump': db.dump( *params ) - elif cmd == 'load': importdb( *params ) - elif cmd == 'quit': break - else: raise zdb_exception( 'bad command: ' + cmd ) - except Exception, ex: - # print >> stderr, ex - handle_exceptions() - - savedb() - -run_main( handle_exceptions = False ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |