Exemple #1
0
 def test_json_read_write_success(self):
     ss_file_path = os.path.join(self.temp_dir, 'ss_dis.json')
     self.assertFalse(exists(ss_file_path))
     write_json(self.ss_dis_dic, ss_file_path)
     self.assertTrue(exists(ss_file_path))
     result = read_json(ss_file_path)
     self.assertEqual(self.ss_dis_dic, result)
     return None
Exemple #2
0
def fetch_ss_dis(dir_path):
    """Return a processed dictionary for ss_dis data.

    Args:
        dir_path (Unicode): The dir path where ss_dis files are located.

    Returns:
        ss_dis_data (dict): A dictionary of processed ss_dis data.

    """
    working_path = os.path.abspath(dir_path)
    ss_dis_files = _find_existing_files(working_path)

    if ss_dis_files['files_to_archive']:
        for name_to_archive in ss_dis_files['files_to_archive']:
            path_to_archive = os.path.join(working_path, name_to_archive)
            _archive_ss_data(
                path_to_archive
            )

    if ss_dis_files['valid_raw_file']:
        valid_raw_fp = os.path.join(
            working_path, ss_dis_files['valid_raw_file']
        )
    else:
        valid_raw_fp = None

    if ss_dis_files['valid_json_file']:
        valid_json_fp = os.path.join(
            working_path, ss_dis_files['valid_json_file']
        )
    else:
        valid_json_fp = None

        # If we a valid pair exists, use the json to return a dictionary.
    if valid_raw_fp and valid_json_fp:
        assert os.path.isfile(valid_raw_fp)
        assert os.path.isfile(valid_json_fp)
        current_json_path = valid_json_fp

    # Generate a companion json file if a single raw file is found.
    elif valid_raw_fp:
        valid_raw_fn = os.path.basename(valid_raw_fp)
        assert not valid_json_fp
        this_timestamp = SS_DIS_PAT.search(valid_raw_fn).group(2)
        companion_json = "{}.{}.{}".format(
            'ss_dis',
            this_timestamp,
            'json'
        )
        companion_json_path = os.path.join(working_path, companion_json)
        ss_dict = _generate_ss_dict(valid_raw_fp)
        write_json(ss_dict, companion_json_path)
        current_json_path = companion_json_path

    # Download new data and generate json file.
    elif not (valid_raw_fp or valid_json_fp):
        new_names = _new_filenames()
        new_raw_path = os.path.join(working_path, new_names.raw)
        new_json_path = os.path.join(working_path, new_names.json)

        _download_ss_data(new_raw_path)
        ss_dict = _generate_ss_dict(new_raw_path)
        write_json(ss_dict, new_json_path)
        current_json_path = new_json_path

    elif valid_raw_fp and not valid_json_fp:
        raise RuntimeError("Should not have a JSON file without a TXT file.")

    else:
        raise RuntimeError("Unhandled case.")

    # Always return the ss_dis dictionary by reading the json
    # file to ensure consistency of future runs.
    ss_dis_data = read_json(current_json_path)

    return ss_dis_data