-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconfig.py
More file actions
executable file
·105 lines (87 loc) · 2.75 KB
/
Copy pathconfig.py
File metadata and controls
executable file
·105 lines (87 loc) · 2.75 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
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
"""
import os
import appdirs
from six.moves import input
import yaml
from .dialog import ConfigGenerator
class NotConfigured(Exception):
pass
# static global
data = {}
class ConfigFile(object):
""" A wrapper for managing creating and reading a config file
takes (optional) appname str kwarg,
for testing creation/destruction """
# TODO emit warning if config doesn't exist
# TODO make subcommand for creating config file
appname = 'gitberg'
file_name = 'config.yaml'
def __init__(self, appname=None):
if appname:
self.appname = appname
self.dir = appdirs.user_config_dir(self.appname)
self.exists_or_make()
self.parse()
@property
def file_path(self):
return os.path.join(self.dir, self.file_name)
def exists_or_make(self):
if not os.path.exists(self.dir):
os.makedirs(self.dir)
if not os.path.exists(self.file_path):
with open(self.file_path, 'a'):
os.utime(self.file_path, None)
def write(self):
# FIXME: VVV
# self.check_self()
with open(self.file_path, 'wb') as self.file:
self.file.write(self.yaml)
return True
@property
def yaml(self):
return yaml.dump(data,
default_flow_style=False)
def __repr__(self):
return self.read()
def read(self):
with open(self.file_path) as _fp:
return _fp.read()
def parse(self):
global data
data = yaml.load(self.read())
data = {} if data is None else data
self.data = data
def check_self(self):
# TODO: do a basic check of internal data values
# TODO: check if file already exists
pass
def check_config():
""" Report if there is an existing config file
"""
configfile = ConfigFile()
global data
if data.keys() > 0:
# FIXME: run a better check of this file
print("gitberg config file exists")
print("\twould you like to edit your gitberg config file?")
else:
print("No config found")
print("\twould you like to create a gitberg config file?")
answer = input("--> [Y/n]")
# By default, the answer is yes, as denoted by the capital Y
if not answer:
answer = 'Y'
# If yes, generate a new configuration
# to be written out as yaml
if answer in 'Yy':
print("Running gitberg config generator ...")
# config.exists_or_make()
config_gen = ConfigGenerator(current=data)
config_gen.ask()
# print(config_gen.answers)
data = config_gen.answers
configfile.write()
print("Config written to {}".format(configfile.file_path))