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
56 if sys.argv[3].isdigit():
57 oid = int(sys.argv[3])
63 if cmd in ('list', 'ls'):
65 objs = [eval(obj).get(oid, connection=conn)]
67 objs = eval(obj).selectBy(**args2dict(args, conn))
69 objs = eval(obj).select(connection=conn)
77 attr = eval('o.' + attr)
79 elif cmd in ('rm', 'remove', 'delete', 'del'):
81 objs = [eval(obj).get(oid, connection=conn)]
83 objs = eval(obj).selectBy(args2dict(args, conn))
87 elif cmd in ('set', 'update'):
89 objs = [eval(obj).get(oid, connection=conn)]
91 where = args[:args.index('--')]
92 args = args[args.index('--')+1:]
93 objs = eval(obj).selectBy(**args2dict(where, conn))
95 print 'Modificando:', o,
96 o.set(**args2dict(args, conn))
98 elif cmd in ('add', 'new', 'insert'):
99 o = eval(obj)(**args2dict(args, conn))
102 print >>sys.stderr, 'Comando incorrecto (%s)!' % cmd
103 print >>sys.stderr, ayuda()
106 print >>sys.stderr, 'Argumento inválido!'
107 print >>sys.stderr, ayuda()
109 except Exception, msg:
110 print >>sys.stderr, 'ERROR:', msg