# Path: build/plugin/src/resources/libraries/youtube_dl/compat.py
# def compat_getenv(key, default=None):
#     from .utils import get_filesystem_encoding
#     env = os.getenv(key, default)
#     if env:
#         env = env.decode(get_filesystem_encoding())
#     return env
# 
# Path: build/plugin/src/resources/libraries/youtube_dl/utils.py
# def expand_path(s):
#     """Expand shell variables and ~"""
#     return os.path.expandvars(compat_expanduser(s))
# 
# def write_json_file(obj, fn):
#     """ Encode obj as JSON and write it to fn, atomically if possible """
# 
#     fn = encodeFilename(fn)
#     if sys.version_info < (3, 0) and sys.platform != 'win32':
#         encoding = get_filesystem_encoding()
#         # os.path.basename returns a bytes object, but NamedTemporaryFile
#         # will fail if the filename contains non ascii characters unless we
#         # use a unicode object
#         path_basename = lambda f: os.path.basename(fn).decode(encoding)
#         # the same for os.path.dirname
#         path_dirname = lambda f: os.path.dirname(fn).decode(encoding)
#     else:
#         path_basename = os.path.basename
#         path_dirname = os.path.dirname
# 
#     args = {
#         'suffix': '.tmp',
#         'prefix': path_basename(fn) + '.',
#         'dir': path_dirname(fn),
#         'delete': False,
#     }
# 
#     # In Python 2.x, json.dump expects a bytestream.
#     # In Python 3.x, it writes to a character stream
#     if sys.version_info < (3, 0):
#         args['mode'] = 'wb'
#     else:
#         args.update({
#             'mode': 'w',
#             'encoding': 'utf-8',
#         })
# 
#     tf = tempfile.NamedTemporaryFile(**compat_kwargs(args))
# 
#     try:
#         with tf:
#             json.dump(obj, tf)
#         if sys.platform == 'win32':
#             # Need to remove existing file on Windows, else os.rename raises
#             # WindowsError or FileExistsError.
#             try:
#                 os.unlink(fn)
#             except OSError:
#                 pass
#         try:
#             mask = os.umask(0)
#             os.umask(mask)
#             os.chmod(tf.name, 0o666 & ~mask)
#         except OSError:
#             pass
#         os.rename(tf.name, fn)
#     except Exception:
#         try:
#             os.remove(tf.name)
#         except OSError:
#             pass
#         raise


import errno
import io
import json
import os
import re
import shutil
import traceback
from .compat import compat_getenv
from .utils import (
    expand_path,
    write_json_file,
)
from __future__ import unicode_literals




class Cache(object):
    def __init__(self, ydl):
        self._ydl = ydl

    def _get_root_dir(self):
        res = self._ydl.params.get('cachedir')
        if res is None:
            cache_root = compat_getenv('XDG_CACHE_HOME', '~/.cache')
            res = os.path.join(cache_root, 'youtube-dlc')

# Path: build/plugin/src/resources/libraries/youtube_dl/compat.py
# def compat_getenv(key, default=None):
#     from .utils import get_filesystem_encoding
#     env = os.getenv(key, default)
#     if env:
#         env = env.decode(get_filesystem_encoding())
#     return env
# 
# Path: build/plugin/src/resources/libraries/youtube_dl/utils.py
# def expand_path(s):
#     """Expand shell variables and ~"""
#     return os.path.expandvars(compat_expanduser(s))
# 
# def write_json_file(obj, fn):
#     """ Encode obj as JSON and write it to fn, atomically if possible """
# 
#     fn = encodeFilename(fn)
#     if sys.version_info < (3, 0) and sys.platform != 'win32':
#         encoding = get_filesystem_encoding()
#         # os.path.basename returns a bytes object, but NamedTemporaryFile
#         # will fail if the filename contains non ascii characters unless we
#         # use a unicode object
#         path_basename = lambda f: os.path.basename(fn).decode(encoding)
#         # the same for os.path.dirname
#         path_dirname = lambda f: os.path.dirname(fn).decode(encoding)
#     else:
#         path_basename = os.path.basename
#         path_dirname = os.path.dirname
# 
#     args = {
#         'suffix': '.tmp',
#         'prefix': path_basename(fn) + '.',
#         'dir': path_dirname(fn),
#         'delete': False,
#     }
# 
#     # In Python 2.x, json.dump expects a bytestream.
#     # In Python 3.x, it writes to a character stream
#     if sys.version_info < (3, 0):
#         args['mode'] = 'wb'
#     else:
#         args.update({
#             'mode': 'w',
#             'encoding': 'utf-8',
#         })
# 
#     tf = tempfile.NamedTemporaryFile(**compat_kwargs(args))
# 
#     try:
#         with tf:
#             json.dump(obj, tf)
#         if sys.platform == 'win32':
#             # Need to remove existing file on Windows, else os.rename raises
#             # WindowsError or FileExistsError.
#             try:
#                 os.unlink(fn)
#             except OSError:
#                 pass
#         try:
#             mask = os.umask(0)
#             os.umask(mask)
#             os.chmod(tf.name, 0o666 & ~mask)
#         except OSError:
#             pass
#         os.rename(tf.name, fn)
#     except Exception:
#         try:
#             os.remove(tf.name)
#         except OSError:
#             pass
#         raise


# Path: build/plugin/src/resources/libraries/youtube_dl/cache.py
import errno
import io
import json
import os
import re
import shutil
import traceback
from .compat import compat_getenv
from .utils import (
    expand_path,
    write_json_file,
)

from __future__ import unicode_literals




class Cache(object):
    def __init__(self, ydl):
        self._ydl = ydl

    def _get_root_dir(self):
        res = self._ydl.params.get('cachedir')
        if res is None:
            cache_root = compat_getenv('XDG_CACHE_HOME', '~/.cache')
            res = os.path.join(cache_root, 'youtube-dlc')

        return expand_path(res)