Skip to content

Commit

Permalink
Merge branch 'improve_command_line' into 'master'
Browse files Browse the repository at this point in the history
Improve AppRecommender command line options



See merge request !26
  • Loading branch information
LucianoPC committed Aug 2, 2016
2 parents 88c4e40 + 4dbdb36 commit 043cc36
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 115 deletions.
95 changes: 0 additions & 95 deletions apprecommender/load_options.py

This file was deleted.

43 changes: 26 additions & 17 deletions apprecommender/main/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@
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
ERROR_TRAIN = 2
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()
Expand All @@ -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()

Expand All @@ -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:
Expand All @@ -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"
Expand Down
54 changes: 54 additions & 0 deletions apprecommender/main/options.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 8 additions & 3 deletions apprecommender/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 043cc36

Please sign in to comment.