hassle.hassle_config

  1import argparse
  2
  3from pathier import Pathier
  4
  5root = Pathier(__file__).parent
  6
  7
  8def get_args() -> argparse.Namespace:
  9    parser = argparse.ArgumentParser()
 10
 11    parser.add_argument(
 12        "-n",
 13        "--name",
 14        type=str,
 15        default=None,
 16        help=""" Your name. This will be used to populate the 'authors' field of a packages 'pyproject.toml'. """,
 17    )
 18
 19    parser.add_argument(
 20        "-e",
 21        "--email",
 22        type=str,
 23        default=None,
 24        help=""" Your email. This will be used to populate the 'authors' field of a packages 'pyproject.toml'. """,
 25    )
 26
 27    parser.add_argument(
 28        "-g",
 29        "--github_username",
 30        type=str,
 31        default=None,
 32        help=""" Your github account name. When creating a new package,
 33        say with the name 'mypackage', the pyproject.toml 'Homepage' field
 34        will be set to 'https://github.com/{github_username}/mypackage'
 35        and the 'Source code' field will be set to
 36        'https://github.com/{github_username}/mypackage/tree/main/src/mypackage'.""",
 37    )
 38
 39    parser.add_argument(
 40        "-d",
 41        "--docs_url",
 42        type=str,
 43        default=None,
 44        help=""" The template url to be used in your pyproject.toml file
 45        indicating where your project docs will be hosted.
 46        Pass the url such that the spot the actual package name will go is
 47        held by '$name', e.g. 'https://somedocswebsite/user/projects/$name'.
 48        If 'hassle_config.toml' didn't exist prior to running this tool and nothing
 49        is given for this arg, it will default to using the package's github
 50        url. e.g. for package 'mypackage' the url will be 
 51        'https://github.com/{your_github_name}/mypackage/tree/main/docs' """,
 52    )
 53
 54    parser.add_argument(
 55        "-t",
 56        "--tag_prefix",
 57        type=str,
 58        default=None,
 59        help=""" The tag prefix to use with git when tagging source code versions.
 60        e.g. hassle will use the current version in your pyproject.toml file to when 
 61        adding a git tag. If you've passed 'v' to this arg and the version of your
 62        hypothetical package is '1.0.1', it will be tagged as 'v1.0.1'.
 63        If 'hassle_config.toml' didn't exist prior to running this tool and you
 64        don't pass anything for this arg, it will default to ''.""",
 65    )
 66
 67    args = parser.parse_args()
 68
 69    return args
 70
 71
 72def config_exists() -> bool:
 73    """Check if hassle_config.toml exists."""
 74    return (root / "hassle_config.toml").exists()
 75
 76
 77def load_config() -> dict:
 78    "Load and return hassle_config contents if it exists."
 79    if config_exists():
 80        return (root / "hassle_config.toml").loads()
 81    else:
 82        raise FileNotFoundError(
 83            f"load_config() could not find {root/'hassle_config.toml'}.\nRun hassle_config to set it."
 84        )
 85
 86
 87def write_config(config: dict):
 88    """Dump config to "hassle_config.toml."""
 89    (root / "hassle_config.toml").dumps(config)
 90
 91
 92def warn():
 93    print("hassle_config.toml has not been set.")
 94    print("Run hassle_config to set it.")
 95    print("Run 'hassle_config -h' for help.")
 96
 97
 98def create_config(
 99    name: str = None,
100    email: str = None,
101    github_username: str = None,
102    docs_url: str = None,
103    tag_prefix: str = None,
104):
105    """Create hassle_config.toml from given args."""
106    print(f"Manual edits can be made at {root/'hassle_config.toml'}")
107    if not config_exists():
108        config = {}
109        if name and email:
110            config["authors"] = [{"name": name, "email": email}]
111        elif name:
112            config["authors"] = [{"name": name}]
113        elif email:
114            config["authors"] = [{"email": email}]
115        else:
116            # In case anything upstream would fail if nothing is present for 'authors'
117            config["authors"] = [{"name": "", "email": ""}]
118        config["project_urls"] = {
119            "Homepage": "",
120            "Documentation": "",
121            "Source code": "",
122        }
123        config["git"] = {}
124    else:
125        config = load_config()
126        if name and email:
127            config["authors"].append({"name": name, "email": email})
128        elif name:
129            config["authors"].append({"name": name})
130        elif email:
131            config["authors"].append({"email": email})
132
133    if github_username:
134        config["project_urls"][
135            "Homepage"
136        ] = f"https://github.com/{github_username}/$name"
137        config["project_urls"][
138            "Source code"
139        ] = f"{config['project_urls']['Homepage']}/tree/main/src/$name"
140
141    if docs_url:
142        config["project_urls"]["Documentation"] = docs_url
143    elif github_username and config["project_urls"]["Documentation"] == "":
144        config["project_urls"][
145            "Documentation"
146        ] = f"https://github.com/{github_username}/$name/tree/main/docs"
147
148    if tag_prefix:
149        config["git"]["tag_prefix"] = tag_prefix
150    elif not config_exists() and not tag_prefix:
151        config["git"]["tag_prefix"] = ""
152
153    if config:
154        write_config(config)
155
156
157def main(args: argparse.Namespace = None):
158    if not args:
159        args = get_args()
160    create_config(
161        args.name, args.email, args.github_username, args.docs_url, args.tag_prefix
162    )
163
164
165if __name__ == "__main__":
166    main(get_args())
def get_args() -> argparse.Namespace:
 9def get_args() -> argparse.Namespace:
10    parser = argparse.ArgumentParser()
11
12    parser.add_argument(
13        "-n",
14        "--name",
15        type=str,
16        default=None,
17        help=""" Your name. This will be used to populate the 'authors' field of a packages 'pyproject.toml'. """,
18    )
19
20    parser.add_argument(
21        "-e",
22        "--email",
23        type=str,
24        default=None,
25        help=""" Your email. This will be used to populate the 'authors' field of a packages 'pyproject.toml'. """,
26    )
27
28    parser.add_argument(
29        "-g",
30        "--github_username",
31        type=str,
32        default=None,
33        help=""" Your github account name. When creating a new package,
34        say with the name 'mypackage', the pyproject.toml 'Homepage' field
35        will be set to 'https://github.com/{github_username}/mypackage'
36        and the 'Source code' field will be set to
37        'https://github.com/{github_username}/mypackage/tree/main/src/mypackage'.""",
38    )
39
40    parser.add_argument(
41        "-d",
42        "--docs_url",
43        type=str,
44        default=None,
45        help=""" The template url to be used in your pyproject.toml file
46        indicating where your project docs will be hosted.
47        Pass the url such that the spot the actual package name will go is
48        held by '$name', e.g. 'https://somedocswebsite/user/projects/$name'.
49        If 'hassle_config.toml' didn't exist prior to running this tool and nothing
50        is given for this arg, it will default to using the package's github
51        url. e.g. for package 'mypackage' the url will be 
52        'https://github.com/{your_github_name}/mypackage/tree/main/docs' """,
53    )
54
55    parser.add_argument(
56        "-t",
57        "--tag_prefix",
58        type=str,
59        default=None,
60        help=""" The tag prefix to use with git when tagging source code versions.
61        e.g. hassle will use the current version in your pyproject.toml file to when 
62        adding a git tag. If you've passed 'v' to this arg and the version of your
63        hypothetical package is '1.0.1', it will be tagged as 'v1.0.1'.
64        If 'hassle_config.toml' didn't exist prior to running this tool and you
65        don't pass anything for this arg, it will default to ''.""",
66    )
67
68    args = parser.parse_args()
69
70    return args
def config_exists() -> bool:
73def config_exists() -> bool:
74    """Check if hassle_config.toml exists."""
75    return (root / "hassle_config.toml").exists()

Check if hassle_config.toml exists.

def load_config() -> dict:
78def load_config() -> dict:
79    "Load and return hassle_config contents if it exists."
80    if config_exists():
81        return (root / "hassle_config.toml").loads()
82    else:
83        raise FileNotFoundError(
84            f"load_config() could not find {root/'hassle_config.toml'}.\nRun hassle_config to set it."
85        )

Load and return hassle_config contents if it exists.

def write_config(config: dict):
88def write_config(config: dict):
89    """Dump config to "hassle_config.toml."""
90    (root / "hassle_config.toml").dumps(config)

Dump config to "hassle_config.toml.

def warn():
93def warn():
94    print("hassle_config.toml has not been set.")
95    print("Run hassle_config to set it.")
96    print("Run 'hassle_config -h' for help.")
def create_config( name: str = None, email: str = None, github_username: str = None, docs_url: str = None, tag_prefix: str = None):
 99def create_config(
100    name: str = None,
101    email: str = None,
102    github_username: str = None,
103    docs_url: str = None,
104    tag_prefix: str = None,
105):
106    """Create hassle_config.toml from given args."""
107    print(f"Manual edits can be made at {root/'hassle_config.toml'}")
108    if not config_exists():
109        config = {}
110        if name and email:
111            config["authors"] = [{"name": name, "email": email}]
112        elif name:
113            config["authors"] = [{"name": name}]
114        elif email:
115            config["authors"] = [{"email": email}]
116        else:
117            # In case anything upstream would fail if nothing is present for 'authors'
118            config["authors"] = [{"name": "", "email": ""}]
119        config["project_urls"] = {
120            "Homepage": "",
121            "Documentation": "",
122            "Source code": "",
123        }
124        config["git"] = {}
125    else:
126        config = load_config()
127        if name and email:
128            config["authors"].append({"name": name, "email": email})
129        elif name:
130            config["authors"].append({"name": name})
131        elif email:
132            config["authors"].append({"email": email})
133
134    if github_username:
135        config["project_urls"][
136            "Homepage"
137        ] = f"https://github.com/{github_username}/$name"
138        config["project_urls"][
139            "Source code"
140        ] = f"{config['project_urls']['Homepage']}/tree/main/src/$name"
141
142    if docs_url:
143        config["project_urls"]["Documentation"] = docs_url
144    elif github_username and config["project_urls"]["Documentation"] == "":
145        config["project_urls"][
146            "Documentation"
147        ] = f"https://github.com/{github_username}/$name/tree/main/docs"
148
149    if tag_prefix:
150        config["git"]["tag_prefix"] = tag_prefix
151    elif not config_exists() and not tag_prefix:
152        config["git"]["tag_prefix"] = ""
153
154    if config:
155        write_config(config)

Create hassle_config.toml from given args.

def main(args: argparse.Namespace = None):
158def main(args: argparse.Namespace = None):
159    if not args:
160        args = get_args()
161    create_config(
162        args.name, args.email, args.github_username, args.docs_url, args.tag_prefix
163    )