def get_last_successful(logger, config_name):
    """
    Read the date and time of the last successfully exported audit data from the sync marker file

    :param logger:  the logger
    :return:        A datetime value (or 2000-01-01 if syncing since the 'beginning of time')
    :config_name:

    """
    last_successful_file = set_last_successful_file_name(config_name, 'audits')

    if os.path.isfile(last_successful_file):
        with open(last_successful_file, 'r+') as last_run:
            check_for_rows = last_run.readlines()
            if check_for_rows:
                last_successful = check_for_rows[0]
                last_successful = last_successful.strip()
            else:
                last_successful = '2000-01-01T00:00:00.000Z'

    else:
        beginning_of_time = '2000-01-01T00:00:00.000Z'
        last_successful = beginning_of_time
        create_directory_if_not_exists(logger, 'last_successful')
        with open(last_successful_file, 'w') as last_run:
            last_run.write(last_successful)
        logger.info('Searching for audits since the beginning of time: ' +
                    beginning_of_time)
    return last_successful
Beispiel #2
0
def initial_setup(logger):
    """
    Creates a new directory in current working directory called 'iauditor_exports_folder'.  If 'iauditor_exports_folder'
    already exists the setup script will notify user that the folder exists and exit. Default config file placed
    in directory, with user API Token. User is asked for iAuditor credentials in order to generate their
    API token.
    :param logger:  the logger
    """

    # setup variables
    current_directory_path = os.getcwd()
    exports_folder_name = 'configs'

    create_directory_if_not_exists(logger, exports_folder_name)

    config_files = os.listdir(exports_folder_name)

    if config_files:
        if len(config_files) > 1:
            config_to_edit = questionary.select(
                "It looks like you already have some config files. Which one do you want to modify?",
                choices=config_files
            ).ask()
        else:
            config_to_edit = config_files[0]
        config_path = os.path.join(exports_folder_name, config_to_edit)
        with open(os.path.join(exports_folder_name, config_to_edit)) as file:
            settings = yaml.load(file, Loader=yaml.FullLoader)
    else:
        settings = model_config
        config_to_edit = 'config.yaml'
        config_path = os.path.join(exports_folder_name, config_to_edit)
    if 'config_name' in settings:
        config_name = settings['config_name']
    else:
        print('Your config file is missing config_name. Delete the file, '
              're run the tool and it will be recreated correctly.')

    current_ls = get_last_successful(logger, config_name)
    parsed_date = parse_ls(current_ls)
    if parsed_date:
        pretty_file_name = parsed_date.strftime("%Y-%m-%d at %H%M")

        update_ls_q = ask_question(logger, f'We are currently searching for inspections completed after {pretty_file_name}, would you '
              f'like to change this?', 'bool')
        if update_ls_q:
            print(box_print('''
            You can use natural language here. For example if you wanted to start the search from March 22nd 2018, 
            you could simply type 'March 22nd 2018'
            '''))
            change_ls = ask_question(logger, 'When should we search from? If you want to start from the beginning, '
                                             'just press enter.', 'text')
            new_date = parse_ls(change_ls)
            if new_date:
                new_date = new_date.strftime("%Y-%m-%dT%H:%M:%S%z")
                update_sync_marker_file(str(new_date), config_name)

    modify_choice(logger, settings, config_path)

    sys.exit()
Beispiel #3
0
def configure(logger, path_to_config_file, export_formats, docker_enabled):
    """
    instantiate and configure logger, load config settings from file, instantiate SafetyCulture SDK
    :param logger:              the logger
    :param path_to_config_file: path to config file
    :param export_formats:      desired export formats
    :return:                    instance of SafetyCulture SDK object, config settings
    """

    config_settings = load_config_settings(logger, path_to_config_file,
                                           docker_enabled)
    config_settings[EXPORT_FORMATS] = export_formats
    if config_settings[PROXY_HTTP] is not None and config_settings[
            PROXY_HTTPS] is not None:
        proxy_settings = {
            "http": config_settings[PROXY_HTTP],
            "https": config_settings[PROXY_HTTPS]
        }
    else:
        proxy_settings = None

    sc_client = sp.SafetyCulture(
        config_settings[API_TOKEN],
        proxy_settings=proxy_settings,
        certificate_settings=config_settings[SSL_CERT],
        ssl_verify=config_settings[SSL_VERIFY])

    if config_settings[EXPORT_PATH] is not None:
        if config_settings[CONFIG_NAME] is not None:
            create_directory_if_not_exists(
                logger, os.path.join(config_settings[EXPORT_PATH]))
        else:
            logger.error(
                "You must set the config_name in your config file before continuing."
            )
            sys.exit()
    else:
        logger.info('No export path was found in ' + path_to_config_file +
                    ', defaulting to /exports')
        config_settings[EXPORT_PATH] = os.path.join(os.getcwd(), 'exports')
        if config_settings[CONFIG_NAME] is not None:
            create_directory_if_not_exists(
                logger, os.path.join(config_settings[EXPORT_PATH]))
        else:
            logger.error(
                "You must set the config_name in your config file before continuing."
            )
            sys.exit()

    return sc_client, config_settings
Beispiel #4
0
def save_exported_document(logger, export_dir, export_doc, filename, extension):
    """
    Write exported document to disk at specified location with specified file name.
    Any existing file with the same name will be overwritten.
    :param logger:      the logger
    :param export_dir:  path to directory for exports
    :param export_doc:  export document to write
    :param filename:    filename to give exported document
    :param extension:   extension to give exported document
    """
    path = os.path.join(export_dir, extension)
    file_path = os.path.join(export_dir, extension, filename + '.' + extension)
    create_directory_if_not_exists(logger, path)
    if os.path.isfile(file_path):
        logger.info('Overwriting existing report at ' + file_path)
    try:
        with open(file_path, 'wb') as export_file:
            export_file.write(export_doc)
    except Exception as ex:
        log_critical_error(logger, ex, 'Exception while writing' + file_path + ' to file')