summaryrefslogtreecommitdiff
path: root/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/pytoml/writer.py
diff options
context:
space:
mode:
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/pytoml/writer.py')
-rw-r--r--venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/pytoml/writer.py127
1 files changed, 0 insertions, 127 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/pytoml/writer.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/pytoml/writer.py
deleted file mode 100644
index 19a8c6e..0000000
--- a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_vendor/pytoml/writer.py
+++ /dev/null
@@ -1,127 +0,0 @@
1from __future__ import unicode_literals
2import io, datetime, math, sys
3
4if sys.version_info[0] == 3:
5 long = int
6 unicode = str
7
8
9def dumps(obj, sort_keys=False):
10 fout = io.StringIO()
11 dump(obj, fout, sort_keys=sort_keys)
12 return fout.getvalue()
13
14
15_escapes = {'\n': 'n', '\r': 'r', '\\': '\\', '\t': 't', '\b': 'b', '\f': 'f', '"': '"'}
16
17
18def _escape_string(s):
19 res = []
20 start = 0
21
22 def flush():
23 if start != i:
24 res.append(s[start:i])
25 return i + 1
26
27 i = 0
28 while i < len(s):
29 c = s[i]
30 if c in '"\\\n\r\t\b\f':
31 start = flush()
32 res.append('\\' + _escapes[c])
33 elif ord(c) < 0x20:
34 start = flush()
35 res.append('\\u%04x' % ord(c))
36 i += 1
37
38 flush()
39 return '"' + ''.join(res) + '"'
40
41
42def _escape_id(s):
43 if any(not c.isalnum() and c not in '-_' for c in s):
44 return _escape_string(s)
45 return s
46
47
48def _format_list(v):
49 return '[{0}]'.format(', '.join(_format_value(obj) for obj in v))
50
51# Formula from:
52# https://docs.python.org/2/library/datetime.html#datetime.timedelta.total_seconds
53# Once support for py26 is dropped, this can be replaced by td.total_seconds()
54def _total_seconds(td):
55 return ((td.microseconds
56 + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6)
57
58def _format_value(v):
59 if isinstance(v, bool):
60 return 'true' if v else 'false'
61 if isinstance(v, int) or isinstance(v, long):
62 return unicode(v)
63 if isinstance(v, float):
64 if math.isnan(v) or math.isinf(v):
65 raise ValueError("{0} is not a valid TOML value".format(v))
66 else:
67 return repr(v)
68 elif isinstance(v, unicode) or isinstance(v, bytes):
69 return _escape_string(v)
70 elif isinstance(v, datetime.datetime):
71 offs = v.utcoffset()
72 offs = _total_seconds(offs) // 60 if offs is not None else 0
73
74 if offs == 0:
75 suffix = 'Z'
76 else:
77 if offs > 0:
78 suffix = '+'
79 else:
80 suffix = '-'
81 offs = -offs
82 suffix = '{0}{1:.02}{2:.02}'.format(suffix, offs // 60, offs % 60)
83
84 if v.microsecond:
85 return v.strftime('%Y-%m-%dT%H:%M:%S.%f') + suffix
86 else:
87 return v.strftime('%Y-%m-%dT%H:%M:%S') + suffix
88 elif isinstance(v, list):
89 return _format_list(v)
90 else:
91 raise RuntimeError(v)
92
93
94def dump(obj, fout, sort_keys=False):
95 tables = [((), obj, False)]
96
97 while tables:
98 name, table, is_array = tables.pop()
99 if name:
100 section_name = '.'.join(_escape_id(c) for c in name)
101 if is_array:
102 fout.write('[[{0}]]\n'.format(section_name))
103 else:
104 fout.write('[{0}]\n'.format(section_name))
105
106 table_keys = sorted(table.keys()) if sort_keys else table.keys()
107 new_tables = []
108 has_kv = False
109 for k in table_keys:
110 v = table[k]
111 if isinstance(v, dict):
112 new_tables.append((name + (k,), v, False))
113 elif isinstance(v, list) and v and all(isinstance(o, dict) for o in v):
114 new_tables.extend((name + (k,), d, True) for d in v)
115 elif v is None:
116 # based on mojombo's comment: https://github.com/toml-lang/toml/issues/146#issuecomment-25019344
117 fout.write(
118 '#{} = null # To use: uncomment and replace null with value\n'.format(_escape_id(k)))
119 has_kv = True
120 else:
121 fout.write('{0} = {1}\n'.format(_escape_id(k), _format_value(v)))
122 has_kv = True
123
124 tables.extend(reversed(new_tables))
125
126 if (name or has_kv) and tables:
127 fout.write('\n')