def clean(): """Recursively remove *.pyc and *.pyo files.""" for dirpath, dirnames, filenames in os.walk('.'): for filename in filenames: if filename.endswith('.pyc') or filename.endswith('.pyo'): filepath = os.path.join(dirpath, filename) click.echo(f'Removing {filepath}') os.remove(filepath)
def sync( symbols, equity_manager: EquityManager = injectable, marketstore_service: MarketstoreService = injectable, ): symbols = symbols or [] if not symbols: equities = {equity.ticker: equity for equity in equity_manager.all()} else: equities = { equity.ticker: equity for equity in equity_manager.filter_by_tickers(symbols) } async def dl(session, symbol): url = yahoo.get_yfi_url(symbol) try: async with session.get(url) as r: data = await r.json() if r.status != 200: return symbol, data['chart']['error']['code'] except Exception as e: return symbol, e else: df = yahoo.yfi_json_to_df(data, '1d') if df is None: return symbol, "Invalid Data" click.echo(f'writing {symbol}') marketstore_service.write(df, f'{symbol}/1D/OHLCV') async def dl_all(symbols): errors = [] async with aiohttp.ClientSession() as session: for batch in chunk(symbols, 8): tasks = [dl(session, symbol) for symbol in batch] results = await asyncio.gather(*tasks, return_exceptions=False) errors.extend([error for error in results if error]) return errors loop = asyncio.get_event_loop() errors = loop.run_until_complete( dl_all(list(set(equities.keys()) | set(symbols)))) if errors: click.echo('!!! Handling Errors !!!') for error in errors: if isinstance(error, tuple): symbol, msg = error if msg == 'Not Found': equity_manager.delete(equities[symbol]) else: print(f'{symbol}: {msg}') else: print(error) # FIXME: properly handle exceptions... equity_manager.commit() click.echo('Done')
def list_roles(): """ List roles. """ roles = role_manager.all() if roles: print_table(['ID', 'Name'], [(role.id, role.name) for role in roles]) else: click.echo('No roles found.')
def create_role(name): """ Create a new role. """ role = role_manager.create(name=name) if click.confirm(f'Are you sure you want to create {role!r}?'): role_manager.save(role, commit=True) click.echo(f'Successfully created {role!r}') else: click.echo('Cancelled.')
def delete_user(query): """ Delete a user. """ user = _query_to_user(query) if click.confirm(f'Are you sure you want to delete {user!r}?'): user_manager.delete(user, commit=True) click.echo(f'Successfully deleted {user!r}') else: click.echo('Cancelled.')
def delete_role(query): """ Delete a role. """ role = _query_to_role(query) if click.confirm(f'Are you sure you want to delete {role!r}?'): role_manager.delete(role, commit=True) click.echo(f'Successfully deleted {role!r}') else: click.echo('Cancelled.')
def activate_user(query): """ Activate a user. """ user = _query_to_user(query) if click.confirm(f'Are you sure you want to activate {user!r}?'): user.active = True user_manager.save(user, commit=True) click.echo(f'Successfully activated {user!r}') else: click.echo('Cancelled.')
def set_password(query, password, send_email): """ Set a user's password. """ user = _query_to_user(query) if click.confirm(f'Are you sure you want to change {user!r}\'s password?'): security_service.change_password(user, password, send_email=send_email) user_manager.save(user, commit=True) click.echo(f'Successfully updated password for {user!r}') else: click.echo('Cancelled.')
def add_role_to_user(user, role): """ Add a role to a user. """ user = _query_to_user(user) role = _query_to_role(role) if click.confirm(f'Are you sure you want to add {role!r} to {user!r}?'): user.roles.append(role) user_manager.save(user, commit=True) click.echo(f'Successfully added {role!r} to {user!r}') else: click.echo('Cancelled.')
def remove_role_from_user(user, role): """ Remove a role from a user. """ user = _query_to_user(user) role = _query_to_role(role) if click.confirm( f'Are you sure you want to remove {role!r} from {user!r}?'): user.roles.remove(role) user_manager.save(user, commit=True) click.echo(f'Successfully removed {role!r} from {user!r}') else: click.echo('Cancelled.')
async def dl(session, symbol): url = yahoo.get_yfi_url(symbol) try: async with session.get(url) as r: data = await r.json() if r.status != 200: return symbol, data['chart']['error']['code'] except Exception as e: return symbol, e else: df = yahoo.yfi_json_to_df(data, '1d') if df is None: return symbol, "Invalid Data" click.echo(f'writing {symbol}') marketstore_service.write(df, f'{symbol}/1D/OHLCV')
def list_users(): """ List users. """ users = user_manager.all() if users: print_table(['ID', 'Email', 'Active', 'Confirmed At'], [( user.id, user.email, 'True' if user.active else 'False', user.confirmed_at.strftime('%Y-%m-%d %H:%M%z') if user.confirmed_at else 'None', ) for user in users]) else: click.echo('No users found.')
def import_fixtures(bundles=None): fixture_dirs = [] for path in ['db', 'db.fixtures']: root = unchained._app.root_path path = path.replace('.', os.sep) full_path = os.path.join(root, path) if os.path.exists(full_path) and os.path.isdir(full_path): fixture_dirs.append(path) for bundle_name in (bundles or unchained.bundles.keys()): bundle = unchained.bundles[bundle_name] dirs = ModelFixtureFoldersHook.get_fixtures_dirs(bundle) if dirs: fixture_dirs.extend(dirs) click.echo(f'Loading fixtures from {bundle_name}') if not fixture_dirs: click.echo('No fixtures directories were found. Exiting') import sys sys.exit(0) factory = SQLAlchemyModelFactory(db_ext.session, unchained.sqlalchemy_bundle.models) loader = FixturesLoader(factory, fixture_dirs=fixture_dirs) loader.create_all(lambda identifier, model, created: click.echo( f'{"Creating" if created else "Updating"} {identifier.key}: {model!r}') ) click.echo('Finished adding fixtures')
def project(dest, app_bundle, force, dev, admin, api, celery, graphene, mail, oauth, security, session, sqlalchemy, webpack): """ Create a new Flask Unchained project. """ if os.path.exists(dest) and os.listdir(dest) and not force: if not click.confirm( f'WARNING: Project directory {dest!r} exists and is ' f'not empty. It will be DELETED!!! Continue?'): click.echo(f'Exiting.') sys.exit(1) # build up a list of dependencies # IMPORTANT: keys here must match setup.py's `extra_requires` keys ctx = dict(dev=dev, admin=admin, api=api, celery=celery, graphene=graphene, mail=mail, oauth=oauth, security=security or oauth, session=security or session, sqlalchemy=security or sqlalchemy, webpack=webpack) ctx['requirements'] = [k for k, v in ctx.items() if v] # remaining ctx vars ctx['app_bundle_module_name'] = app_bundle # copy the project template into place copy_file_tree(PROJECT_TEMPLATE, dest, ctx, [(option, files) for option, files in [ ('api', ['app/serializers']), ('celery', ['app/tasks', 'celery.py']), ('graphene', ['app/graphql']), ('mail', ['templates/email']), ('security', [ 'app/models/role.py', 'app/models/user.py', 'db/fixtures/Role.yaml', 'db/fixtures/User.yaml' ]), ('webpack', ['assets', 'package.json', 'webpack']), ] if not ctx[option]]) click.echo(f'Successfully created a new project at: {dest}')
def create_user(email, password, active, confirmed_at, send_email): """ Create a new user. """ if confirmed_at == 'now': confirmed_at = security.datetime_factory() user = user_manager.create(email=email, password=password, active=active, confirmed_at=confirmed_at) if click.confirm(f'Are you sure you want to create {user!r}?'): security_service.register_user(user, allow_login=False, send_email=send_email) user_manager.save(user, commit=True) click.echo(f'Successfully created {user!r}') else: click.echo('Cancelled.')
def confirm_user(query): """ Confirm a user account. """ user = _query_to_user(query) if click.confirm(f'Are you sure you want to confirm {user!r}?'): if security_service.confirm_user(user): click.echo(f'Successfully confirmed {user!r} at ' f'{user.confirmed_at.strftime("%Y-%m-%d %H:%M:%S%z")}') user_manager.save(user, commit=True) else: click.echo(f'{user!r} has already been confirmed.') else: click.echo('Cancelled.')
def import_fixtures(bundles=None): fixture_dirs = [] for bundle_name in (bundles or unchained.bundles.keys()): bundle = unchained.bundles[bundle_name] fixtures_dir = ModelFixtureFoldersHook.get_fixtures_dir(bundle) if fixtures_dir: fixture_dirs.append(fixtures_dir) click.echo(f'Loading fixtures from {bundle_name}') if not fixture_dirs: click.echo('No fixtures directories were found. Exiting') import sys sys.exit(0) factory = SQLAlchemyModelFactory(db_ext.session, unchained.sqlalchemy_bundle.models) loader = FixturesLoader(factory, fixture_dirs=fixture_dirs) loader.create_all(lambda identifier, model, created: click.echo( f'{"Creating" if created else "Updating"} {identifier.key}: {model!r}') ) click.echo('Finished adding fixtures')
def gar(): click.echo('myapp')
def gar(): """vendor_bundle docstring""" click.echo('vendor_bundle')
def vendor_top_level(): """override_vendor_bundle docstring""" click.echo('override_vendor_bundle')
def gaz(): """the overridden group should not contain this command""" click.echo('vendor_bundle')
def bar(): """override_vendor_bundle docstring""" click.echo('override_vendor_bundle')
def vendor_top_level(): """vendor_bundle docstring""" click.echo('vendor_bundle')
def top_level(): click.echo('myapp')
def baz(): """myapp docstring""" click.echo('myapp')
def execute_tool(desc, *args): command = list(args) + files_and_dirs click.echo(f"{desc}: {' '.join(command)}") ret = call(command) if ret != 0: exit(ret)