-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_config_file.py
169 lines (131 loc) · 4.82 KB
/
parse_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import configparser
import os
from load_config_file import locate_config_file
from default_colors import default_print_info, default_print_error, default_print_instruction
from colored_output import ColoredOutput
from pkg_resources import Requirement, resource_filename
from shutil import copyfile
def write_example_config():
garrick_dir, config_file_name = locate_config_file()
default_print_info(
'Your config file is {}.'.format(os.path.join(garrick_dir, config_file_name))
)
default_print_info(
'I am writing a file called {}.example into the same directory.'.format(config_file_name)
)
default_print_instruction(
'You can work from this file to restore your garrick.conf file to a valid state.'
)
print()
example_config_file = resource_filename(Requirement.parse('garrick'), 'garrick.conf.example')
copyfile(example_config_file, os.path.join(garrick_dir, '{}.example'.format(config_file_name)))
raise Exception('Invalid or incomplete config file.')
def get_config():
garrick_dir, config_file_name = locate_config_file()
config_file = os.path.join(garrick_dir, config_file_name)
config = configparser.ConfigParser(allow_no_value = True)
try:
config.read(config_file)
except Exception as exception:
print()
default_print_error('Something is wrong with your config file.')
default_print_error('ConfigParser has thrown the following exception:')
print()
print(exception)
print()
write_example_config()
return config
def parse_db_files():
config = get_config()
if not 'database_files' in config.sections():
print()
default_print_error(
'Error: There is no [database_files] section in your config file.'
)
print()
write_example_config()
db_files = []
for db_file in config['database_files']:
db_files.append(db_file)
if len(db_files) == 0:
print()
default_print_error(
'Error: No databases are listed in your config file.'
)
default_print_instruction(
'Write a name for a database file into its [database_files] section.'
)
default_print_info('This file will be created the next time you run garrick,')
default_print_info('or it will be used if it already exists.')
print()
write_example_config()
return db_files
def parse_editor():
config = get_config()
if not 'config' in config.sections():
print()
default_print_error('Error: There is no [config] section in your config file.')
print()
write_example_config()
if not 'editor' in config['config']:
print()
default_print_error(
'Error: There is no "editor" variable in the [config] section of your config file.'
)
print()
write_example_config()
editor = config['config']['editor']
if editor == '' or editor == None:
editor = os.getenv('EDITOR')
if editor == None:
print()
default_print_error('Error: No editor is defined in your config file.')
default_print_instruction(
'Add the name of your favourite editor at the end of the line "editor = "'
)
default_print_instruction('so you can use it to edit your cards.')
default_print_info(
"(This is normal if you haven't set the editor variable before.)"
)
print()
write_example_config()
return editor
def parse_colors():
config = get_config()
if not 'config' in config.sections():
print()
default_print_error('Error: There is no [config] section in your config file.')
print()
write_example_config()
if 'info' in config['config']:
info_color = config['config']['info']
else:
info_color = 'brightgreen'
if 'error' in config['config']:
error_color = config['config']['error']
else:
error_color = 'brightred'
if 'instruction' in config['config']:
instruction_color = config['config']['instruction']
else:
instruction_color = 'brightmagenta'
if 'side_of_card' in config['config']:
side_color = config['config']['side_of_card']
else:
side_color = 'brightyellow'
if 'prompt' in config['config']:
prompt_color = config['config']['prompt']
else:
prompt_color = 'brightcyan'
if 'silent_prompt' in config['config']:
silent_prompt_color = config['config']['silent_prompt']
else:
silent_prompt_color = 'brightyellow'
return ColoredOutput(
info_color,
error_color,
instruction_color,
side_color,
prompt_color,
silent_prompt_color
)