-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
44 lines (35 loc) · 1.24 KB
/
utils.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
import difflib
import json
import os
stored_files_dir = 'stored_files'
dir_files_file = 'file_map.json'
def diff_contents(from_contents, to_contents, **kwargs):
return '\n'.join(difflib.unified_diff(from_contents.split('\n'), to_contents.split('\n'), lineterm='', **kwargs))
def input_bool(prompt):
while True:
try:
user_input = input(prompt + ' ([y]/n): ')
if user_input in ('', 'y'):
return True
elif user_input == 'n':
return False
except KeyboardInterrupt:
raise
except:
print('Invalid input')
def load_actual_file(filename, directory):
return open(os.path.join(directory, filename), 'r').read()
def load_stored_files():
return {
filename: open(stored_file_path(filename), 'r').read()
for filename in os.listdir(stored_files_dir)
}
def load_file_dirs():
file_dirs = {}
for dir_files in json.load(open(dir_files_file, 'r')):
for filename in dir_files['files']:
assert filename not in file_dirs, 'Duplicate filenames currently not supported (even across different directories).'
file_dirs[filename] = dir_files['directory'].replace('~', os.environ['HOME'])
return file_dirs
def stored_file_path(filename):
return os.path.join(stored_files_dir, filename)