Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /elisiariocouto_leggen /leggen /main.py
| import os | |
| import sys | |
| from gettext import gettext as _ | |
| from pathlib import Path | |
| import click | |
| from loguru import logger | |
| from leggen.utils.config import config as config_singleton | |
| from leggen.utils.paths import path_manager | |
| from leggen.utils.text import error | |
| cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "commands")) | |
| class Group(click.Group): | |
| # Overriden version to support sections | |
| def format_commands(self, ctx, formatter): | |
| commands = [] | |
| for subcommand in self.list_commands(ctx): | |
| if subcommand.startswith("GROUP_"): | |
| cmd = self.get_command(ctx, subcommand[6:]) | |
| else: | |
| cmd = self.get_command(ctx, subcommand) | |
| # What is this, the tool lied about a command. Ignore it | |
| if cmd is None: | |
| continue | |
| if cmd.hidden: | |
| continue | |
| commands.append((subcommand, cmd)) | |
| # allow for 3 times the default spacing | |
| if len(commands): | |
| limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands) | |
| rows = [] | |
| groups = [] | |
| for subcommand, cmd in commands: | |
| help = cmd.get_short_help_str(limit) | |
| if subcommand.startswith("GROUP_"): | |
| groups.append((subcommand[6:], help)) | |
| else: | |
| rows.append((subcommand, help)) | |
| if groups: | |
| with formatter.section(_("Command Groups")): | |
| formatter.write_dl(groups) | |
| if rows: | |
| with formatter.section(_("Commands")): | |
| formatter.write_dl(rows) | |
| def list_commands(self, ctx): | |
| commands = [] | |
| groups = [] | |
| for filename in os.listdir(cmd_folder): | |
| if filename.endswith(".py") and not filename.startswith("__init__"): | |
| commands.append(filename[:-3]) | |
| if not filename.endswith(".py"): | |
| for group_filename in os.listdir(os.path.join(cmd_folder, filename)): | |
| if group_filename == "__init__.py": | |
| groups.append(f"GROUP_{filename}") | |
| commands.sort() | |
| groups.sort() | |
| return groups + commands | |
| def get_command(self, ctx, name): | |
| name = name.replace("-", "_") | |
| try: | |
| mod = __import__(f"leggen.commands.{name}", None, None, [name]) | |
| except ImportError as e: | |
| error(f"Can't import command {name}. Exception: {e}") | |
| return | |
| return getattr(mod, name, None) | |
| def cli( | |
| ctx: click.Context, | |
| config: str | None, | |
| config_dir: Path, | |
| database: Path, | |
| api_url: str, | |
| api_key: str | None, | |
| log_level: str, | |
| no_verify_ssl: bool, | |
| ): | |
| """ | |
| Leggen: An Open Banking CLI | |
| """ | |
| # Configure loguru log level | |
| logger.remove() # Remove default handler | |
| logger.add( | |
| sys.stderr, | |
| level=log_level.upper(), | |
| format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>", | |
| ) | |
| # Record user-provided paths; nothing is loaded here. The config file is | |
| # read lazily by the singleton when something actually needs it (the | |
| # server lifespan or the CLI's api-key fallback), so help output and the | |
| # bootstrap commands never require one. | |
| if config_dir: | |
| path_manager.set_config_dir(config_dir) | |
| if database: | |
| path_manager.set_database_path(database) | |
| if config: | |
| config_singleton.set_config_path(config) | |
| # Store CLI options in context for commands to use. The api_key config | |
| # fallback happens lazily in LeggenAPIClient.from_context. | |
| ctx.ensure_object(dict) | |
| ctx.obj["api_url"] = api_url | |
| ctx.obj["verify_ssl"] = not no_verify_ssl | |
| ctx.obj["log_level"] = log_level.lower() | |
| ctx.obj["api_key"] = api_key | |
Xet Storage Details
- Size:
- 5.7 kB
- Xet hash:
- 208e424c1d966c5aa6edcfbf2e62bf9f9e4611f4707e88a520dd0656d1fe0ee9
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.