def load(self, callables_fname): """ Loads traced modules information from a `JSON <http://www.json.org/>`_ file. The loaded module information is merged with any existing module information :param callables_fname: File name :type callables_fname: :ref:`FileNameExists` :raises: * OSError (File *[fname]* could not be found) * RuntimeError (Argument \\`callables_fname\\` is not valid) """ # Validate file name _validate_fname(callables_fname) if not os.path.exists(callables_fname): raise OSError( 'File {0} could not be found'.format(callables_fname) ) with open(callables_fname, 'r') as fobj: fdict = json.load(fobj) if sys.hexversion < 0x03000000: # pragma: no cover fdict = _unicode_to_ascii(fdict) self._callables_db.update(fdict['_callables_db']) # Reverse the tuple-to-string conversion that the save method # does due to the fact that JSON keys need to be strings and the # keys of the reverse callable dictionary are tuples where the first # item is a file name and the second item is the starting line of the # callable within that file (dictionary value) rdict = {} for key, value in fdict['_reverse_callables_db'].items(): tokens = key[1:-1].split(',') key = tokens[0].strip()[1:-1] if platform.system().lower() == 'windows': # pragma: no cover while True: tmp = key key = key.replace('\\\\', '\\') if tmp == key: break rdict[(key, int(tokens[1]))] = value self._reverse_callables_db.update(rdict) self._modules_dict.update(fdict['_modules_dict']) self._fnames.update(fdict['_fnames']) self._module_names.extend(fdict['_module_names']) self._class_names.extend(fdict['_class_names']) self._module_names = sorted(list(set(self._module_names))) self._class_names = sorted(list(set(self._class_names)))
def test_make_dir(capsys): """ Test make_dir function behavior """ def mock_os_makedir(file_path): print(file_path) home_dir = os.path.expanduser('~') with mock.patch('os.makedirs', side_effect=mock_os_makedir): fname = os.path.join(home_dir, 'some_dir', 'some_file.ext') putil.misc.make_dir(fname) stdout, _ = capsys.readouterr() actual = repr(os.path.dirname(fname).rstrip())[1:-1] ref = repr(_unicode_to_ascii(stdout.rstrip()))[1:-1] assert actual == ref putil.misc.make_dir( os.path.join(os.path.abspath(os.sep), 'some_file.ext') ) stdout, _ = capsys.readouterr() assert stdout == ''