Commit 75ee2531 authored by Kai Chen's avatar Kai Chen
Browse files

rename *Processor to *Handler and fix bug for python 2

parent 31e684fc
from .io import * from .io import load, dump
from .processors import * from .handlers import BaseFileHandler, JsonHandler, PickleHandler, YamlHandler
from .parse import * from .parse import list_from_file, dict_from_file
__all__ = [
'load', 'dump', 'BaseFileHandler', 'JsonHandler', 'PickleHandler',
'YamlHandler', 'list_from_file', 'dict_from_file'
]
from .base import BaseFileHandler
from .json_handler import JsonHandler
from .pickle_handler import PickleHandler
from .yaml_handler import YamlHandler
__all__ = ['BaseFileHandler', 'JsonHandler', 'PickleHandler', 'YamlHandler']
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
class BaseFileProcessor(object): class BaseFileHandler(object):
__metaclass__ = ABCMeta __metaclass__ = ABCMeta
......
import json import json
from .base import BaseFileProcessor from .base import BaseFileHandler
class JsonProcessor(BaseFileProcessor): class JsonHandler(BaseFileHandler):
@staticmethod @staticmethod
def load_from_path(filepath): def load_from_path(filepath):
......
from six.moves import cPickle as pickle from six.moves import cPickle as pickle
from .base import BaseFileProcessor from .base import BaseFileHandler
class PickleProcessor(BaseFileProcessor): class PickleHandler(BaseFileHandler):
@staticmethod @staticmethod
def load_from_path(filepath, **kwargs): def load_from_path(filepath, **kwargs):
...@@ -30,4 +30,3 @@ class PickleProcessor(BaseFileProcessor): ...@@ -30,4 +30,3 @@ class PickleProcessor(BaseFileProcessor):
def dump_to_fileobj(obj, file, **kwargs): def dump_to_fileobj(obj, file, **kwargs):
kwargs.setdefault('protocol', 2) kwargs.setdefault('protocol', 2)
pickle.dump(obj, file, **kwargs) pickle.dump(obj, file, **kwargs)
\ No newline at end of file
...@@ -4,10 +4,10 @@ try: ...@@ -4,10 +4,10 @@ try:
except ImportError: except ImportError:
from yaml import Loader, Dumper from yaml import Loader, Dumper
from .base import BaseFileProcessor from .base import BaseFileHandler
class YamlProcessor(BaseFileProcessor): class YamlHandler(BaseFileHandler):
@staticmethod @staticmethod
def load_from_path(filepath, **kwargs): def load_from_path(filepath, **kwargs):
......
from .processors import JsonProcessor, PickleProcessor, YamlProcessor from .handlers import JsonHandler, PickleHandler, YamlHandler
from ..utils import is_str from ..utils import is_str
file_processors = { file_handlers = {
'json': JsonProcessor, 'json': JsonHandler,
'yaml': YamlProcessor, 'yaml': YamlHandler,
'yml': YamlProcessor, 'yml': YamlHandler,
'pickle': PickleProcessor, 'pickle': PickleHandler,
'pkl': PickleProcessor 'pkl': PickleHandler
} }
...@@ -27,14 +27,14 @@ def load(file, file_format=None, **kwargs): ...@@ -27,14 +27,14 @@ def load(file, file_format=None, **kwargs):
""" """
if file_format is None and is_str(file): if file_format is None and is_str(file):
file_format = file.split('.')[-1] file_format = file.split('.')[-1]
if file_format not in file_processors: if file_format not in file_handlers:
raise TypeError('Unsupported format: {}'.format(file_format)) raise TypeError('Unsupported format: {}'.format(file_format))
processor = file_processors[file_format] handler = file_handlers[file_format]
if is_str(file): if is_str(file):
obj = processor.load_from_path(file, **kwargs) obj = handler.load_from_path(file, **kwargs)
elif hasattr(file, 'read'): elif hasattr(file, 'read'):
obj = processor.load_from_fileobj(file, **kwargs) obj = handler.load_from_fileobj(file, **kwargs)
else: else:
raise TypeError('"file" must be a filepath str or a file-object') raise TypeError('"file" must be a filepath str or a file-object')
return obj return obj
...@@ -54,7 +54,7 @@ def dump(obj, file=None, file_format=None, **kwargs): ...@@ -54,7 +54,7 @@ def dump(obj, file=None, file_format=None, **kwargs):
file_format (str, optional): Same as :func:`load`. file_format (str, optional): Same as :func:`load`.
Returns: Returns:
bool: True for success, False otherwise bool: True for success, False otherwise.
""" """
if file_format is None: if file_format is None:
if is_str(file): if is_str(file):
...@@ -62,15 +62,15 @@ def dump(obj, file=None, file_format=None, **kwargs): ...@@ -62,15 +62,15 @@ def dump(obj, file=None, file_format=None, **kwargs):
elif file is None: elif file is None:
raise ValueError( raise ValueError(
'file_format must be specified since file is None') 'file_format must be specified since file is None')
if file_format not in file_processors: if file_format not in file_handlers:
raise TypeError('Unsupported format: {}'.format(file_format)) raise TypeError('Unsupported format: {}'.format(file_format))
processor = file_processors[file_format] handler = file_handlers[file_format]
if file is None: if file is None:
return processor.dump_to_str(obj, **kwargs) return handler.dump_to_str(obj, **kwargs)
elif is_str(file): elif is_str(file):
processor.dump_to_path(obj, file, **kwargs) handler.dump_to_path(obj, file, **kwargs)
elif hasattr(file, 'write'): elif hasattr(file, 'write'):
processor.dump_to_fileobj(obj, file, **kwargs) handler.dump_to_fileobj(obj, file, **kwargs)
else: else:
raise TypeError('"file" must be a filename str or a file-object') raise TypeError('"file" must be a filename str or a file-object')
from .base import BaseFileProcessor
from .json import JsonProcessor
from .pickle import PickleProcessor
from .yaml import YamlProcessor
...@@ -6,7 +6,7 @@ import mmcv ...@@ -6,7 +6,7 @@ import mmcv
import pytest import pytest
def _test_processor(file_format, test_obj, str_checker, mode='r+'): def _test_handler(file_format, test_obj, str_checker, mode='r+'):
# dump to a string # dump to a string
dump_str = mmcv.dump(test_obj, file_format=file_format) dump_str = mmcv.dump(test_obj, file_format=file_format)
str_checker(dump_str) str_checker(dump_str)
...@@ -49,7 +49,7 @@ def test_json(): ...@@ -49,7 +49,7 @@ def test_json():
'[{"a": "abc", "b": 1}, 2, "c"]', '[{"b": 1, "a": "abc"}, 2, "c"]' '[{"a": "abc", "b": 1}, 2, "c"]', '[{"b": 1, "a": "abc"}, 2, "c"]'
] ]
_test_processor('json', obj_for_test, json_checker) _test_handler('json', obj_for_test, json_checker)
def test_yaml(): def test_yaml():
...@@ -59,7 +59,7 @@ def test_yaml(): ...@@ -59,7 +59,7 @@ def test_yaml():
'- {a: abc, b: 1}\n- 2\n- c\n', '- {b: 1, a: abc}\n- 2\n- c\n' '- {a: abc, b: 1}\n- 2\n- c\n', '- {b: 1, a: abc}\n- 2\n- c\n'
] ]
_test_processor('yaml', obj_for_test, yaml_checker) _test_handler('yaml', obj_for_test, yaml_checker)
def test_pickle(): def test_pickle():
...@@ -68,7 +68,7 @@ def test_pickle(): ...@@ -68,7 +68,7 @@ def test_pickle():
import pickle import pickle
assert pickle.loads(dump_str) == obj_for_test assert pickle.loads(dump_str) == obj_for_test
_test_processor('pickle', obj_for_test, pickle_checker, mode='rb+') _test_handler('pickle', obj_for_test, pickle_checker, mode='rb+')
def test_exception(): def test_exception():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment