X-Git-Url: https://git.llucax.com/software/sercom-old.git/blobdiff_plain/03d6ed2cc045807bff6070192dd5fe75f14df5a0..a9db9840ef35d1c1450d0a97fbfbf2b308474c73:/src/sc_dbq diff --git a/src/sc_dbq b/src/sc_dbq index e105f1f..2b06334 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: @@ -21,28 +20,33 @@ def args2dict(l, conn): for arg in l: key, val = arg.split('=') if val == 'None': val = None + elif val == 'True': val = True + elif val == 'False': val = False + elif val.isdigit(): + val = int(val) + else: + try: + val = float(val) + except ValueError: pass 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:] @@ -62,7 +66,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)] @@ -86,14 +97,14 @@ try: 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)