Skip to content

Commit

Permalink
reformat code with black
Browse files Browse the repository at this point in the history
  • Loading branch information
nicbus committed Apr 23, 2024
1 parent 3986e13 commit 6265c0e
Show file tree
Hide file tree
Showing 18 changed files with 1,076 additions and 1,078 deletions.
139 changes: 73 additions & 66 deletions faucet_rgb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,27 @@

def print_assets_and_quit(assets, asset_id):
"""Print provided assets and asset ID, then terminate the process."""
print('List of available NIA assets:')
print("List of available NIA assets:")
for asset in assets.nia:
print(' -', asset.asset_id, asset.ticker, asset.name, asset.precision,
asset.balance)
print('List of available CFA assets:')
print(
" -",
asset.asset_id,
asset.ticker,
asset.name,
asset.precision,
asset.balance,
)
print("List of available CFA assets:")
for asset in assets.cfa:
print(' -', asset.asset_id, asset.name, asset.description,
asset.precision, asset.data_paths, asset.balance)
print(
" -",
asset.asset_id,
asset.name,
asset.description,
asset.precision,
asset.data_paths,
asset.balance,
)
print(f'Cannot proceed: configured asset with id "{asset_id}" not found')
sys.exit(1)

Expand All @@ -39,59 +52,60 @@ def validate_migration_map(app):
New asset IDs in `ASSET_MIGRATION_MAP` (if any) must also be defined in the
`ASSETS` section.
"""
mig_map = app.config['ASSET_MIGRATION_MAP']
mig_map = app.config["ASSET_MIGRATION_MAP"]
if mig_map is None:
app.config['NON_MIGRATION_GROUPS'] = set(app.config['ASSETS'])
app.config["NON_MIGRATION_GROUPS"] = set(app.config["ASSETS"])
return
groups_to = set()
for asset_id in mig_map:
containing_group = None
for group_name, group_data in app.config['ASSETS'].items():
for asset in group_data['assets']:
if asset['asset_id'] == asset_id:
for group_name, group_data in app.config["ASSETS"].items():
for asset in group_data["assets"]:
if asset["asset_id"] == asset_id:
containing_group = group_name
if containing_group is None:
print(f'error in ASSET_MIGRATION_MAP! asset {asset_id} is not '
'defined in any group!')
print(f"error in ASSET_MIGRATION_MAP! asset {asset_id} is not " "defined in any group!")
sys.exit(1)

groups_to.add(containing_group)

# check all assets in migration groups are defined as migration destination
dest_asset_ids = mig_map.keys()
for group_to in groups_to:
for asset in app.config['ASSETS'][group_to]['assets']:
asset_id = asset['asset_id']
for asset in app.config["ASSETS"][group_to]["assets"]:
asset_id = asset["asset_id"]
if asset_id not in dest_asset_ids:
print(f'asset ID {asset_id} is not defined as a migration '
'destination while other assets in the same group are!')
print(
f"asset ID {asset_id} is not defined as a migration "
"destination while other assets in the same group are!"
)
sys.exit(1)
app.config['NON_MIGRATION_GROUPS'] = set(app.config["ASSETS"]) - groups_to
app.config["NON_MIGRATION_GROUPS"] = set(app.config["ASSETS"]) - groups_to


def _get_group_and_asset_from_id(app, asset_id):
for group_name, group_data in app.config['ASSETS'].items():
for asset in group_data['assets']:
if asset['asset_id'] == asset_id:
for group_name, group_data in app.config["ASSETS"].items():
for asset in group_data["assets"]:
if asset["asset_id"] == asset_id:
return (group_name, asset)
raise KeyError(asset_id)


def _get_all_requests_waiting_for_migration(rev_mig_map):
"""Gather all requests which haven't completed migration."""
reqs = Request.query.filter(
Request.status == 40 # consider only "served" status
).order_by(Request.wallet_id).all()
reqs = (
Request.query.filter(Request.status == 40) # consider only "served" status
.order_by(Request.wallet_id)
.all()
)
reqs_waiting_for_migration = []
for _, same_wallet_requests in itertools.groupby(reqs,
lambda r: r.wallet_id):
for _, same_wallet_requests in itertools.groupby(reqs, lambda r: r.wallet_id):
it1, it2 = itertools.tee(same_wallet_requests, 2)
for req in it1:
new_asset_id = rev_mig_map.get(req.asset_id)
if new_asset_id is None:
continue
wallet_migration_complete = any(r.asset_id == new_asset_id
for r in it2)
wallet_migration_complete = any(r.asset_id == new_asset_id for r in it2)
if not wallet_migration_complete:
reqs_waiting_for_migration.append(req)
return reqs_waiting_for_migration
Expand All @@ -102,15 +116,14 @@ def create_user_migration_cache(app):
See settings.py::Config for more details about the cache.
"""
mig_map = app.config['ASSET_MIGRATION_MAP']
mig_map = app.config["ASSET_MIGRATION_MAP"]
if mig_map is None:
return

with app.app_context():
rev_mig_map = {v: k for k, v in mig_map.items()}

reqs_waiting_for_migration = _get_all_requests_waiting_for_migration(
rev_mig_map)
reqs_waiting_for_migration = _get_all_requests_waiting_for_migration(rev_mig_map)

# build asset migration cache
mig_cache = {}
Expand All @@ -125,12 +138,12 @@ def create_user_migration_cache(app):
wallet_id = get_sha256_hex(req.wallet_id)
if wallet_id not in mig_cache[group]:
mig_cache[group][wallet_id] = asset
app.config['ASSET_MIGRATION_CACHE'] = mig_cache
app.config["ASSET_MIGRATION_CACHE"] = mig_cache

# update pending requests for old assets
for req in Request.query.filter(
and_(Request.status != 40,
Request.asset_id.in_(rev_mig_map.keys()))):
and_(Request.status != 40, Request.asset_id.in_(rev_mig_map.keys()))
):
# if there is a pending request for an old asset,
# update it with the new asset_id
req.asset_id = rev_mig_map[req.asset_id]
Expand All @@ -140,12 +153,11 @@ def create_user_migration_cache(app):
# log the current migration state
if len(mig_cache):
remaining = len(set().union(*mig_cache.values()))
app.logger.info(
f"{remaining} wallets are still not fully migrated.")
app.logger.info(f"{remaining} wallets are still not fully migrated.")
else:
app.logger.warning(
"All wallets are migrated! "
"You can drop `ASSET_MIGRATION_MAP` from config now.")
"All wallets are migrated! You can drop `ASSET_MIGRATION_MAP` from config now."
)


def create_app(custom_get_app=None, do_init_wallet=True):
Expand All @@ -159,8 +171,8 @@ def create_app(custom_get_app=None, do_init_wallet=True):
app = get_app(__name__) if custom_get_app is None else custom_get_app()

# configure data files to go inside the data dir
log_dir = os.path.sep.join([app.config['DATA_DIR'], 'logs'])
for cfg_var in ('LOG_FILENAME', 'LOG_FILENAME_SCHED'):
log_dir = os.path.sep.join([app.config["DATA_DIR"], "logs"])
for cfg_var in ("LOG_FILENAME", "LOG_FILENAME_SCHED"):
app.config[cfg_var] = os.path.sep.join([log_dir, app.config[cfg_var]])

# configuration checks
Expand All @@ -171,16 +183,17 @@ def create_app(custom_get_app=None, do_init_wallet=True):
# initialize the wallet
if do_init_wallet:
wallet_data = wallet_data_from_config(app.config)
app.config['ONLINE'], app.config['WALLET'] = init_wallet(
app.config['ELECTRUM_URL'], wallet_data)
app.config["ONLINE"], app.config["WALLET"] = init_wallet(
app.config["ELECTRUM_URL"], wallet_data
)

# ensure all the configured assets are available
wallet = app.config['WALLET']
wallet = app.config["WALLET"]
assets = wallet.list_assets([])
asset_ids = [asset.asset_id for asset in assets.nia + assets.cfa]
for _, data in app.config['ASSETS'].items():
for asset in data['assets']:
asset_id = asset['asset_id']
for _, data in app.config["ASSETS"].items():
for asset in data["assets"]:
asset_id = asset["asset_id"]
if asset_id not in asset_ids:
print_assets_and_quit(assets, asset_id)

Expand All @@ -191,23 +204,21 @@ def create_app(custom_get_app=None, do_init_wallet=True):
upgrade()

# configure logging (needs to be after migration as alembic resets it)
LOGGING['handlers']['file']['filename'] = app.config['LOG_FILENAME']
LOGGING['handlers']['file_sched']['filename'] = app.config[
'LOG_FILENAME_SCHED']
LOGGING['handlers']['console']['level'] = app.config['LOG_LEVEL_CONSOLE']
LOGGING['handlers']['file']['level'] = app.config['LOG_LEVEL_FILE']
LOGGING["handlers"]["file"]["filename"] = app.config["LOG_FILENAME"]
LOGGING["handlers"]["file_sched"]["filename"] = app.config["LOG_FILENAME_SCHED"]
LOGGING["handlers"]["console"]["level"] = app.config["LOG_LEVEL_CONSOLE"]
LOGGING["handlers"]["file"]["level"] = app.config["LOG_LEVEL_FILE"]
dictConfig(LOGGING)

# pylint: disable=no-member
@app.before_request
def log_request():
g.request_id = uuid.uuid4()
app.logger.info('> %s %s %s', g.get("request_id"), request.method,
request.full_path)
app.logger.info("> %s %s %s", g.get("request_id"), request.method, request.full_path)

@app.after_request
def log_response(response):
app.logger.info('< %s %s', g.get("request_id"), response.status)
app.logger.info("< %s %s", g.get("request_id"), response.status)
return response

# pylint: enable=no-member
Expand All @@ -220,29 +231,25 @@ def log_response(response):
app.register_blueprint(reserve.bp)

# enable optional X-Forwarded-* headers usage
if app.config['BEHIND_PROXY']:
app.wsgi_app = ProxyFix(app.wsgi_app,
x_for=1,
x_proto=1,
x_host=1,
x_prefix=1)
if app.config["BEHIND_PROXY"]:
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1)

# initialize the scheduler, only if not already running
# this is necessary when re-starting the app from tests
if scheduler.state == STATE_STOPPED:
scheduler.init_app(app)
scheduler.add_job(
func=tasks.batch_donation,
trigger='interval',
seconds=app.config['SCHEDULER_INTERVAL'],
id='batch_donation',
trigger="interval",
seconds=app.config["SCHEDULER_INTERVAL"],
id="batch_donation",
replace_existing=True,
)
scheduler.add_job(
func=tasks.random_distribution,
trigger='interval',
seconds=app.config['SCHEDULER_INTERVAL'],
id='random_distribution',
trigger="interval",
seconds=app.config["SCHEDULER_INTERVAL"],
id="random_distribution",
replace_existing=True,
)
scheduler.start()
Expand Down
Loading

0 comments on commit 6265c0e

Please sign in to comment.