From 85b9fd0880e973a725d0ea799d00314b3658f7fe Mon Sep 17 00:00:00 2001 From: dragonslayer Date: Wed, 24 Feb 2016 19:35:49 +0530 Subject: [PATCH] Added: commandline optional arguments --ignore -i --- cfimport.py | 30 ++++++++++++++++++++++-------- codeforces_importer/importer.py | 27 +++++++++++++++------------ setup.py | 2 +- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/cfimport.py b/cfimport.py index 7293f8d..1b57d51 100644 --- a/cfimport.py +++ b/cfimport.py @@ -1,22 +1,36 @@ import codeforces_importer.importer import sys - +import getopt argc = len(sys.argv) argv = sys.argv -if argc != 3: - print 'Usage ' + argv[0] + " handle local_directory" + +handle = None +dir_path = None +fetch_submission_flag = True; + +if argc < 3: + print "Usage python cfimport.py [-i, --ignore] {handle} {directory_path}" handle = str(raw_input("Enter user's Codeforces handle: ")) dir_path = str(raw_input("Enter local_directory_path for import: ")) - + fetch_submission_flag = str(raw_input("Do you want to import submission program-source (y/n) : ")) == 'y' else: - argv = argv[1:] - handle = argv[0] - dir_path = argv[1] + try: + opt_list, arg = getopt.getopt(sys.argv[1:], 'i', ['ignore']) + except getopt.GetoptError as ex: + print(ex); + print "Usage python cfimport.py [-i, --ignore] {handle} {directory_path}" + sys.exit(1); + else: + for option, val in opt_list: + if option == '-i' or option == '--ignore': + fetch_submission_flag = False + handle = arg[0] + dir_path = arg[1] if handle is not None and dir_path is not None: print 'Importing submissions of ' + handle + ' at ' + dir_path - codeforces_importer.importer.import_codes(handle, dir_path) + codeforces_importer.importer.import_codes(handle, dir_path, fetch_submission_flag) sys.exit(0) else: print 'Invalid args' diff --git a/codeforces_importer/importer.py b/codeforces_importer/importer.py index b9b673d..3e7778b 100644 --- a/codeforces_importer/importer.py +++ b/codeforces_importer/importer.py @@ -8,7 +8,7 @@ from cfi_ignore import CfiIgnore -def import_codes(handle, dir_path='.\log\\', max_sub_lim=10000): +def import_codes(handle, dir_path='.\log\\', fetch_submission_flag=True, max_sub_lim=10000): """Calls modules to import user-submissions-list, extract source-code, adding problems to classifier and write to file. :param handle: user's handle whose submissions are to be imported @@ -57,22 +57,25 @@ def import_codes(handle, dir_path='.\log\\', max_sub_lim=10000): # adds problem to classifier classifier.add(submission.problem, submission.id, relative_path) - # check if the submission is pre-fetched - if cfi_ignore.ignore(problem_id) is False and is_gym(problem_id) is False: + # fetch_submission_flag = True if user desires to import submissions + if fetch_submission_flag: - # extracts the source code at the submission id - code = source_code_extractor.extract_source_code(str(submission.contest_id), str(submission.id)) + # check if the submission is pre-fetched + if cfi_ignore.ignore(problem_id) is False and is_gym(problem_id) is False: - # writing submission to file - file_io.write_to_file(absolute_path, code) + # extracts the source code at the submission id + code = source_code_extractor.extract_source_code(str(submission.contest_id), str(submission.id)) - # add problem to ignore-list so that it is not fetched next time - cfi_ignore.add(problem_id) + # writing submission to file + file_io.write_to_file(absolute_path, code) - print 'Successfully written submission: ' + str(submission.id) + ' to ' + absolute_path + # add problem to ignore-list so that it is not fetched next time + cfi_ignore.add(problem_id) - else: - print 'ignoring submission. cfiignore suggests it has been fetched earlier' + print 'Successfully written submission: ' + str(submission.id) + ' to ' + absolute_path + + else: + print 'ignoring submission. cfiignore suggests it has been fetched earlier' # ignore any exception in parsing source_code except Exception as ex: diff --git a/setup.py b/setup.py index ed0644c..32243a1 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from distutils.core import setup +from setuptools import setup setup( name='CodeforcesImporter',