Skip to content

Commit

Permalink
Merge pull request #21 from dragonslayerx/commandline-arg-fix
Browse files Browse the repository at this point in the history
Added: commandline optional arguments --ignore -i
  • Loading branch information
dragonslayerx committed Feb 24, 2016
2 parents 5d2f767 + 85b9fd0 commit 65fdd02
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
30 changes: 22 additions & 8 deletions cfimport.py
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
27 changes: 15 additions & 12 deletions codeforces_importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from distutils.core import setup
from setuptools import setup

setup(
name='CodeforcesImporter',
Expand Down

0 comments on commit 65fdd02

Please sign in to comment.