-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_config_file.py
77 lines (61 loc) · 2.8 KB
/
load_config_file.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
from default_colors import default_print_info, default_print_error, default_print_instruction
dir_name = '.garrick'
config_file_name = 'garrick.conf'
def locate_config_file():
home_dir = os.getenv('HOME')
if home_dir == None:
print()
default_print_error(
"Sorry, your system doesn't have a HOME environment variable set to any directory."
)
default_print_error("I am not equipped to deal with this situation :(")
default_print_error("Which OS are you running?")
print()
raise Exception('HOME variable not set.')
garrick_dir = os.path.join(home_dir, dir_name)
if not os.path.exists(os.path.join(garrick_dir, config_file_name)):
if os.path.exists(garrick_dir):
print()
default_print_error('Uh oh.')
default_print_error(
'There is a folder named "{}" in your home directory,'.format(dir_name)
)
default_print_error('but it doesn\'t contain the file "{}".'.format(config_file_name))
default_print_error("If you've renamed or moved it, can you please change it back?")
default_print_error('I am scared of breaking things. I will go now. Bai.')
print()
raise Exception('Directory exists.')
create_config_file(garrick_dir)
return garrick_dir, config_file_name
def create_config_file(garrick_dir):
os.mkdir(garrick_dir)
default_editor = os.getenv('EDITOR')
config_file_full_path = os.path.join(garrick_dir, config_file_name)
with open(config_file_full_path, 'w') as f:
f.write('[database_files]\n')
f.write('cards.db\n\n')
f.write('[config]\n')
if default_editor == None:
f.write('editor =\n')
else:
f.write('editor = {}\n'.format(default_editor))
f.write('\n# COLOURS\n')
f.write('# Available choices are:\n')
f.write('# black, red, green, yellow, blue, magenta, cyan, white,\n')
f.write('# brightblack, brightred, etc.\n')
f.write('info = brightgreen\n')
f.write('error = brightred\n')
f.write('instruction = brightmagenta\n')
f.write('side_of_card = brightyellow\n')
f.write('prompt = brightcyan\n')
f.write('silent_prompt = brightyellow\n')
print_info('Created directory {}.'.format(garrick_dir))
if default_editor == None:
default_print_instruction(
'You might want to set the "editor" variable in {},'.format(config_file_full_path)
)
default_print_instruction('so that you can edit your cards with your favourite editor.')
else:
default_print_info('Editor set to {}.'.format(default_editor))
default_print_instruction('(You can change this in {}.)'.format(config_file_full_path))