-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathutil.py
More file actions
40 lines (32 loc) · 1.1 KB
/
Copy pathutil.py
File metadata and controls
40 lines (32 loc) · 1.1 KB
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
import contextlib
import io
import os
DFHACK_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
DOCS_ROOT = os.path.join(DFHACK_ROOT, 'docs')
if not os.path.isdir(DOCS_ROOT):
raise ReferenceError('docs root not found: %s' % DOCS_ROOT)
@contextlib.contextmanager
def write_file_if_changed(path):
with io.StringIO() as buffer:
yield buffer
new_contents = buffer.getvalue()
try:
with open(path, 'r') as infile:
old_contents = infile.read()
except IOError:
old_contents = None
if old_contents != new_contents:
with open(path, 'w') as outfile:
outfile.write(new_contents)
# directive argument helpers (supplementing docutils.parsers.rst.directives)
def directive_arg_str_list(argument):
"""
Converts a space- or comma-separated list of values into a Python list
of strings.
(Directive option conversion function.)
"""
if ',' in argument:
entries = argument.split(',')
else:
entries = argument.split()
return [entry.strip() for entry in entries]