예제 #1
0
    def get_credentials(self):
        """
        Gets valid user credentials from storage.

        If nothing has been stored, or if the stored credentials are invalid,
        the OAuth2 flow is completed to obtain the new credentials.

        Same example code from Google.
        :return: Credentials, the obtained credential.
        """
        home_dir = os.path.expanduser('~')
        credential_dir = os.path.join(home_dir, '.credentials')
        if not os.path.exists(credential_dir):
            os.makedirs(credential_dir)
        credential_path = os.path.join(
            credential_dir, 'sheets.googleapis.com-python-quickstart.json')
        store = Storage(credential_path)
        credentials = store.get()
        if not credentials or credentials.invalid:
            flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
            flow.user_agent = APPLICATION_NAME
            if flags:
                credentials = tools.run_flow(flow, store, flags)
            cool_print_decoration('Storing credentials to ' +
                                credential_path, style='info')
        return credentials
예제 #2
0
def classify(file_name: str):
    ## Set initial params
    with open(file_name, 'r') as f:
        f = json.load(f)
    year = f['year']
    semester = f['semester']
    course = f['course']
    evaluation = f['evaluation']
    files = f['files']
    ocr = f['ocr']
    sheet_id = f['evaluation_sheet_id']
    g_sheets_file = f'results/{course}/{year}-{semester}/{evaluation}'
    if (not check_downloaded_google_sheet_data(g_sheets_file)):
        g_sheets(file_save_path=g_sheets_file, sheets_data='sheets_data.txt', spreadsheet_id=sheet_id)
    student_list = create_student_instances(g_sheets_file, Student)
    # Iterate for each file === one question P1 / P2 ... PN
    for info in files:
        question = list(info.keys())[0]
        path = info[question]
        file = Files(course_name=course, evaluation_name=evaluation, file_path=path, question_name=question)
        cool_print_decoration(f'Processing question:{question}\nOCR: {ocr}', style='info')
        cool_print('Transforming .pdf into .png', style='info')
        file_list = multiple_pdf_to_png(f_name=path, save=False)
        cool_print('Finished transforming .pdf into .png!', style='info')
        file.set_file_list(file_list)
        file_classifier(file, student_list=student_list, ocr=ocr)
예제 #3
0
def main(_file='', _data={}) -> int:

    if (_file):
        _data = read_data(_file) if check_file(_file) else {}

        if not _data:
            print_invalid_file(_file)
            return 0

    COURSE_NAME = _data['course']
    EVALUATION = _data['evaluation']
    OCR = _data['ocr']
    COPIES = _data['copies'] if _data.get('copies') else 1

    create_directory(f'{os.getcwd()}/ANSWER_SHEETS/') if not os.path.exists(
        f'{os.getcwd()}/ANSWER_SHEETS/') else None

    ANSWER_SHEETS_DIR_PATH = f'{os.getcwd()}/ANSWER_SHEETS/{COURSE_NAME}_{EVALUATION}'

    cool_print(
        f'\nAnswer sheets will be available in: {ANSWER_SHEETS_DIR_PATH}',
        style='result')

    _current_time = asctime(localtime(time()))
    cool_print(f'\n\nInitializing program...[{_current_time}]', style='info')

    if png_template_exists(f'{TEMPLATE_DIRECTORY}/{_data["template"]}'):
        cool_print_decoration(
            'ERROR: Found template in .pdf format.\nConverting template to .png format...',
            'danger')
        pdf_to_png(f'{TEMPLATE_DIRECTORY}/{_data["template"]}.pdf')

    create_directory(ANSWER_SHEETS_DIR_PATH
                     ) if not os.path.exists(ANSWER_SHEETS_DIR_PATH) else None

    _data['template'] = f'{TEMPLATE_DIRECTORY}/{_data["template"]}.png'
    cool_print(f'\nPreparing data...', style='info')
    _files = make_files(_data, _save_file=False)

    cool_print(f'\nAdding texts to templates...', style='info')
    _answer_sheets = composite_multiple_images(
        _files, _save_pages=False, _save_path=ANSWER_SHEETS_DIR_PATH, ocr=OCR)

    _answer_sheets = _answer_sheets if COPIES == 1 else make_copies(
        _answer_sheets, COPIES)

    cool_print(f'\nMerging files...[could take a while]', style='info')
    pdf_merger(_answer_sheets, f'{ANSWER_SHEETS_DIR_PATH}/compilation.pdf')

    cool_print(f'\nDone!', style='info')

    return 1
예제 #4
0
def read_requirements(req_path: str) -> None:
    _installed = installed_packages()
    _req = ''
    with open(req_path, 'r') as f:
        _req = set([mod_name.strip().split('==')[0] for mod_name in f])
    _filered_modules = set(
        filter(lambda mod_name: mod_name in _req, _installed))
    if _filered_modules.difference(_req):
        txt = ''
        for name in _filered_modules.difference(_req):
            txt += 'Missing: {} module.\n'.format(name)
        cool_print_decoration(txt, 'danger')
    else:
        cool_print_decoration('Requirements check!', 'result')
예제 #5
0
 def classify_data(self, ocr):
     """
     Applies pytesseract.image_to_string for each Image instance found if ocr == text
     Applies other classifier depending on ocr
     """
     cool_print_decoration(f'Starting classification with ocr: {ocr}',
                           style='info')
     if (ocr == 'text'):
         self.pytesseract_classifier()
     elif (ocr == 'qr'):
         cool_print('Starting part I: Data decoding', style='info')
         self.qr_classifier.decode_data()
         cool_print('Ended part I: Data decoding\n', style='info')
         cool_print('Starting part II: Classification', style='info')
         self.qr_classifier.classify()
         cool_print('Ending part II: Classification\n', style='info')
예제 #6
0
 def __service_sheets(_range, extract=True):
     """
     Calls Google's service and returns array result from an api call
     :param _range: Cell range to look for
     :param extract: Boolean that indicates top level extraction
     :return: array of elements resulting from api call
     """
     try:
         result = service.get(spreadsheetId=GoogleSheets.SPREADSHEET_ID, range=_range).execute()[
             'values']
     except HttpError as e:
         cool_print_decoration(
             "Request limit reached. Try again later (>100s)\n{}".format(e), style='alert')
         sys.exit(0)
     else:
         return result[0] if extract else result
예제 #7
0
 def fetch_data(self):
     header_range = self.get_header_range()
     range_generator = self.get_next_range(header_range)
     students_data = {}
     key_error = False
     data: list
     while not key_error:
         nxt = next(range_generator)
         row = self.get_row_number(nxt)
         if (int(row) % 90 == 0):
             # API limit requests: 100 requests per 100s per user
             cool_print_decoration(
                 'Too many requests. Going to sleep...', style='info')
             sleep(100)
             cool_print_decoration('Ready to work again!', style='result')
         try:
             data = self._service(nxt)
             print(data)
             if len(data) > 1:
                 student_id = data[0]
                 student_array = data[1:]
                 students_data[student_id] = student_array
         except KeyError:
             cool_print_decoration('Done with API.', style='result')
             key_error = True
     self._students_data = students_data
예제 #8
0
def print_invalid_options():
    _msg = 'Error.\nPlease check available flags commands for CLI or check file path and name'
    cool_print_decoration(_msg, 'danger')
예제 #9
0
        range_generator = self.get_next_range(header_range)
        students_data = {}
        key_error = False
        data: list
        while not key_error:
            nxt = next(range_generator)
            row = self.get_row_number(nxt)
            if (int(row) % 90 == 0):
                # API limit requests: 100 requests per 100s per user
                cool_print_decoration(
                    'Too many requests. Going to sleep...', style='info')
                sleep(100)
                cool_print_decoration('Ready to work again!', style='result')
            try:
                data = self._service(nxt)
                print(data)
                if len(data) > 1:
                    student_id = data[0]
                    student_array = data[1:]
                    students_data[student_id] = student_array
            except KeyError:
                cool_print_decoration('Done with API.', style='result')
                key_error = True
        self._students_data = students_data

if __name__ == '__main__':
    google_sheets = GoogleSheets(_type='SHEETS')
    google_sheets.authorize_credentials()
    google_sheets.create_service()
    cool_print_decoration('Connection with API successful.', style='result')
예제 #10
0
def main(file_save_path='', sheets_data='', spreadsheet_id='') -> int:
    """
    :param: file_save_path: should include
        abspath/results/:course_name:/:evaluation_name:
    """
    if path_exists('{}/{}'.format(file_save_path, STUDENTS_FILE_NAME)):
        cool_print_decoration(
            'Information already stored in {}/{}\n\nTo update data, delete previous file.'
            .format(file_save_path, STUDENTS_FILE_NAME),
            style='info')
        return 0
    google_sheets = GoogleSheets(_type='SHEETS',
                                 sheets_data=sheets_data,
                                 spreadsheet_id=spreadsheet_id)
    google_sheets.authorize_credentials()
    google_sheets.create_service()
    cool_print_decoration('Connection with API successful.', style='result')
    cool_print_decoration('Starting data recolection.', style='info')
    google_sheets.fetch_data()
    cool_print_decoration('Saving data in {}.'.format(file_save_path),
                          style='info')

    if not path_exists('{}'.format(file_save_path)):
        _dir_list = file_save_path.split('/')
        create_directories(dir_list=_dir_list)
        cool_print_decoration(
            'Creating path directory {} path.'.format(file_save_path),
            style='info')

    students_data = google_sheets.students_data
    cool_print_decoration('Storing information in {}/{}'.format(
        file_save_path, STUDENTS_FILE_NAME),
                          style='info')
    with open("{}/{}".format(file_save_path, STUDENTS_FILE_NAME), 'w') as file:
        json.dump(students_data, file, indent=4)
    cool_print_decoration('Information stored!', style='result')
    return 1