# Path: g_sorcery/compatibility.py
# class TemporaryDirectory(object):
#     def __init__(self):
#         self.name = mkdtemp()
# 
#     def __del__(self):
#         shutil.rmtree(self.name)
# 
# Path: g_sorcery/exceptions.py
# class FileJSONError(GSorceryError):
#     pass
# 
# class DownloadingError(GSorceryError):
#     pass
# 
# Path: g_sorcery/serialization.py
# class JSONSerializer(json.JSONEncoder):
#     """
#     Custom JSON encoder.
# 
#     Each serializable class should have a method serialize
#     that returns JSON serializable value. If class additionally
#     has a classmethod deserialize that it can be deserialized
#     and additional metainformation is added to the resulting JSON.
#     """
#     def default(self, obj):
#         res = step_to_raw_serializable(obj)
#         if res:
#             return res
#         else:
#             return json.JSONEncoder.default(self, obj)
# 
# def deserializeHook(json_object):
#     """
#     Custom JSON decoder.
# 
#     Each class that can be deserialized should have classmethod deserialize
#     that takes value (previously returned by serialize method) and transforms
#     it into class instance.
#     """
#     return step_from_raw_serializable(json_object)


import glob
import json
import hashlib
import os
import tarfile
from .compatibility import TemporaryDirectory
from .exceptions import FileJSONError, DownloadingError
from .serialization import JSONSerializer, deserializeHook

    Args:
        uri: URI.
        parser: Parser that will be applied to downloaded files.
        open_file: Whether parser accepts a file descriptor.
        open_mode: Open mode for a file.
        output: What output name should downloaded file have.
        timeout: URI access timeout.
    (it will be a key identifying data loaded from this file)

    Returns:
        Dictionary with a loaded data. Key is filename, content is data returned by parser.
    """
    download_dir = TemporaryDirectory()
    loaded_data = {}
    if wget(uri, download_dir.name, output, timeout=timeout):
        raise DownloadingError("wget failed: " + uri)
    for f_name in glob.glob(os.path.join(download_dir.name, "*")):
        if tarfile.is_tarfile(f_name):
            unpack_dir = TemporaryDirectory()
            with tarfile.open(f_name) as f:
                f.extractall(unpack_dir.name)
            for uf_name in glob.glob(os.path.join(unpack_dir, "*")):
                loaded_data.update(_call_parser(uf_name, parser,
                                    open_file=open_file, open_mode=open_mode))
            del unpack_dir
        else:
            name, extention = os.path.splitext(f_name)
            if extention in [".xz", ".lzma"]:
                if (os.system("xz -d " + f_name)):

# Path: g_sorcery/compatibility.py
# class TemporaryDirectory(object):
#     def __init__(self):
#         self.name = mkdtemp()
# 
#     def __del__(self):
#         shutil.rmtree(self.name)
# 
# Path: g_sorcery/exceptions.py
# class FileJSONError(GSorceryError):
#     pass
# 
# class DownloadingError(GSorceryError):
#     pass
# 
# Path: g_sorcery/serialization.py
# class JSONSerializer(json.JSONEncoder):
#     """
#     Custom JSON encoder.
# 
#     Each serializable class should have a method serialize
#     that returns JSON serializable value. If class additionally
#     has a classmethod deserialize that it can be deserialized
#     and additional metainformation is added to the resulting JSON.
#     """
#     def default(self, obj):
#         res = step_to_raw_serializable(obj)
#         if res:
#             return res
#         else:
#             return json.JSONEncoder.default(self, obj)
# 
# def deserializeHook(json_object):
#     """
#     Custom JSON decoder.
# 
#     Each class that can be deserialized should have classmethod deserialize
#     that takes value (previously returned by serialize method) and transforms
#     it into class instance.
#     """
#     return step_from_raw_serializable(json_object)


# Path: g_sorcery/fileutils.py
import glob
import json
import hashlib
import os
import tarfile
from .compatibility import TemporaryDirectory
from .exceptions import FileJSONError, DownloadingError
from .serialization import JSONSerializer, deserializeHook


    Args:
        uri: URI.
        parser: Parser that will be applied to downloaded files.
        open_file: Whether parser accepts a file descriptor.
        open_mode: Open mode for a file.
        output: What output name should downloaded file have.
        timeout: URI access timeout.
    (it will be a key identifying data loaded from this file)

    Returns:
        Dictionary with a loaded data. Key is filename, content is data returned by parser.
    """
    download_dir = TemporaryDirectory()
    loaded_data = {}
    if wget(uri, download_dir.name, output, timeout=timeout):
        raise DownloadingError("wget failed: " + uri)
    for f_name in glob.glob(os.path.join(download_dir.name, "*")):
        if tarfile.is_tarfile(f_name):
            unpack_dir = TemporaryDirectory()
            with tarfile.open(f_name) as f:
                f.extractall(unpack_dir.name)
            for uf_name in glob.glob(os.path.join(unpack_dir, "*")):
                loaded_data.update(_call_parser(uf_name, parser,
                                    open_file=open_file, open_mode=open_mode))
            del unpack_dir
        else:
            name, extention = os.path.splitext(f_name)
            if extention in [".xz", ".lzma"]:
                if (os.system("xz -d " + f_name)):

                    raise DownloadingError("xz failed: "