2019-12-25 10:08:11 +00:00
|
|
|
from typing import Type
|
2019-12-25 15:12:20 +00:00
|
|
|
from pathlib import Path
|
2019-12-23 13:56:48 +00:00
|
|
|
from .idatahandler import IDataHandler
|
|
|
|
|
|
|
|
|
2019-12-25 10:08:11 +00:00
|
|
|
def get_datahandlerclass(datatype: str) -> Type[IDataHandler]:
|
2019-12-23 13:56:48 +00:00
|
|
|
"""
|
|
|
|
Get datahandler class.
|
|
|
|
Could be done using Resolvers, but since this may be called often and resolvers
|
|
|
|
are rather expensive, doing this directly should improve performance.
|
|
|
|
:param datatype: datatype to use.
|
|
|
|
:return: Datahandler class
|
|
|
|
"""
|
|
|
|
|
|
|
|
if datatype == 'json':
|
|
|
|
from .jsondatahandler import JsonDataHandler
|
|
|
|
return JsonDataHandler
|
|
|
|
elif datatype == 'jsongz':
|
|
|
|
from .jsondatahandler import JsonGzDataHandler
|
|
|
|
return JsonGzDataHandler
|
|
|
|
else:
|
|
|
|
raise ValueError(f"No datahandler for datatype {datatype} available.")
|
2019-12-25 15:12:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_datahandler(datadir: Path, data_format: str = None,
|
|
|
|
data_handler: IDataHandler = None) -> IDataHandler:
|
|
|
|
"""
|
|
|
|
:param datadir: Folder to save data
|
|
|
|
:data_format: dataformat to use
|
|
|
|
:data_handler: returns this datahandler if it exists or initializes a new one
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not data_handler:
|
|
|
|
HandlerClass = get_datahandlerclass(data_format or 'json')
|
|
|
|
data_handler = HandlerClass(datadir)
|
|
|
|
return data_handler
|