config.py
7.49 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
"""
config - python module for configuration options.
"""
__author__ = "Tassia Camoes Araujo <tassia@gmail.com>"
__copyright__ = "Copyright (C) 2011 Tassia Camoes Araujo"
__license__ = """
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import getopt
import sys
import os
from logging import *
import logging.handlers
from ConfigParser import *
class Config():
"""
Class to handle configuration options.
"""
def __init__(self):
"""
Set configuration options.
"""
self.debug = 0
self.verbose = 0
self.output = "/dev/null"
self.config = None
self.tags_db = "/var/lib/debtags/package-tags"
self.tags_index = "~/.app-recommender/debtags_index"
self.axi = "/var/lib/apt-xapian-index/index"
self.axi_values = "/var/lib/apt-xapian-index/values"
self.popcon_index = "~/.app-recommender/popcon_index"
self.popcon_dir = "~/.app-recommender/popcon_dir"
self.clusters_dir = "~/.app-recommender/clusters_dir"
self.strategy = "cta" # defaults to the cheapest one
self.reindex = 0
self.load_options()
self.set_logger()
def usage(self):
"""
Print usage help.
"""
print "\n [ general ]"
print " -h, --help Print this help"
print " -d, --debug Set logging level to debug."
print " -v, --verbose Set logging level to verbose."
print " -o, --output=PATH Path to file to save output."
print " -c, --config=PATH Path to configuration file."
print ""
print " [ recommender ]"
print " -t, --tagsdb=PATH Path to debtags database."
print " -i, --tagsindex=PATH Path to debtags dedicated index."
print " -r, --force-reindex Force reindexing debtags database."
print " -a, --axi=PATH Path to Apt-xapian-index."
print " -p, --popconindex=PATH Path to popcon dedicated index."
print " -m, --popcondir=PATH Path to popcon submissions dir."
print " -l, --clustersdir=PATH Path to popcon clusters dir."
print " -s, --strategy=OPTION Recommendation strategy."
print ""
print " [ strategy options ] "
print " ct = content-based using tags "
print " cta = content-based using tags via apt-xapian-index"
print " cp = content-based using package descriptions "
print " col = collaborative "
print " colct = collaborative through tags content "
print " colcp = collaborative through package descriptions content "
def read_option(self, section, option):
"""
Read option from configuration file if it is defined there or return
default value.
"""
var = "self.%s" % option
if self.config.has_option(section, option):
return self.config.get(section, option)
else:
return eval(var)
def load_options(self):
"""
Load options from configuration file and command line arguments.
"""
try:
self.config = ConfigParser()
self.config.read(['/etc/apprecommender/recommender.conf',
os.path.expanduser('~/apprecommender.rc')])
except (MissingSectionHeaderError), err:
logging.error("Error in config file syntax: %s", str(err))
os.abort()
self.debug = self.read_option('general', 'debug')
self.debug = self.read_option('general', 'verbose')
self.output_filename = self.read_option('general', 'output')
self.config = self.read_option('general', 'config')
self.tags_db = self.read_option('recommender', 'tags_db')
self.tags_index = self.read_option('recommender', 'tags_index')
self.reindex = self.read_option('recommender', 'reindex')
self.axi = self.read_option('recommender', 'axi')
self.popcon_index = self.read_option('recommender', 'popcon_index')
self.popcon_dir = self.read_option('recommender', 'popcon_dir')
self.clusters_dir = self.read_option('recommender', 'clusters_dir')
short_options = "hdvo:c:t:i:ra:p:m:s:"
long_options = ["help", "debug", "verbose", "output=", "config=",
"tagsdb=", "tagsindex=", "reindex", "axi=",
"popconindex=", "popcondir=", "clustersdir=",
"strategy="]
try:
opts, args = getopt.getopt(sys.argv[1:], short_options,
long_options)
except getopt.GetoptError as error:
self.set_logger()
logging.error("Bad syntax: %s" % str(error))
self.usage()
sys.exit()
for o, p in opts:
if o in ("-h", "--help"):
self.usage()
sys.exit()
elif o in ("-d", "--debug"):
self.debug = 1
elif o in ("-v", "--verbose"):
self.verbose = 1
elif o in ("-o", "--output"):
self.output = p
elif o in ("-c", "--config"):
self.config = p
elif o in ("-t", "--tagsdb"):
self.tags_db = p
elif o in ("-i", "--tagsindex"):
self.tags_index = p
elif o in ("-r", "--force-reindex"):
self.reindex = 1
elif o in ("-a", "--axi"):
self.axi = p + "/index"
self.axi_values = p + "/values"
elif o in ("-p", "--popconindex"):
self.popcon_index = p
elif o in ("-m", "--popcondir"):
self.popcon_dir = p
elif o in ("-l", "--clustersdir"):
self.popcon_dir = p
elif o in ("-s", "--strategy"):
self.strategy = p
else:
assert False, "unhandled option"
def set_logger(self):
"""
Configure application logger and log level.
"""
self.logger = getLogger('') # root logger is used by default
self.logger.setLevel(DEBUG)
if self.debug == 1:
log_level = DEBUG
elif self.verbose == 1:
log_level = INFO
else:
log_level = WARNING
console_handler = StreamHandler(sys.stdout)
console_handler.setFormatter(Formatter('%(levelname)s: %(message)s'))
console_handler.setLevel(log_level)
self.logger.addHandler(console_handler)
file_handler = logging.handlers.RotatingFileHandler(self.output,
maxBytes=5000,
backupCount=5)
log_format = '%(asctime)s AppRecommender %(levelname)-8s %(message)s'
file_handler.setFormatter(Formatter(log_format))
file_handler.setLevel(log_level)
self.logger.addHandler(file_handler)