Modules
Top-level package for Dev Wrangler.
cli
¶
Typer-based CLI for devwrangler.
main(req_file=<typer.models.ArgumentInfo object at 0x7f2c06576b90>, dev=<typer.models.ArgumentInfo object at 0x7f2c06576bc0>)
¶
CLI main function.
Source code in devwrangler/cli.py
def main(
req_file: str = typer.Argument('requirements-dev.txt'),
dev: bool = typer.Argument(True),
):
"""CLI main function."""
app_dir = typer.get_app_dir(APP_NAME, force_posix=True)
config_file_path: Path = Path(app_dir) / 'config.json'
# check if config data exists, if not, proceed with default config
if not config_file_path.exists():
typer.echo(f"Config file not found at {config_file_path}")
typer.echo("Using our default configuration")
config_data = parse_config_file()
else:
config_data = parse_config_file(config_file_path)
create_virtual_environment(VENV_PATH, logger=typer.echo)
set_workspace_settings(config_data, workspace_path=(PROJECT_ROOT / '.vscode'))
devwrangler
¶
Main module.
create_virtual_environment(venv_path, logger=<built-in function print>)
¶
Generate a virtual environment for the user, including special packages.
Source code in devwrangler/devwrangler.py
def create_virtual_environment(venv_path: Path, logger: Callable = print):
"""Generate a virtual environment for the user, including special packages."""
pretty_path = '/'.join(venv_path.parts[-2:])
msg = f"Generating virtual environment in {pretty_path}"
logger(msg)
try:
venv.create(
venv_path,
with_pip=True,
prompt=venv_path.parent.name,
upgrade_deps=True,
)
except TypeError:
# upgrade_deps was introduced in 3.9, without it we need to make a separate
# call to run_command
venv.create(venv_path, with_pip=True, prompt=venv_path.parent.name)
run_command(
[
str(get_py_prefix(venv_path)),
"-m",
"pip",
"install",
"-U",
"pip",
"setuptools",
]
)
get_py_prefix(venv_path)
¶
Return the appropriate prefix for the virtual environment's Python.
If platform.system() is 'Windows'¶
get_py_prefix(Path.cwd()/'.venv') Path('.venv/Scripts/python.exe')
If platform.system() is not 'Windows'¶
get_py_prefix(Path.cwd()/'.venv') Path('.venv/bin/python3')
Source code in devwrangler/devwrangler.py
def get_py_prefix(venv_path: Path) -> Path:
"""Return the appropriate prefix for the virtual environment's Python.
>>> # If platform.system() is 'Windows'
>>> get_py_prefix(Path.cwd()/'.venv')
Path('.venv/Scripts/python.exe')
>>> # If platform.system() is not 'Windows'
>>> get_py_prefix(Path.cwd()/'.venv')
Path('.venv/bin/python3')
"""
if platform.system().lower() == 'windows':
return venv_path / "Scripts" / "python.exe"
return venv_path / "bin" / "python3"
install_requirements(requirements_path=PosixPath('/home/runner/work/devwrangler/devwrangler/requirements.txt'), venv_path=PosixPath('/home/runner/work/devwrangler/devwrangler/.venv'))
¶
Install a requirements file, if it exists.
Source code in devwrangler/devwrangler.py
def install_requirements(
requirements_path: Path = Path.cwd() / 'requirements.txt',
venv_path: Path = Path.cwd() / '.venv',
):
"""Install a requirements file, if it exists."""
run_command(
[str(get_py_prefix(venv_path)), "pip", "install", "-r", str(requirements_path)]
)
parse_config_file(config_file_path=None)
¶
Parse a devwrangler JSON file.
parse_config_file(Path.home() / '.devwrangler' / 'config.json') { 'vscode': {...}, 'dev_requirements': True, ... }
Source code in devwrangler/devwrangler.py
def parse_config_file(config_file_path: Optional[Path] = None) -> Dict[str, Any]:
"""Parse a devwrangler JSON file.
>>> parse_config_file(Path.home() / '.devwrangler' / 'config.json')
{ 'vscode': {...}, 'dev_requirements': True, ... }
"""
if config_file_path is None:
return DEFAULT_VSCODE_CONFIG
else:
with open(config_file_path) as json_file:
return json.load(json_file)
run_command(command)
¶
Attempt to run a specific command in the users's shell.
run_command(['ls'])
Source code in devwrangler/devwrangler.py
def run_command(command: Sequence[str]):
"""Attempt to run a specific command in the users's shell.
>>> run_command(['ls'])
"""
print(f"{' '.join(str(part) for part in command)}")
try:
sub.run(
command,
check=True,
encoding="utf-8",
)
except sub.CalledProcessError:
warnings.warn(
(
f"Problem encountered when running `{' '.join(command)}`\n\n"
f"Review the output above to manually debug the issue"
)
)
sys.exit(1)
set_workspace_settings(config_data, workspace_path)
¶
Save the settings.json file for the project.
Source code in devwrangler/devwrangler.py
def set_workspace_settings(config_data: Dict[str, Any], workspace_path: Path):
"""Save the settings.json file for the project."""
settings_path = workspace_path / 'settings.json'
if not workspace_path.exists():
workspace_path.mkdir()
if not settings_path.exists():
with open(settings_path, mode='w') as vsc_settings:
json.dump(config_data, vsc_settings, sort_keys=True, indent=2)
else:
with open(settings_path) as f:
existing_settings = json.load(f)
config_data |= existing_settings
with open(settings_path, 'w') as vsc_settings:
json.dump(config_data, vsc_settings, sort_keys=True, indent=2)