|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing some utility functions for setup.cfg creation. |
|
8 """ |
|
9 |
|
10 # |
|
11 # http://setuptools.readthedocs.io/en/latest/setuptools.html#specifying-values |
|
12 # |
|
13 # str - simple string |
|
14 # list-comma - dangling list or string of comma-separated values |
|
15 # list-semi - dangling list or string of semicolon-separated values |
|
16 # bool - True is 1, yes, true |
|
17 # dict - list-comma where keys are separated from values by = |
|
18 # |
|
19 |
|
20 def _bool2string(value): |
|
21 """ |
|
22 Function to convert a bool value to a setup.cfg string. |
|
23 |
|
24 @param value bool value to be converted |
|
25 @type bool |
|
26 @return setup.cfg string |
|
27 @rtype str |
|
28 """ |
|
29 return "True" if value else "False" |
|
30 |
|
31 |
|
32 def _list2string(value): |
|
33 """ |
|
34 Function to convert a list value to a setup.cfg string. |
|
35 |
|
36 @param value list value to be converted |
|
37 @type list |
|
38 @return setup.cfg string |
|
39 @rtype str |
|
40 """ |
|
41 if value: |
|
42 return "\n{0}".format("\n".join(sorted(filter(None, value)))) |
|
43 |
|
44 |
|
45 def _dict2list(value): |
|
46 """ |
|
47 Function to convert a dict value to a setup.cfg list string. |
|
48 |
|
49 @param value dict value to be converted |
|
50 @type dict |
|
51 @yield setup.cfg string |
|
52 @ytype str |
|
53 """ |
|
54 for k, v in value.items(): |
|
55 yield "{0} = {1}".format(k, v) |
|
56 |
|
57 |
|
58 def _dict2string(value): |
|
59 """ |
|
60 Function to convert a dict value to a setup.cfg string. |
|
61 |
|
62 @param value dict value to be converted |
|
63 @type dict |
|
64 @return setup.cfg string |
|
65 @rtype str |
|
66 """ |
|
67 return _list2string(list(_dict2list(value))) |
|
68 |
|
69 def toString(value): |
|
70 """ |
|
71 Function to convert a value to a setup.cfg string |
|
72 |
|
73 @param value value to be converted |
|
74 @type bool, list, set, tuple or dict |
|
75 @return setup.cfg string |
|
76 @rtype str |
|
77 """ |
|
78 if isinstance(value, bool): |
|
79 return _bool2string(value) |
|
80 if isinstance(value, (list, set, tuple)): |
|
81 return _list2string(value) |
|
82 if isinstance(value, dict): |
|
83 return _dict2string(value) |
|
84 return str(value).rstrip() |