-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpack.py
289 lines (229 loc) · 9.34 KB
/
pack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from string import Formatter
import json
import urllib.request
from urllib.error import HTTPError
import re
from typing import Union, Mapping, Iterable, Generator
from configparser import ConfigParser
import os
DFLT_CONFIG_FILE = 'setup.cfg'
DFLT_CONFIG_SECTION = 'metadata'
# TODO: postprocess_ini_section_items and preprocess_ini_section_items: Add comma separated possibility?
# TODO: Find out if configparse has an option to do this processing alreadys
def postprocess_ini_section_items(items: Union[Mapping, Iterable]) -> Generator:
r"""Transform newline-separated string values into actual list of strings (assuming that intent)
>>> section_from_ini = {
... 'name': 'epythet',
... 'keywords': '\n\tdocumentation\n\tpackaging\n\tpublishing'
... }
>>> section_for_python = dict(postprocess_ini_section_items(section_from_ini))
>>> section_for_python
{'name': 'epythet', 'keywords': ['documentation', 'packaging', 'publishing']}
"""
splitter_re = re.compile('[\n\r\t]+')
if isinstance(items, Mapping):
items = items.items()
for k, v in items:
if v.startswith('\n'):
v = splitter_re.split(v[1:])
v = [vv.strip() for vv in v if vv.strip()]
v = [vv for vv in v if not vv.startswith('#')] # remove commented lines
yield k, v
# TODO: Find out if configparse has an option to do this processing alreadys
def preprocess_ini_section_items(items: Union[Mapping, Iterable]) -> Generator:
"""Transform list values into newline-separated strings, in view of writing the value to a ini formatted section
>>> section = {
... 'name': 'epythet',
... 'keywords': ['documentation', 'packaging', 'publishing']
... }
>>> for_ini = dict(preprocess_ini_section_items(section))
>>> print('keywords =' + for_ini['keywords']) # doctest: +NORMALIZE_WHITESPACE
keywords =
documentation
packaging
publishing
"""
if isinstance(items, Mapping):
items = items.items()
for k, v in items:
if isinstance(v, list):
v = '\n\t' + '\n\t'.join(v)
yield k, v
def read_configs(
config_file=DFLT_CONFIG_FILE,
section=DFLT_CONFIG_SECTION,
postproc=postprocess_ini_section_items):
c = ConfigParser()
c.read_file(open(config_file, 'r'))
if section is None:
d = dict(c)
if postproc:
d = {k: dict(postproc(v)) for k, v in c}
else:
d = dict(c[section])
if postproc:
d = dict(postproc(d))
return d
def write_configs(
configs,
config_file=DFLT_CONFIG_FILE,
section=DFLT_CONFIG_SECTION,
preproc=preprocess_ini_section_items
):
c = ConfigParser()
if os.path.isfile(config_file):
c.read_file(open(config_file, 'r'))
c[section] = dict(preproc(configs))
with open(config_file, 'w') as fp:
c.write(fp)
dflt_formatter = Formatter()
def increment_version(version_str):
version_nums = list(map(int, version_str.split('.')))
version_nums[-1] += 1
return '.'.join(map(str, version_nums))
DLFT_PYPI_PACKAGE_JSON_URL_TEMPLATE = 'https://pypi.python.org/pypi/{package}/json'
# TODO: Perhaps there's a safer way to analyze errors (and determine if the package exists or other HTTPError)
def current_pypi_version(
package: str,
url_template=DLFT_PYPI_PACKAGE_JSON_URL_TEMPLATE
) -> Union[str, None]:
"""
Return version of package on pypi.python.org using json.
```
> get_version('py2store')
'0.0.7'
```
:param package: Name of the package
:return: A version (string) or None if there was an exception (usually means there
"""
req = urllib.request.Request(url_template.format(package=package))
try:
r = urllib.request.urlopen(req)
if r.code == 200:
t = json.loads(r.read())
releases = t.get('releases', [])
if releases:
return sorted(releases, key=lambda r: tuple(map(int, r.split('.'))))[-1]
else:
raise ValueError(f"response code was {r.code}")
except HTTPError:
return None # to indicate (hopefully) that name doesn't exist
except Exception:
raise
def next_version_for_package(
package: str,
url_template=DLFT_PYPI_PACKAGE_JSON_URL_TEMPLATE,
version_if_current_version_none='0.0.1'
) -> str:
current_version = current_pypi_version(package, url_template)
if current_version is not None:
return increment_version(current_version)
else:
return version_if_current_version_none
def my_setup(**setup_kwargs):
from setuptools import setup
import json
print("Setup params -------------------------------------------------------")
print(json.dumps(setup_kwargs, indent=2))
print("--------------------------------------------------------------------")
setup(**setup_kwargs)
def ujoin(*args):
"""Join strings with the url seperator (/).
Note that will add a / where it's missing (as in between 'https://pypi.org' and 'project/'),
and only use one if two consecutive tokens use respectively end and start with a /
(as in 'project/' and '/pipoke/').
>>> ujoin('https://pypi.org', 'project/', '/pipoke/')
'https://pypi.org/project/pipoke/'
Extremal cases
>>> ujoin('https://pypi.org')
'https://pypi.org'
>>> ujoin('https://pypi.org/')
'https://pypi.org/'
>>> ujoin('')
''
>>> ujoin()
''
"""
if len(args) == 0 or len(args[0]) == 0:
return ''
return ((args[0][0] == '/') * '/' # prepend slash if first arg starts with it
+ '/'.join(x[(x[0] == '/'):(len(x) - (x[-1] == '/'))] for x in args)
+ (args[-1][-1] == '/') * '/') # append slash if last arg ends with it
########### Partial and incremental formatting #########################################################################
class PartialFormatter(Formatter):
"""A string formatter that won't complain if the fields are only partially formatted.
But note that you will lose the spec part of your template (e.g. in {foo:1.2f}, you'll loose the 1.2f
if not foo is given -- but {foo} will remain).
"""
def get_value(self, key, args, kwargs):
try:
return super().get_value(key, args, kwargs)
except KeyError:
return '{' + key + '}'
def format_fields_set(self, s):
return {x[1] for x in self.parse(s) if x[1]}
partial_formatter = PartialFormatter()
# TODO: For those who love algorithmic optimization, there's some wasted to cut out here below.
def _unformatted(d):
for k, v in d.items():
if isinstance(v, str) and len(partial_formatter.format_fields_set(v)) > 0:
yield k
def _fields_to_format(d):
for k, v in d.items():
if isinstance(v, str):
yield from partial_formatter.format_fields_set(v)
def format_str_vals_of_dict(d, *, max_formatting_loops=10, **kwargs):
"""
:param d:
:param max_formatting_loops:
:param kwargs:
:return:
>>> d = {
... 'filepath': '{root}/{file}.{ext}',
... 'ext': 'txt'
... }
>>> format_str_vals_of_dict(d, root='ROOT', file='FILE')
{'filepath': 'ROOT/FILE.txt', 'ext': 'txt'}
Note that if the input mapping `d` and the kwargs have a conflict, the mapping version is used!
>>> format_str_vals_of_dict(d, root='ROOT', file='FILE', ext='will_not_be_used')
{'filepath': 'ROOT/FILE.txt', 'ext': 'txt'}
But if you want to override an input mapping, you can -- the usual way:
>>> format_str_vals_of_dict(dict(d, ext='will_be_used'), root='ROOT', file='FILE')
{'filepath': 'ROOT/FILE.will_be_used', 'ext': 'will_be_used'}
If you don't provide enough fields to satisfy all the format fields in the values of `d`,
you'll be told to bugger off.
>>> format_str_vals_of_dict(d, root='ROOT')
Traceback (most recent call last):
...
ValueError: I won't be able to complete that. You'll need to provide the values for:
file
And it's recursive...
>>> d = {
... 'filepath': '{root}/{filename}',
... 'filename': '{file}.{ext}'
... }
>>> my_configs = {'root': 'ROOT', 'file': 'FILE', 'ext': 'EXT'}
>>> format_str_vals_of_dict(d, **my_configs)
{'filepath': 'ROOT/FILE.EXT', 'filename': 'FILE.EXT'}
# TODO: Could make the above work if filename is give, but not file nor ext! At least as an option.
"""
d = dict(**d) # make a shallow copy
# The defaults (kwargs) cannot overlap with any keys of d, so:
kwargs = {k: kwargs[k] for k in set(kwargs) - set(d)}
provided_fields = set(d) | set(kwargs)
missing_fields = set(_fields_to_format(d)) - provided_fields
if missing_fields:
raise ValueError("I won't be able to complete that. You'll need to provide the values for:\n" +
f" {', '.join(missing_fields)}")
for i in range(max_formatting_loops):
unformatted = set(_unformatted(d))
if unformatted:
for k in unformatted:
d[k] = partial_formatter.format(d[k], **kwargs, **d)
else:
break
else:
raise ValueError(f"There are still some unformatted fields, "
f"but I reached my max {max_formatting_loops} allowed loops. " +
f"Those fields are: {set(_fields_to_format(d)) - (set(d) | set(kwargs))}")
return d