Beispiel #1
0
 def test_defdict_with_key(self):
     factory = mock.MagicMock()
     testdict = defdict.DefaultDictWithKey(factory)
     # pylint: disable=pointless-statement
     testdict['a']
     testdict['b']
     self.assertEqual(2, len(factory.mock_calls))
     self.assertEqual(('a', ), factory.mock_calls[0][1])
     self.assertEqual(('b', ), factory.mock_calls[1][1])
Beispiel #2
0
    detected = chardet.detect(rawdata)
    encoding = detected['encoding']

    # Ignore encoding errors for reading the contents because input files
    # routinely break this assumption.
    errors = 'ignore'

    with open(filename, encoding=encoding, errors=errors) as file:
        return file.read()


def get_file(filename):
    """Create or reuse a globally registered instance of a FileMemo.

    Note: the FileMemo objects' lifetimes are reused for the duration of the
    process. This is usually the intended behavior. Always create them by
    calling this constructor.

    Args:
      filename: A path string, the absolute name of the file whose memo to create.
    Returns:
      A FileMemo instance.

    """
    assert path.isabs(filename), (
        "Path should be absolute in order to guarantee a single call.")
    return _CACHE[filename]


_CACHE = defdict.DefaultDictWithKey(_FileMemo)