X-Git-Url: https://git.llucax.com/software/sercom-old.git/blobdiff_plain/e8abee9ba294fb9900e0d057945b286886e117e1..44941ea0c8336d1b0241fc3e09051ef5fe25b1c8:/src/sc_dbq?ds=sidebyside diff --git a/src/sc_dbq b/src/sc_dbq index 61f69e4..1c90a3f 100755 --- a/src/sc_dbq +++ b/src/sc_dbq @@ -5,12 +5,11 @@ # Módulos estándar import os import sys -import locale -import ConfigParser # Módulos externos import sqlobject # Módulos locales -from sercom.dbo import * +import sercom +from sercom.sqlo import * def ayuda(): return '''Uso: @@ -20,28 +19,28 @@ def args2dict(l, conn): d = {'connection': conn} for arg in l: key, val = arg.split('=') + if val == 'None': val = None + if val == 'True': val = True + if val == 'False': val = False d[key] = val return d -if len(sys.argv) < 3: - print >>sys.stderr, 'Faltan argumentos!' - print >>sys.stderr, ayuda() - sys.exit(1) - -# Seteo locale -locale.setlocale(locale.LC_ALL, '') +# Inicializo +conf, conn, log = sercom.init('dbq') -# Obtengo configuración -conf = ConfigParser.SafeConfigParser() -if not conf.read(('/etc/sercom/sercom.ini', os.path.expanduser('~/.sercom/sercom.ini'), 'sercom.ini')): - sys.stderr.write('No se pudo obtener configuración!\n') +if len(sys.argv) < 3: + log.error('Faltan argumentos!') + log.error(ayuda()) sys.exit(1) -# Conexión a la DB -conn = sqlobject.connectionForURI(conf.get('dbo', 'database')) - # Argumentos interesantes obj = sys.argv[1] +try: + pos = obj.index('.') + attr = obj[pos+1:] + obj = obj[:pos] +except: + attr = None cmd = sys.argv[2] oid = None args = sys.argv[3:] @@ -61,7 +60,14 @@ try: else: objs = eval(obj).select(connection=conn) for o in objs: - print o + if attr is None: + print o + else: + print o + print attr + ':' + import pprint + attr = eval('o.' + attr) + pprint.pprint(attr) elif cmd in ('rm', 'remove', 'delete', 'del'): if oid is not None: objs = [eval(obj).get(oid, connection=conn)] @@ -79,20 +85,20 @@ try: objs = eval(obj).selectBy(**args2dict(where, conn)) for o in objs: print 'Modificando:', o, - o.set(**args2dict(args)) + o.set(**args2dict(args, conn)) print '->', o elif cmd in ('add', 'new', 'insert'): o = eval(obj)(**args2dict(args, conn)) print 'Agregado:', o else: - print >>sys.stderr, 'Comando incorrecto (%s)!' % cmd - print >>sys.stderr, ayuda() + log.error('Comando incorrecto (%s)!', cmd) + log.error(ayuda()) sys.exit(2) except ValueError: - print >>sys.stderr, 'Argumento inválido!' - print >>sys.stderr, ayuda() + log.error('Argumento inválido!') + log.error(ayuda()) sys.exit(3) except Exception, msg: - print >>sys.stderr, 'ERROR:', msg + log.exception(msg) sys.exit(100)