config.py
13.3 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/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
import logging
import logging.handlers
from ConfigParser import *
from singleton import Singleton
class Config(Singleton):
"""
Class to handle configuration options.
"""
def __init__(self):
"""
Set default configuration options.
"""
if not hasattr(self, 'initialized'):
## general options
self.debug = 0
self.verbose = 1
self.output = "log"
## data_source options
self.base_dir = os.path.expanduser("~/.app-recommender/")
# filters for valid packages
self.filters_dir = os.path.join(self.base_dir,"filters")
self.pkgs_filter = os.path.join(self.filters_dir,"programs")
# package information packages
self.axi = "/var/lib/apt-xapian-index/index"
self.axi_programs = os.path.join(self.base_dir,"axi_programs")
self.axi_desktopapps = os.path.join(self.base_dir,"axi_desktopapps")
# popcon indexes
self.index_mode = "old"
# check if there are popcon indexes available
self.popcon = 1
self.popcon_programs = os.path.join(self.base_dir,"popcon_programs")
self.popcon_desktopapps = os.path.join(self.base_dir,"popcon_desktopapps")
self.popcon_index = self.popcon_programs
self.popcon_dir = os.path.join(self.base_dir,"popcon-entries")
self.max_popcon = 1000
# popcon clustering
self.clusters_dir = os.path.join(self.base_dir,"clusters-dir")
self.k_medoids = 100
# self.dde_url = "http://dde.debian.net/dde/q/udd/packs/all/%s?t=json"
self.dde_url = "http://46.4.235.200:8000/q/udd/packages/all/%s?t=json"
self.dde_server = "46.4.235.200"
self.dde_port = "8000"
## recomender options
self.strategy = "cb"
self.weight = "bm25"
self.bm25_k1 = 1.2
self.bm25_k2 = 0
self.bm25_k3 = 7
self.bm25_b = 0.75
self.bm25_nl = 0.5
# user content profile size
self.profile_size = 50
# neighborhood size
self.k_neighbors = 50
# popcon profiling method: full, voted
self.popcon_profiling = "full"
self.load_options()
self.set_logger()
self.initialized = 1
logging.info("Basic config")
def usage(self):
"""
Print usage help.
"""
print "[FIXME: deprecated 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 ""
print " [ data sources ]"
print " -f, --filtersdir=PATH Path to filters directory"
print " -b, --pkgsfilter=FILTER File containing packages to be considered for recommendations"
print " -a, --axi=PATH Path to apt-xapian-index"
print " -e, --dde=URL DDE url"
print " -p, --popconindex=PATH Path to popcon index"
print " -m, --popcondir=PATH Path to popcon submissions dir"
print " -u, --indexmode=MODE 'old'|'reindex'|'cluster'|'recluster'"
print " -l, --clustersdir=PATH Path to popcon clusters dir"
print " -c, --medoids=k Number of medoids for clustering"
print " -x, --maxpopcon=k Number of submissions to be considered"
print ""
print " [ recommender ]"
print " -w, --weight=OPTION Search weighting scheme"
print " -s, --strategy=OPTION Recommendation strategy"
print " -z, --profilesize=k Size of user profile"
print " -i, --profiling=OPTION Profile filter strategy"
print " -n, --neighbors=k Size of neighborhood for collaboration"
print ""
print " [ weight options ] "
print " trad = traditional probabilistic weighting"
print " bm25 = bm25 weighting scheme"
print ""
print " [ strategy options ] "
print " cb = content-based "
print " cbt = content-based using only tags as content "
print " cbd = content-based using only package descriptions as content "
print " col = collaborative "
print " colct = collaborative through tags 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('~/.app_recommender.rc'),
'app_recommender.cfg'])
except (MissingSectionHeaderError), err:
logging.error("Error in config file syntax: %s", str(err))
os.abort()
self.debug = int(self.read_option('general', 'debug'))
self.debug = int(self.read_option('general', 'verbose'))
self.output = os.path.join(self.base_dir,
self.read_option('general','output'))
self.filters_dir = os.path.join(self.base_dir,
self.read_option('data_sources',
'filters_dir'))
self.pkgs_filter = os.path.join(self.filters_dir,
self.read_option('data_sources',
'pkgs_filter'))
self.axi = self.read_option('data_sources', 'axi')
self.axi_programs = os.path.join(self.base_dir,
self.read_option('data_sources',
'axi_programs'))
self.axi_desktopapps = os.path.join(self.base_dir,
self.read_option('data_sources',
'axi_desktopapps'))
#self.index_mode = self.read_option('data_sources', 'index_mode')
self.popcon = int(self.read_option('data_sources', 'popcon'))
self.popcon_programs = os.path.join(self.base_dir,
self.read_option('data_sources',
'popcon_programs'))
self.popcon_desktopapps = os.path.join(self.base_dir,
self.read_option('data_sources',
'popcon_desktopapps'))
self.popcon_index = os.path.join(self.base_dir,
self.read_option('data_sources',
'popcon_index'))
self.popcon_dir = os.path.join(self.base_dir,
self.read_option('data_sources',
'popcon_dir'))
self.max_popcon = int(self.read_option('data_sources', 'max_popcon'))
self.clusters_dir = os.path.join(self.base_dir,
self.read_option('data_sources',
'clusters_dir'))
self.k_medoids = int(self.read_option('data_sources', 'k_medoids'))
self.dde_url = self.read_option('data_sources', 'dde_url')
self.dde_server = self.read_option('data_sources', 'dde_server')
self.dde_port = self.read_option('data_sources', 'dde_port')
self.weight = self.read_option('recommender', 'weight')
self.bm25_k1 = float(self.read_option('recommender', 'bm25_k1'))
self.bm25_k2 = float(self.read_option('recommender', 'bm25_k2'))
self.bm25_k3 = float(self.read_option('recommender', 'bm25_k3'))
self.bm25_b = float(self.read_option('recommender', 'bm25_b'))
self.bm25_nl = float(self.read_option('recommender', 'bm25_nl'))
self.strategy = self.read_option('recommender', 'strategy')
self.profile_size = int(self.read_option('recommender',
'profile_size'))
self.k_neighbors = int(self.read_option('recommender',
'k_neighbors'))
self.popcon_profiling = self.read_option('recommender',
'popcon_profiling')
short_options = "hdvo:f:b:a:e:p:m:u:l:c:x:w:s:z:i:n:"
long_options = ["help", "debug", "verbose", "output=", "filtersdir=",
"pkgsfilter=", "axi=", "dde=", "popconindex=",
"popcondir=", "indexmode=", "clustersdir=", "kmedoids=",
"maxpopcon=", "weight=", "strategy=", "profile_size=",
"profiling=", "neighbors="]
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 ("-f", "--filtersdir"):
self.filters_dir = p
elif o in ("-b", "--pkgsfilter"):
self.pkgs_filter = p
elif o in ("-a", "--axi"):
self.axi = p
elif o in ("-e", "--dde"):
self.dde_url = p
elif o in ("-p", "--popconindex"):
self.popcon_index = p
elif o in ("-m", "--popcondir"):
self.popcon_dir = p
elif o in ("-u", "--index_mode"):
self.index_mode = p
elif o in ("-l", "--clustersdir"):
self.clusters_dir = p
elif o in ("-c", "--kmedoids"):
self.k_medoids = int(p)
elif o in ("-x", "--max_popcon"):
self.max_popcon = int(p)
elif o in ("-w", "--weight"):
self.weight = p
elif o in ("-s", "--strategy"):
self.strategy = p
elif o in ("-z", "--profile_size"):
self.profile_size = int(p)
elif o in ("-z", "--profiling"):
self.profiling = p
elif o in ("-n", "--neighbors"):
self.k_neighbors = int(p)
else:
assert False, "unhandled option"
def set_logger(self):
"""
Configure application logger and log level.
"""
self.logger = logging.getLogger('') # root logger is used by default
self.logger.setLevel(logging.DEBUG)
if self.debug == 1:
log_level = logging.DEBUG
elif self.verbose == 1:
log_level = logging.INFO
else:
log_level = logging.WARNING
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
console_handler.setLevel(log_level)
self.logger.addHandler(console_handler)
file_handler = logging.handlers.RotatingFileHandler(self.output,
maxBytes=50000000,
backupCount=5)
log_format = '%(asctime)s %(levelname)-8s %(message)s'
file_handler.setFormatter(logging.Formatter(log_format,
datefmt='%Y-%m-%d %H:%M:%S'))
file_handler.setLevel(log_level)
self.logger.addHandler(file_handler)
logging.info("Set up logger")