#!/usr/bin/env python2.4 # -*- encoding: iso-8859-1 -*- # vim: set et sw=4 sts=4 : # Módulos estándar import os import sys # Módulos externos import sqlobject # Módulos locales import sercom from sercom.sqlo import * def ayuda(): return '''Uso: %s objeto comando [opciones]''' % sys.argv[0] 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 # Inicializo conf, conn, log = sercom.init('dbq') if len(sys.argv) < 3: log.error('Faltan argumentos!') log.error(ayuda()) sys.exit(1) # 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:] try: if sys.argv[3].isdigit(): oid = int(sys.argv[3]) args = sys.argv[4:] except: pass # Proceso comandos try: if cmd in ('list', 'ls'): if oid is not None: objs = [eval(obj).get(oid, connection=conn)] elif args: objs = eval(obj).selectBy(**args2dict(args, conn)) else: objs = eval(obj).select(connection=conn) for o in objs: 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)] else: objs = eval(obj).selectBy(args2dict(args, conn)) for o in objs: print 'Borrando:', o o.destroySelf() elif cmd in ('set', 'update'): if oid is not None: objs = [eval(obj).get(oid, connection=conn)] else: where = args[:args.index('--')] args = args[args.index('--')+1:] objs = eval(obj).selectBy(**args2dict(where, conn)) for o in objs: print 'Modificando:', o, o.set(**args2dict(args, conn)) print '->', o elif cmd in ('add', 'new', 'insert'): o = eval(obj)(**args2dict(args, conn)) print 'Agregado:', o else: log.error('Comando incorrecto (%s)!', cmd) log.error(ayuda()) sys.exit(2) except ValueError: log.error('Argumento inválido!') log.error(ayuda()) sys.exit(3) except Exception, msg: log.exception(msg) sys.exit(100)