Ejemplo n.º 1
0
def save_followers(followers, username):
    current_datetime = datetime.now().strftime('%Y-%m-%d %H%M%S')
    filename = 'Followers at ' + current_datetime + '.txt'
    write_to_file(root_dir + username + '/' + ARCHIVE_DIRNAME + filename,
                  str(followers))
    write_to_file(root_dir + username + '/' + FOLLOWERS_FILENAME,
                  str(followers))
Ejemplo n.º 2
0
def save_report(report, username, filetype):
    current_datetime = datetime.now().strftime('%Y-%m-%d %H%M%S')
    archive_report_filename = 'Report generated ' + current_datetime + '.' + filetype

    write_to_file(
        root_dir + username + '/' + ARCHIVE_DIRNAME + archive_report_filename,
        report)
    filename = root_dir + username + '/' + REPORT_FILENAME + '.' + filetype
    write_to_file(filename, report)
    return filename
Ejemplo n.º 3
0
def generate_dirs():
    dirs = dict()

    data_dir = input("Enter the directory where you want data to be stored: ")
    good_dir = is_valid_dir(data_dir) and ends_in_slash(data_dir)
    while not (good_dir):
        if not ends_in_slash(data_dir):
            data_dir = input(
                "That is not a valid directory. Valid directories must end in a slash. Please try again: "
            )
        else:
            data_dir = input(
                "That is not a directory you can create. Please try again: ")
        good_dir = is_valid_dir(data_dir) and ends_in_slash(data_dir)
    dirs['data'] = data_dir

    wants_to_store_logins = get_boolean_response(STORE_LOGIN_DATA_PROMPT)
    if wants_to_store_logins:
        logins_dir = input(
            "Enter the location of the text file where you store your logins: "
        )
        while not (is_valid_filepath(data_dir)):
            logins_dir = input(
                "That is not a file you can create. Please try again: ")
        dirs['logins'] = logins_dir

    # Save dirs.txt
    write_to_file('dirs.txt', str(dirs))

    # Get login details
    if wants_to_store_logins:
        print(
            "Now enter the user details you want to save. To finish, enter * as a username:"******"Username: "******"Password: "******"Username: ").strip()
            keep_going = username != '*'

        # Save logins file
        write_to_file(logins_dir, str(logins))
Ejemplo n.º 4
0
def create_files(username):
    """Checks if the necessary files and directories exists.
       If they don't, creates them. Necessary files and directories:
       * root_dir
       * root_dir/username/
       * root_dir/username/archive/
       * root_dir/username/Followers.txt
    """

    archive = ARCHIVE_DIRNAME

    dirs = [
        root_dir, root_dir + username + "/",
        root_dir + username + "/" + archive
    ]

    for directory in dirs:
        if not os.path.isdir(directory):
            os.makedirs(directory)
            print("Creating directory: " + directory)

    if not os.path.exists(root_dir + username + '/' + FOLLOWERS_FILENAME):
        # Followers text file doesn't exist - create empty set of followers
        write_to_file(root_dir + username + '/' + FOLLOWERS_FILENAME, '{}')
Ejemplo n.º 5
0
def execute(filepath):

    valid, payload = jsonutil.load_and_validate_payload(
        schemas, request.Request.IN)

    if valid is False:
        return -1

    prefix = jsonutil.get_source_value(payload, "prefix")
    version = jsonutil.get_version(payload, "version")

    if prefix is (None or "") or not prefix.isalpha():
        common.log("invalid Prefix")
        return -1

    file_io.write_to_file(os.path.join(filepath, "default"),
                          name_generator.generate_default(version, prefix))

    file_io.write_to_file(os.path.join(filepath, "heroku"),
                          name_generator.generate_heroku(version, prefix))

    print(json.dumps({"version": {"version": version}}))

    return 0
def test1_write_to_file():
	ts = TS.TimeSeries()
	ts = fio.read_from_file("../timeSeriesData/TimeSeriesData1/1_temperature_test.csv")

	fio.write_to_file(ts, "1_temperature_test_output.csv")
Ejemplo n.º 7
0
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
    :param dir_path: local directory path where submissions are saved
    :param max_sub_lim: max #overall_submissions to fetch from submission page
    """

    try:
        # fetch user's submissions-list using Codeforces API
        importer = SubmissionImport(handle, max_sub_lim)
        submissions_list = importer.get_submissions()

        print 'Fetching submission list: Success\n'

        # read cfiignore file in the dir_path directory and ignores pre-fetched submissions
        cfi_ignore = CfiIgnore(dir_path)

        if submissions_list is not None:

            # instance of classifier for storing problem_name, associated_tags information
            classifier = Classifier()

            # fetch problems from Codeforces API
            problem_list = problem_importer.fetch_problems_by_category()
            for problem in problem_list:
                for tag in problem['tags']:
                    classifier.add_problem_tag(problem, tag)

            # ensures directory creation
            ensure_dir_creation(dir_path)

            for submission in submissions_list:
                try:

                    # print submission_details
                    submission.log()

                    # get problem_details
                    problem_id, problem_name = get_problem_details(submission)

                    # file path for cloned file
                    file_name = get_file_name(problem_id, problem_name,
                                              submission.prog_lang)
                    absolute_path = os.path.join(dir_path, file_name)
                    relative_path = os.path.join('.//', file_name)

                    # adds problem to classifier
                    classifier.add(submission.problem, submission.id,
                                   relative_path)

                    # fetch_submission_flag = True if user desires to import submissions
                    if fetch_submission_flag:

                        # check if the submission is pre-fetched
                        if cfi_ignore.ignore(problem_id) is False and is_gym(
                                problem_id) is False:

                            # extracts the source code at the submission id
                            code = source_code_extractor.extract_source_code(
                                str(submission.contest_id), str(submission.id))

                            # writing submission to file
                            file_io.write_to_file(absolute_path, code)

                            # add problem to ignore-list so that it is not fetched next time
                            cfi_ignore.add(problem_id)

                            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:
                    print ex.message

                print ''

            # construct date vs count
            date_vs_info = {}
            for submission in submissions_list:
                submission_date = submission.submission_time.strftime(
                    "%Y-%m-%d")
                submission_data = date_vs_info.get(
                    submission_date,
                    [submission.submission_time_components, 0])
                submission_data[1] += 1
                date_vs_info[submission_date] = submission_data

            try:
                # generates html file
                html_generator.generate_html(handle, classifier, dir_path,
                                             "classification")

                # generates html file for data visualization
                html_generator.generate_html(handle, classifier, dir_path,
                                             "visualization",
                                             date_vs_info.values())

                # writes cfi ignore
                cfi_ignore.write_ignore_list()

            except Exception as ex:
                print ex.message
                print 'Error generating html file'

    except Exception as ex:
        print 'Error: ' + ex.message
        print 'Unable to fetch your submissions at the moment.'

    else:
        print 'Import-Status: Successful.'
Ejemplo n.º 8
0
def import_codes(handle, dir_path='.\log\\', 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
    :param dir_path: local directory path where submissions are saved
    :param max_sub_lim: max #overall_submissions to fetch from submission page
    """

    try:
        # fetch user's submissions-list using Codeforces API
        importer = SubmissionImport(handle, max_sub_lim)
        submissions_list = importer.get_submissions()
        print 'Fetching submission list: Success\n'

        # read cfiignore file in the dir_path directory and ignores pre-fetched submissions
        cfi_ignore = CfiIgnore(dir_path);

        if submissions_list is not None:

            # instance of classifier for storing problem_name, associated_tags information
            classifier = Classifier()

            # ensures directory creation
            ensure_dir_creation(dir_path)

            for submission in submissions_list:
                try:

                    # print submission_details
                    submission.log()

                    # get problem_details
                    problem_id, problem_name = get_problem_details(submission)

                    # file path for cloned file
                    file_name = get_file_name(problem_id, problem_name)
                    absolute_path = os.path.join(dir_path, file_name)
                    relative_path = os.path.join('.//', file_name)

                    # 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:

                        # extracts the source code at the submission id
                        code = source_code_extractor.extract_source_code(str(submission.contest_id), str(submission.id))

                        # writing submission to file
                        file_io.write_to_file(absolute_path, code)

                        # add problem to ignore-list so that it is not fetched next time
                        cfi_ignore.add(problem_id)

                        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:
                    print ex.message

                print ''

            try:
                # generates html file
                html_generator.generate_html(handle, classifier, dir_path)

                # writes cfi ignore
                cfi_ignore.write_ignore_list()

            except Exception as ex:
                print ex.message
                print 'Error generating html file'

    except Exception as ex:
        print 'Error: ' + ex.message
        print 'Unable to fetch your submissions at the moment.'

    else:
        print 'Import-Status: Successful.'
Ejemplo n.º 9
0
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
    :param dir_path: local directory path where submissions are saved
    :param max_sub_lim: max #overall_submissions to fetch from submission page
    """

    try:
        # fetch user's submissions-list using Codeforces API
        importer = SubmissionImport(handle, max_sub_lim)
        submissions_list = importer.get_submissions()

        print 'Fetching submission list: Success\n'

        # read cfiignore file in the dir_path directory and ignores pre-fetched submissions
        cfi_ignore = CfiIgnore(dir_path);

        if submissions_list is not None:

            # instance of classifier for storing problem_name, associated_tags information
            classifier = Classifier()

            # fetch problems from Codeforces API
            problem_list = problem_importer.fetch_problems_by_category()
            for problem in problem_list:
                for tag in problem['tags']:
                    classifier.add_problem_tag(problem, tag)

            # ensures directory creation
            ensure_dir_creation(dir_path)

            for submission in submissions_list:
                try:

                    # print submission_details
                    submission.log()

                    # get problem_details
                    problem_id, problem_name = get_problem_details(submission)

                    # file path for cloned file
                    file_name = get_file_name(problem_id, problem_name, submission.prog_lang)
                    absolute_path = os.path.join(dir_path, file_name)
                    relative_path = os.path.join('.//', file_name)

                    # adds problem to classifier
                    classifier.add(submission.problem, submission.id, relative_path)

                    # fetch_submission_flag = True if user desires to import submissions
                    if fetch_submission_flag:

                        # check if the submission is pre-fetched
                        if cfi_ignore.ignore(problem_id) is False and is_gym(problem_id) is False:

                            # extracts the source code at the submission id
                            code = source_code_extractor.extract_source_code(str(submission.contest_id), str(submission.id))

                            # writing submission to file
                            file_io.write_to_file(absolute_path, code)

                            # add problem to ignore-list so that it is not fetched next time
                            cfi_ignore.add(problem_id)

                            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:
                    print ex.message

                print ''

            # construct date vs count
            date_vs_info = {}
            for submission in submissions_list:
                submission_date = submission.submission_time.strftime("%Y-%m-%d")
                submission_data = date_vs_info.get(submission_date, [submission.submission_time_components, 0])
                submission_data[1] += 1
                date_vs_info[submission_date] = submission_data


            try:
                # generates html file
                html_generator.generate_html(handle, classifier, dir_path, "classification")

                # generates html file for data visualization
                html_generator.generate_html(handle, classifier, dir_path, "visualization", date_vs_info.values())

                # writes cfi ignore
                cfi_ignore.write_ignore_list()

            except Exception as ex:
                print ex.message
                print 'Error generating html file'

    except Exception as ex:
        print 'Error: ' + ex.message
        print 'Unable to fetch your submissions at the moment.'

    else:
        print 'Import-Status: Successful.'