|
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 |
|
21 def _bool2string(value): |
|
22 """ |
|
23 Function to convert a bool value to a setup.cfg string. |
|
24 |
|
25 @param value bool value to be converted |
|
26 @type bool |
|
27 @return setup.cfg string |
|
28 @rtype str |
|
29 """ |
|
30 return "True" if value else "False" |
|
31 |
|
32 |
|
33 def _list2string(value): |
|
34 """ |
|
35 Function to convert a list value to a setup.cfg string. |
|
36 |
|
37 @param value list value to be converted |
|
38 @type list |
|
39 @return setup.cfg string |
|
40 @rtype str |
|
41 """ |
|
42 if value: |
|
43 return "\n{0}".format("\n".join(sorted(filter(None, value)))) |
|
44 |
|
45 return "" |
|
46 |
|
47 |
|
48 def _dict2list(value): |
|
49 """ |
|
50 Function to convert a dict value to a setup.cfg list string. |
|
51 |
|
52 @param value dict value to be converted |
|
53 @type dict |
|
54 @yield setup.cfg string |
|
55 @ytype str |
|
56 """ |
|
57 for k, v in value.items(): |
|
58 yield "{0} = {1}".format(k, v) |
|
59 |
|
60 |
|
61 def _dict2string(value): |
|
62 """ |
|
63 Function to convert a dict value to a setup.cfg string. |
|
64 |
|
65 @param value dict value to be converted |
|
66 @type dict |
|
67 @return setup.cfg string |
|
68 @rtype str |
|
69 """ |
|
70 return _list2string(list(_dict2list(value))) |
|
71 |
|
72 |
|
73 def toString(value): |
|
74 """ |
|
75 Function to convert a value to a setup.cfg string. |
|
76 |
|
77 @param value value to be converted |
|
78 @type bool, list, set, tuple or dict |
|
79 @return setup.cfg string |
|
80 @rtype str |
|
81 """ |
|
82 if isinstance(value, bool): |
|
83 return _bool2string(value) |
|
84 if isinstance(value, (list, set, tuple)): |
|
85 return _list2string(value) |
|
86 if isinstance(value, dict): |
|
87 return _dict2string(value) |
|
88 return str(value).rstrip() |