def setUp(self): self.config = testing.setUp(settings=self.settings) self.config.include('pyramid_mako') self.config.include('pyramid_tm') setup_routes(self.config) DBSession.remove() self.trans = self.conn.begin() DBSession.configure(bind=self.conn) Base.metadata.create_all(self.engine) self.session = DBSession
def main(argv=sys.argv): if len(argv) != 2: usage(argv) config_uri = argv[1] setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) initialize_db() alembic = Config(config_uri) command.stamp(alembic, 'head')
def main(argv=sys.argv): parser = ArgumentParser(description=__doc__) parser.add_argument('config') parser.add_argument('csvfile', type=str, action='store', help="CSV file") parser.add_argument('-q', dest='quiet', action='store_true', help="Dont print every address") commit = parser.add_mutually_exclusive_group() commit.add_argument('-n', dest='commit_never', action='store_true', help="Dont commit changes and dont ask") commit.add_argument('-y', dest='commit_always', action='store_true', help="Commit changes without asking") args = parser.parse_args() config_uri = args.config setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) transaction.begin() known_domains = {} counter = 0 with open(args.csvfile, 'r') as csvfile: reader = csv.reader(csvfile) for row in reader: local, password, redirect, dname = row if dname in known_domains: domain = known_domains[dname] else: try: domain = Domain.query(name=dname).one() except NoResultFound: domain = Domain(name=dname) DBSession.add(domain) known_domains[domain.name] = domain if not local: if not redirect: print('Invalid address: catch-all without redirect') return 1 domain.catchall = redirect print('*@%s -> %s' % (domain.name, domain.catchall)) continue address = Address() address.local_part = local address.password = password or None address.redirect = redirect or None address.domain = domain address.domain_id = domain.id DBSession.add(address) if not args.quiet: print(address.address, end='') if address.password: print(' [...%s]' % address.password[-8:], end='') if address.redirect: print(' -> %s' % address.redirect, end='') print() counter += 1 if args.commit_always: transaction.commit() print('saved.') elif args.commit_never: transaction.abort() print('aborted.') else: r = input('Commit %d addresses? ' % counter) if r.strip().lower() == 'y': transaction.commit() print('saved.') else: transaction.abort() print('aborted.')
def main(argv=sys.argv): parser = ArgumentParser(description=__doc__) parser.add_argument('config') parser.add_argument('username', type=str, action='store', help="Username") command = parser.add_mutually_exclusive_group() command.add_argument('-P', '--set-password', action='store_true', help="Change user's password (prompt)") command.add_argument('-L', '--lock', action='store_true', help="Lock account") command.add_argument('-U', '--unlock', action='store_true', help="Unlock account") command.add_argument('-I', '--info', action='store_true', help="Account details (default)") command.add_argument('-A', '--add', action='store_true', help="Add account") args = parser.parse_args() config_uri = args.config setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) with transaction.manager: if args.add: u = User(username=args.username) p = getpass.getpass() if not p: print("No password supplied, user not created.") return 1 u.set_password(p) DBSession.add(u) DBSession.flush() log('add_user', user=u.id) else: user = User.query(username=args.username).first() if not user: print("User not found.") return 1 if args.set_password: p = getpass.getpass() if not p: print("No password supplied, user not created.") return 1 user.set_password(p) log('set_password', target=user) print("Password changed") elif args.lock: user.enabled = False log('disable', target=user) print("Account locked") elif args.unlock: user.enabled = True log('enable', target=user) print("Account unlocked") else: print("Account #%d" % user.id) print("Username: %s" % user.username) print("Status: %s" % 'enabled' if user.enabled else 'disabled')
def main(argv=sys.argv): parser = ArgumentParser(description=__doc__) parser.add_argument('config') parser.add_argument('-u', '--userid', type=int, action='store', help='Filter by user ID') parser.add_argument('-a', '--addressid', type=int, action='store', help='Filter by address ID') parser.add_argument('-U', '--user', type=str, action='store', help='Filter by username') parser.add_argument('-A', '--address', type=str, action='store', help='Filter by address') parser.add_argument('-s', '--site', action='store_true', help='Get only site entries (no user or address)') parser.add_argument('-t', '--type', action='store', help='Filter by entry type') parser.add_argument('-r', '--reverse', action='store_true', help='Latest entries first') parser.add_argument('-n', '--lines', type=int, action='store', help='Limit number of entries') args = parser.parse_args() config_uri = args.config setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) q = DBSession.query(Log) filters = ('userid', 'addressid', 'user', 'address', 'site') if sum(1 for f in filters if getattr(args, f)) > 1: print('Only use one of ' + ', '.join('--'+f for f in filters)) return 1 if args.userid: q = q.filter(Log.user_id == args.userid) elif args.addressid: q = q.filter(Log.address_id == args.addressid) elif args.user: q = q.filter(User.username == args.user) q = q.filter(Log.user_id == User.id) elif args.address: q = q.filter(Address.address == args.address) q = q.filter(Log.address_id == Address.id) q = q.filter(Address.domain_id == Domain.id) elif args.site: q = q.filter(Log.user_id == None) q = q.filter(Log.address_id == None) if args.type: q = q.filter(Log.type == args.type) if args.reverse: q = q.order_by(Log.date) else: q = q.order_by(Log.date.desc()) if args.lines: q = q.limit(args.lines) for entry in reversed(q.all()): date = entry.date.strftime('%Y-%m-%d %H:%M:%S') if entry.user_id: id = 'user#{}'.format(entry.user_id) elif entry.address_id: id = 'addr#{}'.format(entry.address_id) else: id = 'site' print('{date} {id}: {text}' .format(date=date, id=id, text=entry.get_text()))