1# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. 2# 3# Use of this source code is governed by a BSD-style license 4# that can be found in the LICENSE file in the root of the source 5# tree. An additional intellectual property rights grant can be found 6# in the file PATENTS. All contributing project authors may 7# be found in the AUTHORS file in the root of the source tree. 8"""Data access utility functions and classes. 9""" 10 11import json 12import os 13 14 15def MakeDirectory(path): 16 """Makes a directory recursively without rising exceptions if existing. 17 18 Args: 19 path: path to the directory to be created. 20 """ 21 if os.path.exists(path): 22 return 23 os.makedirs(path) 24 25 26class Metadata(object): 27 """Data access class to save and load metadata. 28 """ 29 30 def __init__(self): 31 pass 32 33 _GENERIC_METADATA_SUFFIX = '.mdata' 34 _AUDIO_TEST_DATA_FILENAME = 'audio_test_data.json' 35 36 @classmethod 37 def LoadFileMetadata(cls, filepath): 38 """Loads generic metadata linked to a file. 39 40 Args: 41 filepath: path to the metadata file to read. 42 43 Returns: 44 A dict. 45 """ 46 with open(filepath + cls._GENERIC_METADATA_SUFFIX) as f: 47 return json.load(f) 48 49 @classmethod 50 def SaveFileMetadata(cls, filepath, metadata): 51 """Saves generic metadata linked to a file. 52 53 Args: 54 filepath: path to the metadata file to write. 55 metadata: a dict. 56 """ 57 with open(filepath + cls._GENERIC_METADATA_SUFFIX, 'w') as f: 58 json.dump(metadata, f) 59 60 @classmethod 61 def LoadAudioTestDataPaths(cls, metadata_path): 62 """Loads the input and the reference audio track paths. 63 64 Args: 65 metadata_path: path to the directory containing the metadata file. 66 67 Returns: 68 Tuple with the paths to the input and output audio tracks. 69 """ 70 metadata_filepath = os.path.join(metadata_path, 71 cls._AUDIO_TEST_DATA_FILENAME) 72 with open(metadata_filepath) as f: 73 return json.load(f) 74 75 @classmethod 76 def SaveAudioTestDataPaths(cls, output_path, **filepaths): 77 """Saves the input and the reference audio track paths. 78 79 Args: 80 output_path: path to the directory containing the metadata file. 81 82 Keyword Args: 83 filepaths: collection of audio track file paths to save. 84 """ 85 output_filepath = os.path.join(output_path, 86 cls._AUDIO_TEST_DATA_FILENAME) 87 with open(output_filepath, 'w') as f: 88 json.dump(filepaths, f) 89 90 91class AudioProcConfigFile(object): 92 """Data access to load/save APM simulator argument lists. 93 94 The arguments stored in the config files are used to control the APM flags. 95 """ 96 97 def __init__(self): 98 pass 99 100 @classmethod 101 def Load(cls, filepath): 102 """Loads a configuration file for an APM simulator. 103 104 Args: 105 filepath: path to the configuration file. 106 107 Returns: 108 A dict containing the configuration. 109 """ 110 with open(filepath) as f: 111 return json.load(f) 112 113 @classmethod 114 def Save(cls, filepath, config): 115 """Saves a configuration file for an APM simulator. 116 117 Args: 118 filepath: path to the configuration file. 119 config: a dict containing the configuration. 120 """ 121 with open(filepath, 'w') as f: 122 json.dump(config, f) 123 124 125class ScoreFile(object): 126 """Data access class to save and load float scalar scores. 127 """ 128 129 def __init__(self): 130 pass 131 132 @classmethod 133 def Load(cls, filepath): 134 """Loads a score from file. 135 136 Args: 137 filepath: path to the score file. 138 139 Returns: 140 A float encoding the score. 141 """ 142 with open(filepath) as f: 143 return float(f.readline().strip()) 144 145 @classmethod 146 def Save(cls, filepath, score): 147 """Saves a score into a file. 148 149 Args: 150 filepath: path to the score file. 151 score: float encoding the score. 152 """ 153 with open(filepath, 'w') as f: 154 f.write('{0:f}\n'.format(score)) 155