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

pull/8/head
Kai Chen 2018-09-21 14:00:56 +08:00
parent 31e684fcfe
commit 75ee2531a0
9 changed files with 42 additions and 36 deletions

View File

@ -1,3 +1,8 @@
from .io import *
from .processors import *
from .parse import *
from .io import load, dump
from .handlers import BaseFileHandler, JsonHandler, PickleHandler, YamlHandler
from .parse import list_from_file, dict_from_file
__all__ = [
'load', 'dump', 'BaseFileHandler', 'JsonHandler', 'PickleHandler',
'YamlHandler', 'list_from_file', 'dict_from_file'
]

View File

@ -0,0 +1,6 @@
from .base import BaseFileHandler
from .json_handler import JsonHandler
from .pickle_handler import PickleHandler
from .yaml_handler import YamlHandler
__all__ = ['BaseFileHandler', 'JsonHandler', 'PickleHandler', 'YamlHandler']

View File

@ -1,7 +1,7 @@
from abc import ABCMeta, abstractmethod
class BaseFileProcessor(object):
class BaseFileHandler(object):
__metaclass__ = ABCMeta

View File

@ -1,9 +1,9 @@
import json
from .base import BaseFileProcessor
from .base import BaseFileHandler
class JsonProcessor(BaseFileProcessor):
class JsonHandler(BaseFileHandler):
@staticmethod
def load_from_path(filepath):

View File

@ -1,9 +1,9 @@
from six.moves import cPickle as pickle
from .base import BaseFileProcessor
from .base import BaseFileHandler
class PickleProcessor(BaseFileProcessor):
class PickleHandler(BaseFileHandler):
@staticmethod
def load_from_path(filepath, **kwargs):
@ -30,4 +30,3 @@ class PickleProcessor(BaseFileProcessor):
def dump_to_fileobj(obj, file, **kwargs):
kwargs.setdefault('protocol', 2)
pickle.dump(obj, file, **kwargs)

View File

@ -4,10 +4,10 @@ try:
except ImportError:
from yaml import Loader, Dumper
from .base import BaseFileProcessor
from .base import BaseFileHandler
class YamlProcessor(BaseFileProcessor):
class YamlHandler(BaseFileHandler):
@staticmethod
def load_from_path(filepath, **kwargs):

View File

@ -1,12 +1,12 @@
from .processors import JsonProcessor, PickleProcessor, YamlProcessor
from .handlers import JsonHandler, PickleHandler, YamlHandler
from ..utils import is_str
file_processors = {
'json': JsonProcessor,
'yaml': YamlProcessor,
'yml': YamlProcessor,
'pickle': PickleProcessor,
'pkl': PickleProcessor
file_handlers = {
'json': JsonHandler,
'yaml': YamlHandler,
'yml': YamlHandler,
'pickle': PickleHandler,
'pkl': PickleHandler
}
@ -27,14 +27,14 @@ def load(file, file_format=None, **kwargs):
"""
if file_format is None and is_str(file):
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))
processor = file_processors[file_format]
handler = file_handlers[file_format]
if is_str(file):
obj = processor.load_from_path(file, **kwargs)
obj = handler.load_from_path(file, **kwargs)
elif hasattr(file, 'read'):
obj = processor.load_from_fileobj(file, **kwargs)
obj = handler.load_from_fileobj(file, **kwargs)
else:
raise TypeError('"file" must be a filepath str or a file-object')
return obj
@ -54,7 +54,7 @@ def dump(obj, file=None, file_format=None, **kwargs):
file_format (str, optional): Same as :func:`load`.
Returns:
bool: True for success, False otherwise
bool: True for success, False otherwise.
"""
if file_format is None:
if is_str(file):
@ -62,15 +62,15 @@ def dump(obj, file=None, file_format=None, **kwargs):
elif file is None:
raise ValueError(
'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))
processor = file_processors[file_format]
handler = file_handlers[file_format]
if file is None:
return processor.dump_to_str(obj, **kwargs)
return handler.dump_to_str(obj, **kwargs)
elif is_str(file):
processor.dump_to_path(obj, file, **kwargs)
handler.dump_to_path(obj, file, **kwargs)
elif hasattr(file, 'write'):
processor.dump_to_fileobj(obj, file, **kwargs)
handler.dump_to_fileobj(obj, file, **kwargs)
else:
raise TypeError('"file" must be a filename str or a file-object')

View File

@ -1,4 +0,0 @@
from .base import BaseFileProcessor
from .json import JsonProcessor
from .pickle import PickleProcessor
from .yaml import YamlProcessor

View File

@ -6,7 +6,7 @@ import mmcv
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_str = mmcv.dump(test_obj, file_format=file_format)
str_checker(dump_str)
@ -49,7 +49,7 @@ def test_json():
'[{"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():
@ -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'
]
_test_processor('yaml', obj_for_test, yaml_checker)
_test_handler('yaml', obj_for_test, yaml_checker)
def test_pickle():
@ -68,7 +68,7 @@ def test_pickle():
import pickle
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():