Coverage for /home/pradyumna/Languages/python/packages/pyprojstencil/pyprojstencil/command_line.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python3
2# -*- coding:utf-8; mode:python; -*-
3#
4# Copyright 2021 Pradyumna Paranjape
5# This file is part of pyprojstencil.
6#
7# pyprojstencil is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Lesser General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# pyprojstencil is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with pyprojstencil. If not, see <https://www.gnu.org/licenses/>.
19#
20"""
21Command line inputs
22"""
24from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
25from pathlib import Path
27from argcomplete import autocomplete
29from pyprojstencil.configure import PyConfig, read_config
30from pyprojstencil.errors import NoProjectNameError
31from pyprojstencil.get_license import get_license
34def project_path(project: str) -> Path:
35 """
36 Check whether parent exists
38 Args:
39 project: path of project
41 Returns:
42 resolved ``Path`` to project
44 Raises:
45 FileExistsError: project already exists
46 """
47 if Path(project).exists():
48 raise FileExistsError(f'{project} already exists')
49 return Path(project).resolve()
52def filepath(location: str) -> Path:
53 """
54 Check whether the given location is a valid path to a file
56 Args:
57 location: Path to be validated
59 Returns:
60 ``Path`` for location
62 Raises:
63 FileNotFoundError: location is not an existing file
64 """
65 location_path = Path(location)
66 if location_path.is_file():
67 return location_path
68 raise FileNotFoundError(f"{location} was not found")
71def _cli() -> ArgumentParser:
72 """
73 Accept command line arguments
74 """
75 description = '''
76 '''
77 parser = ArgumentParser(description=description,
78 formatter_class=RawDescriptionHelpFormatter)
79 parser.add_argument('-c',
80 '--config',
81 help='pre-defined configuration',
82 default=None,
83 type=filepath)
84 parser.add_argument('-l',
85 '--license',
86 default='LGPLv3',
87 type=str,
88 help='License name or "custom" [default: LGPLv3]')
89 parser.add_argument('-y',
90 '--years',
91 default=None,
92 type=str,
93 help='copyright period e.g. 2020-2021')
94 parser.add_argument('-a',
95 '--author',
96 default=None,
97 type=str,
98 help='Name of author')
99 parser.add_argument('-p',
100 '--python-exe',
101 default=None,
102 dest='pyversion',
103 type=str,
104 help='Python version in virtual environment [eg. 3.9]')
105 parser.add_argument('project',
106 type=project_path,
107 help="Name of python project")
108 autocomplete(parser)
109 return parser
112def cli() -> Namespace:
113 """
114 Command line arguments
115 """
116 parser = _cli()
117 return parser.parse_args()
120def configure() -> PyConfig:
121 """
122 Set configuration variables
124 Returns:
125 Configuration variables object
127 Raises:
128 NoProjectNameError: project name missing
129 """
130 cli_inputs = cli()
131 cli_inputs.license, cli_inputs.license_header = get_license(
132 cli_inputs.license)
133 project = cli_inputs.project
134 if project is None:
135 raise NoProjectNameError
136 delattr(cli_inputs, 'project')
137 config = read_config(project=project, **vars(cli_inputs))
138 if config.author is None:
139 raise NoProjectNameError
140 return config