Skip to content

Commit

Permalink
Replace cformat() with Color()
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Aug 30, 2021
1 parent 19b9a6e commit e222ae8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
18 changes: 9 additions & 9 deletions bin/maintenance/update_header.py
Expand Up @@ -14,7 +14,7 @@
import click
import yaml

from indico.util.console import cformat
from indico.vendor.colorclass import Color


# Dictionary listing the files for which to change the header.
Expand Down Expand Up @@ -127,14 +127,14 @@ def _update_header(file_path, config, substring, regex, data, ci):
if shebang_line:
content = shebang_line + '\n' + content
if content != orig_content:
msg = 'Incorrect header in {}' if ci else cformat('%{green!}Updating header of %{blue!}{}')
msg = 'Incorrect header in {}' if ci else Color('{b}{green}Updating header of {blue}{}{reset}')
print(msg.format(os.path.relpath(file_path)))
if not ci:
with open(file_path, 'w') as file_write:
file_write.write(content)
return True
elif not found:
msg = 'Missing header in {}' if ci else cformat('%{red!}Missing header%{reset} in %{blue!}{}')
msg = 'Missing header in {}' if ci else Color('{b}{red}Missing header{reset} in {b}{blue}{}{reset}')
print(msg.format(os.path.relpath(file_path)))
return True

Expand Down Expand Up @@ -174,23 +174,23 @@ def main(ctx, ci, year, path):
error = False
if path and os.path.isdir(path):
if not ci:
print(cformat('Updating headers to the year %{yellow!}{year}%{reset} for all the files in '
'%{yellow!}{path}%{reset}...').format(year=year, path=path))
print(Color('Updating headers to the year {b}{yellow}{year}{reset} for all the files in '
'{b}{yellow}{path}{reset}...').format(year=year, path=path))
for root, _, filenames in os.walk(path):
for filename in filenames:
if not blacklisted(path, root):
if update_header(os.path.join(root, filename), year, ci):
error = True
elif path and os.path.isfile(path):
if not ci:
print(cformat('Updating headers to the year %{yellow!}{year}%{reset} for the file '
'%{yellow!}{file}%{reset}...').format(year=year, file=path))
print(Color('Updating headers to the year {b}{yellow}{year}{reset} for the file '
'{b}{yellow}{file}{reset}...').format(year=year, file=path))
if update_header(path, year, ci):
error = True
else:
if not ci:
print(cformat('Updating headers to the year %{yellow!}{year}%{reset} for all '
'git-tracked files...').format(year=year))
print(Color('Updating headers to the year {b}{yellow}{year}{reset} for all '
'git-tracked files...').format(year=year))
try:
for filepath in subprocess.check_output(['git', 'ls-files'], text=True).splitlines():
filepath = os.path.abspath(filepath)
Expand Down
6 changes: 3 additions & 3 deletions bin/utils/storage_checksums.py
Expand Up @@ -15,8 +15,8 @@
from indico.core.db import db
from indico.core.storage import StoredFileMixin
from indico.modules.events.static.models.static import StaticSite, StaticSiteState
from indico.util.console import cformat
from indico.util.fs import get_file_checksum
from indico.vendor.colorclass import Color
from indico.web.flask.app import make_app


Expand Down Expand Up @@ -49,7 +49,7 @@ def query_chunked(model, chunk_size):
def main():
models = {model: make_query(model).count() for model in StoredFileMixin.__subclasses__()}
models = {model: total for model, total in models.items() if total}
labels = {model: cformat('Processing %{blue!}{}%{reset} (%{cyan}{}%{reset} rows)').format(model.__name__, total)
labels = {model: Color('Processing {b}{blue}{}{reset} ({cyan}{}{reset} rows)').format(model.__name__, total)
for model, total in models.items()}
max_length = max(len(x) for x in labels.values())
labels = {model: label.ljust(max_length) for model, label in labels.items()}
Expand All @@ -61,7 +61,7 @@ def main():
with obj.open() as f:
checksum = get_file_checksum(f)
except Exception as exc:
click.echo(cformat('\n%{red!}Could not open %{reset}%{yellow}{}%{red!}: %{reset}%{yellow!}{}')
click.echo(Color('\n{b}{red}Could not open {reset}{yellow}{}{b}{red}: {reset}{b}{yellow}{}{reset}')
.format(obj, exc))
else:
obj.md5 = checksum
Expand Down
20 changes: 10 additions & 10 deletions indico/cli/database.py
Expand Up @@ -21,7 +21,7 @@
from indico.core.db.sqlalchemy.migration import PluginScriptDirectory, migrate, prepare_db
from indico.core.db.sqlalchemy.util.management import get_all_tables
from indico.core.plugins import plugin_engine
from indico.util.console import cformat
from indico.vendor.colorclass import Color


@cli_group()
Expand Down Expand Up @@ -102,19 +102,19 @@ def reset_alembic():

def _safe_downgrade(*args, **kwargs):
func = kwargs.pop('_func')
print(cformat('%{yellow!}*** DANGER'))
print(cformat('%{yellow!}***%{reset} '
'%{red!}This operation may %{yellow!}PERMANENTLY ERASE %{red!}some data!%{reset}'))
print(Color('{b}{yellow}*** DANGER{reset}'))
print(Color('{b}{yellow}***{reset} '
'{b}{red}This operation may {b}{yellow}PERMANENTLY ERASE {b}{red}some data!{reset}'))
if current_app.debug:
skip_confirm = os.environ.get('INDICO_ALWAYS_DOWNGRADE', '').lower() in ('1', 'yes')
print(cformat('%{yellow!}***%{reset} '
"%{green!}Debug mode is active, so you probably won't destroy valuable data"))
print(Color('{b}{yellow}***{reset} '
"{b}{green}Debug mode is active, so you probably won't destroy valuable data{reset}"))
else:
skip_confirm = False
print(cformat('%{yellow!}***%{reset} '
'%{red!}Debug mode is NOT ACTIVE, so make sure you are on the right machine!'))
if not skip_confirm and input(cformat('%{yellow!}***%{reset} '
'To confirm this, enter %{yellow!}YES%{reset}: ')) != 'YES':
print(Color('{b}{yellow}***{reset} '
'{b}{red}Debug mode is NOT ACTIVE, so make sure you are on the right machine!{reset}'))
if not skip_confirm and input(Color('{b}{yellow}***{reset} '
'To confirm this, enter {b}{yellow}YES{reset}: ')) != 'YES':
click.secho('Aborted', fg='green')
sys.exit(1)
else:
Expand Down
4 changes: 2 additions & 2 deletions indico/cli/devserver.py
Expand Up @@ -37,8 +37,8 @@ def run_watchman():
try:
Watchman().run()
except pywatchman.WatchmanError as exc:
from indico.util.console import cformat
print(cformat('%{red!}watchman error: {}').format(exc))
from indico.vendor.colorclass import Color
print(Color('{b}{red}watchman error: {}{reset}').format(exc))


def run_server(info, host, port, url, ssl, ssl_key, ssl_cert, quiet, proxy, enable_evalex, evalex_from, reloader_type):
Expand Down
4 changes: 2 additions & 2 deletions indico/cli/i18n.py
Expand Up @@ -20,7 +20,7 @@
from babel.messages.pofile import read_po
from flask.helpers import get_root_path

from indico.util.console import cformat
from indico.vendor.colorclass import Color


@click.group()
Expand Down Expand Up @@ -181,7 +181,7 @@ def check_format_strings():
all_valid = False
click.echo(f'Found invalid format strings in {os.path.relpath(path, root_path)}')
for item in invalid:
click.echo(cformat('%{yellow}{}%{reset} | %{yellow!}{}%{reset}\n%{red}{}%{reset} != %{red!}{}%{reset}')
click.echo(Color('{yellow}{}{reset} | {b}{yellow}{}{reset}\n{red}{}{reset} != {b}{red}{}{reset}')
.format(item['orig'], item['trans'],
list(item['orig_placeholders']), list(item['trans_placeholders'])))
click.echo()
Expand Down

0 comments on commit e222ae8

Please sign in to comment.