9593d25c98530330eb334cb01d673cdbf1e2e175
[g-pypi.git] / g_pypi / config.py
1 #!/usr/bin/env python
2
3 # pylint: disable-msg=R0201
4 # method could be function but we need shared class data
5
6 """
7
8 config.py
9 =========
10
11 Creates and reads config file using ConfigObj
12
13         config['keyword'] = get_keyword()
14         config['overlay'] = get_portdir_overlay()
15         config['format'] = "ansi"
16
17 """
18
19 import os
20 import logging
21
22 from configobj import ConfigObj
23
24 from g_pypi.portage_utils import get_keyword, get_portdir_overlay
25
26 __docformat__ = 'restructuredtext'
27
28 CONFIG_DIR = os.path.expanduser("~/.g-pypi")
29
30
31 class MyConfig:
32
33     """
34     Holds options from config file
35     """
36
37     config = None
38     options = None
39     logger = None
40
41     def __init__(self):
42         self.set_config(self.get_config())
43     
44     def get_config(self):
45         """Read config file, create if it doesn't exist"""
46         if not os.path.exists(self.get_rc_filename()):
47             self.create_config()
48         return ConfigObj(self.get_rc_filename()) 
49
50     def create_config(self):
51         """Create config file with defaults"""
52         if not os.path.exists(CONFIG_DIR):
53             os.mkdir(CONFIG_DIR)
54         self.create_config_obj()
55
56     def create_config_obj(self):
57         """Set defaults for ConigObj"""
58         config = ConfigObj()
59         config.filename = self.get_rc_filename()
60         config['keyword'] = get_keyword()
61         config['overlay'] = get_portdir_overlay()
62         config['format'] = "ansi"
63         config['background'] = "dark"
64         config.write()
65         self.set_config(config)
66         #logger isn't set yet
67         print "Your default keyword will be: %s " % \
68                 config['keyword']
69         print "Your default overlay will be: %s " % \
70                 config['overlay']
71         print "To change these edit: %s \n\n" % config.filename
72
73     def set_config(self, config):
74         """Set config"""
75         MyConfig.config = config
76
77     def set_options(self, options):
78         """Set options"""
79         MyConfig.options = options
80
81     def get_rc_filename(self):
82         """Return rc_file filename"""
83         return os.path.join(CONFIG_DIR, "g-pypirc")
84
85     def set_logger(self):
86         """Set logger"""
87         MyConfig.logger = logging.getLogger("g-pypi")
88         if MyConfig.options.verbose:
89             MyConfig.logger.setLevel(logging.INFO)
90         elif MyConfig.options.quiet:
91             MyConfig.logger.setLevel(logging.ERROR)
92         elif MyConfig.options.debug:
93             MyConfig.logger.setLevel(logging.DEBUG)
94         else:
95             MyConfig.logger.setLevel(logging.INFO)
96         MyConfig.logger.addHandler(logging.StreamHandler())
97
98     def get_logger(self):
99         """Return logging object"""
100         return MyConfig.logger
101