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
9"""Data access utility functions and classes.
10"""
11
12import json
13import os
14
15
16def MakeDirectory(path):
17  """Makes a directory recursively without rising exceptions if existing.
18
19  Args:
20    path: path to the directory to be created.
21  """
22  if os.path.exists(path):
23    return
24  os.makedirs(path)
25
26
27class Metadata(object):
28  """Data access class to save and load metadata.
29  """
30
31  def __init__(self):
32    pass
33
34  _GENERIC_METADATA_SUFFIX = '.mdata'
35  _AUDIO_TEST_DATA_FILENAME = 'audio_test_data.json'
36
37  @classmethod
38  def LoadFileMetadata(cls, filepath):
39    """Loads generic metadata linked to a file.
40
41    Args:
42      filepath: path to the metadata file to read.
43
44    Returns:
45      A dict.
46    """
47    with open(filepath + cls._GENERIC_METADATA_SUFFIX) as f:
48      return json.load(f)
49
50  @classmethod
51  def SaveFileMetadata(cls, filepath, metadata):
52    """Saves generic metadata linked to a file.
53
54    Args:
55      filepath: path to the metadata file to write.
56      metadata: a dict.
57    """
58    with open(filepath + cls._GENERIC_METADATA_SUFFIX, 'w') as f:
59      json.dump(metadata, f)
60
61  @classmethod
62  def LoadAudioTestDataPaths(cls, metadata_path):
63    """Loads the input and the reference audio track paths.
64
65    Args:
66      metadata_path: path to the directory containing the metadata file.
67
68    Returns:
69      Tuple with the paths to the input and output audio tracks.
70    """
71    metadata_filepath = os.path.join(
72        metadata_path, cls._AUDIO_TEST_DATA_FILENAME)
73    with open(metadata_filepath) as f:
74      return json.load(f)
75
76  @classmethod
77  def SaveAudioTestDataPaths(cls, output_path, **filepaths):
78    """Saves the input and the reference audio track paths.
79
80    Args:
81      output_path: path to the directory containing the metadata file.
82
83    Keyword Args:
84      filepaths: collection of audio track file paths to save.
85    """
86    output_filepath = os.path.join(output_path, 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