Skip to content

Commit

Permalink
chore: Reduce single use variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Oct 11, 2024
1 parent c6a1c3a commit 4e7fb07
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
13 changes: 5 additions & 8 deletions scrapyd_client/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,34 @@ def spiders(args):
def parse_cli_args(args):
cfg = get_config()

target_default = cfg.get("deploy", "url", fallback=DEFAULT_TARGET_URL).rstrip("/")
username_default = cfg.get("deploy", "username", fallback=None)
password_default = cfg.get("deploy", "password", fallback=None)
project_default = cfg.get("deploy", "project", fallback=None)
project_kwargs = {
"metavar": "PROJECT",
"required": True,
"help": "Specifies the project, can be a globbing pattern.",
}
if project_default:
if project_default := cfg.get("deploy", "project", fallback=None):
project_kwargs["default"] = project_default

description = "A command line interface for Scrapyd."
mainparser = ArgumentParser(description=description)
subparsers = mainparser.add_subparsers()

mainparser.add_argument(
"-t",
"--target",
default=target_default,
default=cfg.get("deploy", "url", fallback=DEFAULT_TARGET_URL).rstrip("/"),
help="Specifies the Scrapyd's API base URL.",
)
mainparser.add_argument(
"-u",
"--username",
default=username_default,
default=cfg.get("deploy", "username", fallback=None),
help="Specifies the username to connect to the Scrapyd target.",
)
mainparser.add_argument(
"-p",
"--password",
default=password_default,
default=cfg.get("deploy", "password", fallback=None),
help="Specifies the password to connect to the Scrapyd target.",
)

Expand Down
17 changes: 4 additions & 13 deletions scrapyd_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import netrc
import os
from configparser import BasicInterpolation, ConfigParser
Expand All @@ -17,19 +19,8 @@ def before_get(self, parser, section, option, value, defaults):
return os.path.expandvars(value)


def get_auth(url, username, password):
"""
Retrieve authentication from arguments or infers from .netrc.
:param url: The URL to check.
:type url: str
:param username: The username to use.
:type username: str
:param password: The password to use.
:type password: str
:returns: An HTTPBasicAuth object or None.
:rtype: requests.auth.HTTPBasicAuth or None
"""
def get_auth(url: str, username: str, password: str) -> HTTPBasicAuth | None:
"""Retrieve authentication from arguments or infers from .netrc."""
if username:
return HTTPBasicAuth(username=username, password=password)

Expand Down

0 comments on commit 4e7fb07

Please sign in to comment.