1 #!/usr/bin/env python2.4
2 # -*- encoding: iso-8859-1 -*-
3 # vim: set et sw=4 sts=4 :
13 from sercom.dbo import *
17 %s objeto comando [opciones]''' % sys.argv[0]
19 def args2dict(l, conn):
20 d = {'connection': conn}
22 key, val = arg.split('=')
23 if val == 'None': val = None
28 print >>sys.stderr, 'Faltan argumentos!'
29 print >>sys.stderr, ayuda()
33 locale.setlocale(locale.LC_ALL, '')
35 # Obtengo configuración
36 conf = ConfigParser.SafeConfigParser()
37 if not conf.read(('/etc/sercom/sercom.ini', os.path.expanduser('~/.sercom/sercom.ini'), 'sercom.ini')):
38 sys.stderr.write('No se pudo obtener configuración!\n')
42 conn = sqlobject.connectionForURI(conf.get('dbo', 'database'))
44 # Argumentos interesantes
50 if sys.argv[3].isdigit():
51 oid = int(sys.argv[3])
57 if cmd in ('list', 'ls'):
59 objs = [eval(obj).get(oid, connection=conn)]
61 objs = eval(obj).selectBy(**args2dict(args, conn))
63 objs = eval(obj).select(connection=conn)
66 elif cmd in ('rm', 'remove', 'delete', 'del'):
68 objs = [eval(obj).get(oid, connection=conn)]
70 objs = eval(obj).selectBy(args2dict(args, conn))
74 elif cmd in ('set', 'update'):
76 objs = [eval(obj).get(oid, connection=conn)]
78 where = args[:args.index('--')]
79 args = args[args.index('--')+1:]
80 objs = eval(obj).selectBy(**args2dict(where, conn))
82 print 'Modificando:', o,
83 o.set(**args2dict(args, conn))
85 elif cmd in ('add', 'new', 'insert'):
86 o = eval(obj)(**args2dict(args, conn))
89 print >>sys.stderr, 'Comando incorrecto (%s)!' % cmd
90 print >>sys.stderr, ayuda()
93 print >>sys.stderr, 'Argumento inválido!'
94 print >>sys.stderr, ayuda()
96 except Exception, msg:
97 print >>sys.stderr, 'ERROR:', msg