diff --git a/apprecommender/load_options.py b/apprecommender/load_options.py deleted file mode 100644 index 5dee332..0000000 --- a/apprecommender/load_options.py +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env python - -import getopt -import sys -import logging - -from apprecommender.config import Config -from apprecommender.singleton import Singleton - - -class LoadOptions(Singleton): - - def __init__(self): - self.options = [] - - def load(self): - config = Config() - short_options = 'hdvo:d:v:s:z:idvo:tdvo:b:n:cdvo' - long_options = ['help', 'debug', 'verbose', 'strategy=', - 'profile_size=', 'init', 'train', 'because', - 'nrecommendation', 'contribute'] - try: - opts, args = getopt.getopt(sys.argv[1:], short_options, - long_options) - self.options = opts - except getopt.GetoptError as error: - config.set_logger() - logging.error('Bad syntax: {}'.format(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'): - config.debug = 1 - elif o in ('-v', '--verbose'): - config.verbose = 1 - elif o in ('-s', '--strategy'): - config.strategy = p - elif o in ('-z', '--profile_size'): - config.profile_size = int(p) - elif o in ('-i', '--init'): - continue - elif o in ('-t', '--train'): - continue - elif o in ('-b', '--because'): - config.because = True - elif o in ('-n', '--num-recommendations'): - config.num_recommendations = int(p) - elif o in ('-c', '--contribute'): - continue - else: - assert False, "unhandled option" - - config.set_logger() - - def usage(self): - """ - Print usage help. - """ - print "[FIXME: deprecated help]" - print "\n [ general ]" - print " -h, --help Print this help" - print " -i, --init Initialize AppRecommender data" - print " -t, --train Make training of AppRecommender" \ - " machine learning" - print " -n, --num-recommendations Set the number of packages that" \ - " will be recommended" - print " -b, --because Display user packages that" \ - " generated a given recommendation" - print " -d, --debug Set logging level to debug" - print " -v, --verbose Set logging level to verbose" - print "" - print " [ recommender ]" - print " -s, --strategy=OPTION Recommendation strategy" - print " -z, --profilesize=k Size of user profile" - print "" - print " [ strategy options ] " - print " cb = content-based, mixed profile" - print " cbt = content-based, tags only profile" - print " cbd = content-based, description terms only profile" - print " cbh = content-based, half-half profile" - print " cbtm = content-based, time-context profile" - print " cb_eset = cb with eset profiling" - print " cbt_eset = cbt with eset profiling" - print " cbd_eset = cbd_eset with eset profiling" - print " cbh_eset = cbh with eset profiling" - print " mlbva = machine_learning, Binary Vector Approach" - print " mlbow = machine_learning, Bag Of Words" - print "" - print " [ contribute with AppRecommender ]" - print " -c, --contribute classify recommendations" \ - " helping AppRecommender to improve recommendations" diff --git a/apprecommender/main/cli.py b/apprecommender/main/cli.py index e9e2778..6994444 100644 --- a/apprecommender/main/cli.py +++ b/apprecommender/main/cli.py @@ -5,10 +5,10 @@ from apprecommender.main.app_recommender import AppRecommender from apprecommender.config import Config from apprecommender.initialize import Initialize -from apprecommender.load_options import LoadOptions from apprecommender.strategy import (MachineLearning, MachineLearningBVA, MachineLearningBOW) from apprecommender.main import collect_user_data +from apprecommender.main.options import get_parser SUCCESS = 0 ERROR_INIT = 1 @@ -16,15 +16,23 @@ PERMISSION_DENIED = 3 -def check_for_flag(options, short_flag, long_flag): - for option, _ in options: - if option in (short_flag, long_flag): - return True +def parse_options(args, config): - return False + if args['strategy']: + config.strategy = args['strategy'] + if args['debug']: + config.debug = 1 + if args['verbose']: + config.verbose = 1 + if args['profile_size']: + config.profile_size = args['profile_size'] + if args['because']: + config.because = True + if args['num_recommendations']: + config.num_recommendations = args['num_recommendations'] -def run_apprecommender(options): +def run_apprecommender(): try: app_recommender = AppRecommender() app_recommender.make_recommendation() @@ -38,12 +46,8 @@ def run_apprecommender(options): return PERMISSION_DENIED -def run(): - load_options = LoadOptions() - load_options.load() - options = load_options.options - - if check_for_flag(options, '-i', '--init'): +def run(args): + if args['init']: print "Initializing AppRecommender" initialize = Initialize() @@ -53,7 +57,7 @@ def run(): return PERMISSION_DENIED return SUCCESS - elif check_for_flag(options, '-t', '--train'): + elif args['train']: print "Training machine learning" try: @@ -63,14 +67,19 @@ def run(): return PERMISSION_DENIED return SUCCESS - elif check_for_flag(options, '-c', '--contribute'): + elif args['contribute']: collect_user_data.main() else: - return run_apprecommender(load_options.options) + config = Config() + parse_options(args, config) + return run_apprecommender() def main(): - result = run() + parser = get_parser() + args = vars(parser.parse_args()) + + result = run(args) if result is ERROR_INIT: print "\n" diff --git a/apprecommender/main/options.py b/apprecommender/main/options.py new file mode 100644 index 0000000..969a41d --- /dev/null +++ b/apprecommender/main/options.py @@ -0,0 +1,54 @@ +import argparse + + +def get_parser(): + apprec_description = 'Package recommender system for Debian (and derived) \ + distros' + parser = argparse.ArgumentParser(description=apprec_description) + + parser.add_argument( + '-s', '--strategy', + help='select strategy to run apprecommender (default: cb)', + type=str) + + parser.add_argument( + '-d', '--debug', + help='run apprecommender on debug mode', + type=int) + + parser.add_argument( + '-v', '--verbose', + help='run apprecommender on verbose mode', + type=int) + + parser.add_argument( + '-z', '--profile-size', + help='set the profile size of an user on apprecommender', + type=int) + + parser.add_argument( + '-i', '--init', + help='initialize apprecommender database', + action='store_true') + + parser.add_argument( + '-t', '--train', + help='train machine learning algorithms', + action='store_true') + + parser.add_argument( + '-b', '--because', + help="display which user's packages generated a recommendation", + action='store_true') + + parser.add_argument( + '-n', '--num-recommendations', + help='set the number of packages that will be recommended', + type=int) + + parser.add_argument( + '-c', '--contribute', + help='classify recommendations and help apprecommender to improve', + action='store_true') + + return parser diff --git a/apprecommender/tests/test_run.py b/apprecommender/tests/test_run.py index 160e496..0fadb91 100644 --- a/apprecommender/tests/test_run.py +++ b/apprecommender/tests/test_run.py @@ -4,6 +4,8 @@ import logging import apprecommender.main.cli as apprec + +from apprecommender.main.options import get_parser from apprecommender.config import Config from apprecommender.ml.data import MachineLearningData @@ -15,18 +17,21 @@ def setUp(self): self.axi_desktopapps = Config().axi_desktopapps + parser = get_parser() + self.args = vars(parser.parse_args('')) + def tearDown(self): Config().axi_desktopapps = self.axi_desktopapps def test_success_run_apprec(self): logging.getLogger().disabled = False - result = apprec.run() + result = apprec.run(self.args) self.assertEqual(apprec.SUCCESS, result) def test_error_init_on_run_apprec(self): Config().axi_desktopapps = "asd" - result = apprec.run() + result = apprec.run(self.args) self.assertEqual(apprec.ERROR_INIT, result) @@ -38,7 +43,7 @@ def test_error_train_on_run_apprec(self): training_path = MachineLearningData.MACHINE_LEARNING_TRAINING MachineLearningData.MACHINE_LEARNING_TRAINING = "error.txt" - result = apprec.run() + result = apprec.run(self.args) config.strategy = strategy MachineLearningData.MACHINE_LEARNING_TRAINING = training_path