Python appdirs.user_cache_dir() Examples

The following are 30 code examples of appdirs.user_cache_dir(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module appdirs , or try the search function .
Example #1
Source File: cache.py    From stdpopsim with GNU General Public License v3.0 6 votes vote down vote up
def set_cache_dir(cache_dir=None):
    """
    The cache_dir is the directory in which stdpopsim stores and checks for
    downloaded data. If the specified cache_dir is not None, this value is
    converted to a pathlib.Path instance, which is used as the cache directory.
    If cache_dir is None (the default), the cache directory is set either from
    the environment variable `STDPOPSIM_CACHE` if it exists, or set to the
    default location using the :mod:`appdirs` module.

    No checks for existance, writability, etc. are performed by this function.
    """
    if cache_dir is None:
        cache_dir = os.environ.get("STDPOPSIM_CACHE", None)
    if cache_dir is None:
        cache_dir = appdirs.user_cache_dir("stdpopsim", "popgensims")
    global _cache_dir
    _cache_dir = pathlib.Path(cache_dir)
    logger.info(f"Set cache_dir to {_cache_dir}") 
Example #2
Source File: __init__.py    From GoogleSpeech with GNU Lesser General Public License v2.1 6 votes vote down vote up
def __init__(self, text, lang, segment_num, segment_count=None):
    self.text = text
    self.lang = lang
    self.segment_num = segment_num
    self.segment_count = segment_count
    self.preload_mutex = threading.Lock()
    if not hasattr(__class__, "cache"):
      db_filepath = os.path.join(appdirs.user_cache_dir(appname="google_speech",
                                                        appauthor=False),
                                 "google_speech-cache.sqlite")
      os.makedirs(os.path.dirname(db_filepath), exist_ok=True)
      cache_name = "sound_data"
      __class__.cache = web_cache.ThreadedWebCache(db_filepath,
                                                   cache_name,
                                                   expiration=60 * 60 * 24 * 365,  # 1 year
                                                   caching_strategy=web_cache.CachingStrategy.LRU)
      logging.getLogger().debug("Total size of file '%s': %s" % (db_filepath,
                                                                 __class__.cache.getDatabaseFileSize()))
      purged_count = __class__.cache.purge()
      logging.getLogger().debug("%u obsolete entries have been removed from cache '%s'" % (purged_count, cache_name))
      row_count = len(__class__.cache)
      logging.getLogger().debug("Cache '%s' contains %u entries" % (cache_name, row_count)) 
Example #3
Source File: 01_basic_test.py    From genomepy with MIT License 6 votes vote down vote up
def test_cache(capsys):
    my_cache_dir = os.path.join(user_cache_dir("genomepy"), __version__)
    if os.path.exists(my_cache_dir):
        rmtree(my_cache_dir)
    os.makedirs(my_cache_dir)
    cached = Bucket(my_cache_dir, days=7)

    @cached
    def expensive_method():
        print("Method called.")

    @expensive_method.callback
    def expensive_method(callinfo):
        print("Cache used.")

    expensive_method()
    expensive_method()

    captured = capsys.readouterr().out.strip().split("\n")
    assert captured == ["Method called.", "Cache used."] 
Example #4
Source File: util.py    From ngraph-python with Apache License 2.0 6 votes vote down vote up
def get_cache_dir(subdir=None):
    """
    Function for getting cache directory to store reused files like kernels, or scratch space
    for autotuning, etc.
    """
    cache_dir = os.environ.get("NEON_CACHE_DIR")

    if cache_dir is None:
        cache_dir = appdirs.user_cache_dir("neon", "neon")

    if subdir:
        subdir = subdir if isinstance(subdir, list) else [subdir]
        cache_dir = os.path.join(cache_dir, *subdir)

    if not os.path.exists(cache_dir):
        os.makedirs(cache_dir)

    return cache_dir 
Example #5
Source File: settings.py    From clay with GNU General Public License v3.0 6 votes vote down vote up
def _ensure_directories(self):
        """
        Create config dir, config file & cache dir if they do not exist yet.
        """
        self._config_dir = appdirs.user_config_dir('clay', 'Clay')
        self._config_file_path = os.path.join(self._config_dir, 'config.yaml')
        self._colours_file_path = os.path.join(self._config_dir, 'colours.yaml')

        try:
            os.makedirs(self._config_dir)
        except OSError as error:
            if error.errno != errno.EEXIST:
                raise

        self._cache_dir = appdirs.user_cache_dir('clay', 'Clay')
        try:
            os.makedirs(self._cache_dir)
        except OSError as error:
            if error.errno != errno.EEXIST:
                raise

        if not os.path.exists(self._config_file_path):
            with open(self._config_file_path, 'w') as settings_file:
                settings_file.write('{}') 
Example #6
Source File: persist.py    From neon with Apache License 2.0 6 votes vote down vote up
def get_cache_dir(subdir=None):
    """
    Function for getting cache directory to store reused files like kernels, or scratch space
    for autotuning, etc.
    """
    cache_dir = os.environ.get("NEON_CACHE_DIR")

    if cache_dir is None:
        cache_dir = appdirs.user_cache_dir("neon", "neon")

    if subdir:
        subdir = subdir if isinstance(subdir, list) else [subdir]
        cache_dir = os.path.join(cache_dir, *subdir)

    if not os.path.exists(cache_dir):
        os.makedirs(cache_dir)

    return cache_dir 
Example #7
Source File: db.py    From fac with MIT License 6 votes vote down vote up
def __init__(self, config, api):
        self.config = config
        self.api = api
        self.cache_dir = user_cache_dir('fac', appauthor=False)
        self.storage = FileStorage(os.path.join(self.cache_dir, 'index'))

        self.schema = Schema(
            name=TEXT(sortable=True, phrase=True, field_boost=3,
                      analyzer=intraword),
            owner=TEXT(sortable=True, field_boost=2.5,
                       analyzer=intraword),
            title=TEXT(field_boost=2.0, phrase=False),
            summary=TEXT(phrase=True),
            downloads=NUMERIC(sortable=True),
            sort_name=SortColumn(),
            name_id=ID(stored=True),
        )

        try:
            self.index = self.storage.open_index()
        except EmptyIndexError:
            self.index = None

        self.db = JSONFile(os.path.join(self.cache_dir, 'mods.json')) 
Example #8
Source File: file.py    From dart-fss with MIT License 5 votes vote down vote up
def get_cache_folder():
    """ Create cache folder

    Returns
    -------
    str
        Cache Folder Path
    """
    from appdirs import user_cache_dir
    appname = 'dart-fss'
    cache_dir = user_cache_dir(appname)
    create_folder(cache_dir)
    return cache_dir 
Example #9
Source File: fetcher.py    From akshare with MIT License 5 votes vote down vote up
def __init__(self):
        self.path = Path(
            appdirs.user_cache_dir(appname="akshare/wdbank", version=wdbank.__version__)
        )
        self.path.parent.mkdir(parents=True, exist_ok=True)
        try:
            with self.path.open("rb") as cachefile:
                self.cache = {
                    i: (date, json)
                    for i, (date, json) in pickle.load(cachefile).items()
                    if (TODAY - datetime.date.fromordinal(date)).days < EXP
                }
        except (IOError, EOFError):
            self.cache = {} 
Example #10
Source File: __init__.py    From PhonePi_SampleServer with MIT License 5 votes vote down vote up
def get_default_cache():
    """
    Return the ``PYTHON_EGG_CACHE`` environment variable
    or a platform-relevant user cache dir for an app
    named "Python-Eggs".
    """
    return (
        os.environ.get('PYTHON_EGG_CACHE')
        or appdirs.user_cache_dir(appname='Python-Eggs')
    ) 
Example #11
Source File: sr.py    From sarracenia with GNU General Public License v2.0 5 votes vote down vote up
def appname(self,n):
        self.__appname = n
        self.user_config_dir = appdirs.user_config_dir(self.appname, self.appauthor)
        self.user_cache_dir = appdirs.user_cache_dir(self.appname, self.appauthor) 
Example #12
Source File: test_cache.py    From stdpopsim with GNU General Public License v3.0 5 votes vote down vote up
def test_none(self):
        stdpopsim.set_cache_dir(None)
        cache_dir = pathlib.Path(appdirs.user_cache_dir("stdpopsim", "popgensims"))
        self.assertEqual(stdpopsim.get_cache_dir(), cache_dir) 
Example #13
Source File: sr.py    From sarracenia with GNU General Public License v2.0 5 votes vote down vote up
def _clean_missing_proc_state(self):
        """ remove state pid files for process which are not running
        """

        if not os.path.isdir(self.user_cache_dir):
           return
        os.chdir(self.user_cache_dir)
        for instance in self.missing:
            (c, cfg, i) = instance
            if os.path.isdir(c):
                os.chdir(c)
                for cfg in os.listdir():
                    if os.path.isdir(cfg):
                        os.chdir(cfg)
                        for filename in os.listdir():
                            if filename[-4:] == '.pid':
                                p = pathlib.Path(filename)
                                if sys.version_info[0] > 3 or sys.version_info[1] > 4:
                                    t = p.read_text().strip()
                                else:
                                    with p.open() as f:
                                        t = f.read().strip()
                                if t.isdigit():
                                    pid = int(t)
                                    if pid not in self.procs:
                                        os.unlink(filename)
                                else:
                                    os.unlink(filename)

                        os.chdir('..')
                os.chdir('..') 
Example #14
Source File: sr.py    From sarracenia with GNU General Public License v2.0 5 votes vote down vote up
def _find_missing_instances(self):
        """ find processes which are no longer running, based on pidfiles in state, and procs.
        """
        missing = []
        self.missing = []
        if not os.path.isdir(self.user_cache_dir):
           return
        os.chdir(self.user_cache_dir)
        for c in self.components:
            if os.path.isdir(c):
                os.chdir(c)
                for cfg in os.listdir():
                    if os.path.isdir(cfg):
                        os.chdir(cfg)
                        for filename in os.listdir():
                            if filename[-4:] == '.pid':
                                i = int(filename[-6:-4])
                                p = pathlib.Path(filename)
                                if sys.version_info[0] > 3 or sys.version_info[1] > 4:
                                    t = p.read_text().strip()
                                else:
                                    with p.open() as f:
                                        t = f.read().strip()
                                if t.isdigit():
                                    pid = int(t)
                                    if pid not in self.procs:
                                        missing.append([c, cfg, i])
                                else:
                                    missing.append([c, cfg, i])

                        os.chdir('..')
                os.chdir('..')

        self.missing = missing 
Example #15
Source File: sr.py    From sarracenia with GNU General Public License v2.0 5 votes vote down vote up
def save_states(self,savename):
        """ DEVELOPER ONLY.. copy state files to an alternate tree.
        """

        self.states = {}
        if not os.path.isdir(self.user_cache_dir):
           return
        os.chdir(self.user_cache_dir)
        other_cache_dir = appdirs.user_cache_dir(savename, self.appauthor)
        if not os.path.exists(other_cache_dir):
            os.mkdir(other_cache_dir)
        for c in self.components:
            if os.path.isdir(c):
                os.chdir(c)
                other_c_dir= other_cache_dir + os.sep + c
                if not os.path.exists(other_c_dir):
                    os.mkdir(other_c_dir)
                self.states[c] = {}
                for cfg in os.listdir():
                    os.chdir(cfg)
                    other_cfg_dir= other_c_dir + os.sep + cfg
                    if not os.path.exists(other_cfg_dir):
                        os.mkdir(other_cfg_dir)
                    for f in os.listdir():
                        to=other_cfg_dir + os.sep + f
                        print( 'save_states copying: %s %s' % ( f , to ) )
                        shutil.copyfile( f, to )                
                    os.chdir('..')
                os.chdir('..')
        os.chdir('..') 
Example #16
Source File: sr.py    From sarracenia with GNU General Public License v2.0 5 votes vote down vote up
def _launch_instance(self, component_path, c, cfg: str, i):
        """
          start up a instance process (always daemonish/background fire & forget type process.)
        """
        if cfg is None:
            lfn = self.user_cache_dir + os.sep + 'log' + os.sep + 'sr_' + c + "_%02d" % i + '.log'
        else:
            lfn = self.user_cache_dir + os.sep + 'log' + os.sep + 'sr_' + c + '_' + cfg + "_%02d" % i + '.log'

        os.makedirs(os.path.dirname(lfn), exist_ok=True)

        if c[0] != 'c':  # python components
            if cfg is None:
                cmd = [sys.executable, component_path, '--no', "%d" % i, 'start']
            else:
                cmd = [sys.executable, component_path, '--no', "%d" % i, 'start', cfg]
        else:  # C components
            cmd = [component_path, 'start', cfg]

        #print( "launching +%s+  re-directed to: %s" % ( cmd, lfn ), flush=True )

        try:
            with open(lfn, "a") as lf:
                subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=lf, stderr=subprocess.STDOUT)
        except Exception as ex:
            print( "failed to launch: %s >%s >2&1 (reason: %s) " % ( ' '.join(cmd), lfn, ex ) ) 
Example #17
Source File: cachefile.py    From URLExtract with MIT License 5 votes vote down vote up
def _get_writable_cache_dir(self):
        """
        Get writable cache directory with fallback to user's cache directory
        and global temp directory

        :raises: CacheFileError when cached directory is not writable for user
        :return: path to cache directory
        :rtype: str
        """
        dir_path_data = self._get_default_cache_dir()

        if os.access(dir_path_data, os.W_OK):
            self._default_cache_file = True
            return dir_path_data

        dir_path_user = user_cache_dir(self._URLEXTRACT_NAME)
        if not os.path.exists(dir_path_user):
            try:
                os.makedirs(dir_path_user, exist_ok=True)
            except PermissionError:
                # if PermissionError exception is raised we should continue
                # and try to set the last fallback dir
                pass

        if os.access(dir_path_user, os.W_OK):
            return dir_path_user

        dir_path_temp = tempfile.gettempdir()
        if os.access(dir_path_temp, os.W_OK):
            return dir_path_temp

        raise CacheFileError("Cache directories are not writable.") 
Example #18
Source File: sr_config.py    From sarracenia with GNU General Public License v2.0 5 votes vote down vote up
def configure_statehost(self):
        self.logger.debug("configure_statehost")

        hostdir = None

        # user asked for statehost

        if self.statehost :
           hostdir = self.hostname
           if self.hostform == 'short' : hostdir = self.hostname.split('.')[0] 

        # finalize user_log_dir

        if hostdir and not hostdir in self.user_log_dir :
           self.user_log_dir = self.user_log_dir[:-4] + os.sep + hostdir + '/log'

        # create user_log_dir 

        self.logger.debug("sr_config user_log_dir  %s " % self.user_log_dir ) 
        try: 
            os.makedirs(self.user_log_dir, 0o775,True)
        except Exception as ex:
            self.logger.warning( "making %s: %s" % ( self.user_log_dir, ex ) )

        # finalize user_cache_dir

        if hostdir and not hostdir in self.user_cache_dir :
           self.user_cache_dir  = appdirs.user_cache_dir (self.appname,self.appauthor)
           self.user_cache_dir += os.sep + hostdir
           self.user_cache_dir += os.sep + self.program_name.replace('sr_','')
           self.user_cache_dir += os.sep + "%s" % self.config_name

        # create user_cache_dir

        if not self.program_name in [ 'sr', 'sr_config' ]:
           self.logger.debug("sr_config user_cache_dir  %s " % self.user_cache_dir ) 
           try: 
                os.makedirs(self.user_cache_dir,  0o775,True)
           except Exception as ex:
                self.logger.warning( "making %s: %s" % ( self.user_cache_dir, ex ) ) 
Example #19
Source File: dirs.py    From aw-core with Mozilla Public License 2.0 5 votes vote down vote up
def get_cache_dir(module_name: Optional[str]) -> str:
    cache_dir = appdirs.user_cache_dir("activitywatch")
    return os.path.join(cache_dir, module_name) if module_name else cache_dir 
Example #20
Source File: http_helpers.py    From sacad with Mozilla Public License 2.0 5 votes vote down vote up
def __init__(self, *, allow_session_cookies=False, min_delay_between_accesses=0, jitter_range_ms=None,
               rate_limited_domains=None, logger=logging.getLogger()):
    self.allow_session_cookies = allow_session_cookies
    self.session = None
    self.watcher_db_filepath = os.path.join(appdirs.user_cache_dir(appname="sacad",
                                                                   appauthor=False),
                                            "rate_watcher.sqlite")
    self.min_delay_between_accesses = min_delay_between_accesses
    self.jitter_range_ms = jitter_range_ms
    self.rate_limited_domains = rate_limited_domains
    self.logger = logger 
Example #21
Source File: __main__.py    From himawaripy with MIT License 5 votes vote down vote up
def parse_args():
    parser = argparse.ArgumentParser(description="set (near-realtime) picture of Earth as your desktop background",
                                     epilog="http://labs.boramalper.org/himawaripy")

    parser.add_argument("--version", action="version", version="%(prog)s {}.{}.{}".format(*HIMAWARIPY_VERSION))

    group = parser.add_mutually_exclusive_group()

    group.add_argument("--auto-offset", action="store_true", dest="auto_offset", default=False,
                       help="determine offset automatically")
    group.add_argument("-o", "--offset", type=int, dest="offset", default=10,
                       help="UTC time offset in hours, must be less than or equal to +10")

    parser.add_argument("-l", "--level", type=int, choices=[4, 8, 16, 20], dest="level", default=4,
                        help="increases the quality (and the size) of each tile. possible values are 4, 8, 16, 20")
    parser.add_argument("-d", "--deadline", type=int, dest="deadline", default=6,
                        help="deadline in minutes to download all the tiles, set 0 to cancel")
    parser.add_argument("--save-battery", action="store_true", dest="save_battery", default=False,
                        help="stop refreshing on battery")
    parser.add_argument("--output-dir", type=str, dest="output_dir",
                        help="directory to save the temporary background image",
                        default=appdirs.user_cache_dir(appname="himawaripy", appauthor=False))
    parser.add_argument("--dont-change", action="store_true", dest="dont_change", default=False,
                        help="don't change the wallpaper (just download it)")

    args = parser.parse_args()

    if not -12 <= args.offset <= 10:
        sys.exit("OFFSET has to be between -12 and +10!\n")

    if not args.deadline >= 0:
        sys.exit("DEADLINE has to be greater than (or equal to if you want to disable) zero!\n")

    return args 
Example #22
Source File: __init__.py    From lambda-packs with MIT License 5 votes vote down vote up
def get_default_cache():
    """
    Return the ``PYTHON_EGG_CACHE`` environment variable
    or a platform-relevant user cache dir for an app
    named "Python-Eggs".
    """
    return (
        os.environ.get('PYTHON_EGG_CACHE')
        or appdirs.user_cache_dir(appname='Python-Eggs')
    ) 
Example #23
Source File: c.py    From kotori with GNU Affero General Public License v3.0 5 votes vote down vote up
def from_header(cls, include_path=None, header_files=None):
        cache_dir = user_cache_dir('lst', 'kotori')
        if not os.path.isdir(cache_dir): os.makedirs(cache_dir)
        library = LibraryAdapter(
            header_files, cls.compile(include_path, header_files),
            include_path=include_path, library_path=include_path,
            cache_path=cache_dir)
        return library 
Example #24
Source File: util.py    From kotori with GNU Affero General Public License v3.0 5 votes vote down vote up
def setup_h2m_structs_pyclibrary():
    cache_dir = os.path.join(user_cache_dir('kotori'), 'lst')
    if not os.path.isdir(cache_dir): os.makedirs(cache_dir)
    lib_dir = os.path.join(os.path.dirname(__file__), 'cpp')
    library = LibraryAdapter(u'h2m_structs.h', u'h2m_structs.so', include_path=lib_dir, library_path=lib_dir, cache_path=cache_dir)
    struct_registry = StructRegistryByID(library)
    return struct_registry 
Example #25
Source File: util.py    From kotori with GNU Affero General Public License v3.0 5 votes vote down vote up
def setup_h2m_structs_cffi():
    cache_dir = os.path.join(user_cache_dir('kotori'), 'lst')
    if not os.path.isdir(cache_dir): os.makedirs(cache_dir)
    lib_dir = os.path.join(os.path.dirname(__file__), 'cpp')
    library = LibraryAdapterCFFI(u'h2m_structs.h', u'h2m_structs.so', include_path=lib_dir, library_path=lib_dir, cache_path=cache_dir)
    struct_registry = StructRegistryByID(library)
    return struct_registry 
Example #26
Source File: cache.py    From acsoo with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, cachename):
        self.cachedir = os.path.join(appdirs.user_cache_dir(), cachename)
        if not os.path.exists(self.cachedir):
            os.makedirs(self.cachedir)
            with open(os.path.join(self.cachedir, "CACHEDIR.TAG"), "w") as f:
                f.write(
                    "Signature: 8a477f597d28d172789f06886806bc55\n"
                    "# This file is a cache directory tag created by 'acsoo'.\n"
                    "# For information about cache directory tags, see:\n"
                    "#	http://www.brynosaurus.com/cachedir/\n"
                ) 
Example #27
Source File: test_wheel.py    From acsoo with GNU General Public License v3.0 5 votes vote down vote up
def test_wheel(mocker, tmp_path):
    # mock appdirs.user_cache_dir, so the cache
    # goes to a known place
    cachedir = tmp_path / "cache"
    mocker.patch.object(appdirs, "user_cache_dir", lambda: str(cachedir))
    runner = CliRunner()
    with runner.isolated_filesystem():
        with open("requirements.txt", "w") as f:
            f.write(
                "requests\n"
                "-e git+https://github.com/acsone/acsoo"
                "@178a896c360ed6059bb76da35af4e99815a8b8de"
                "#egg=acsoo\n"
            )
        res = runner.invoke(wheel, ["--no-deps", "--exclude-project"])
        assert res.exit_code == 0
        # acsoo wheel must be in cache
        cache_content = list(cachedir.glob("**/acsoo*.whl"))
        assert len(cache_content) == 1
        # two wheels must have been generated
        files = sorted(os.listdir("release"))
        assert len(files) == 2
        assert files[0].startswith("acsoo-2.0.2")
        assert files[1].startswith("requests")

        # run it again
        shutil.rmtree("release")
        res = runner.invoke(wheel, ["--no-deps", "--exclude-project"])
        assert res.exit_code == 0
        assert "Obtained -e git+https://github.com/acsone/acsoo" in res.output
        files = sorted(os.listdir("release"))
        assert len(files) == 2
        assert files[0].startswith("acsoo-2.0.2")
        assert files[1].startswith("requests") 
Example #28
Source File: __init__.py    From pkg_resources with MIT License 5 votes vote down vote up
def get_default_cache():
    """
    Return the ``PYTHON_EGG_CACHE`` environment variable
    or a platform-relevant user cache dir for an app
    named "Python-Eggs".
    """
    return (
        os.environ.get('PYTHON_EGG_CACHE')
        or appdirs.user_cache_dir(appname='Python-Eggs')
    ) 
Example #29
Source File: map.py    From pygbif with MIT License 5 votes vote down vote up
def __make_path(self):
        uu = hashlib.sha256(self.response.content).hexdigest()
        base_path = user_cache_dir("python/pygbif")
        if not os.path.exists(base_path):
            os.makedirs(base_path)
        file_ext = (
            ".png" if has(self.response.headers["Content-Type"], "png") else ".mvt"
        )
        path = base_path + "/" + uu + file_ext
        return path 
Example #30
Source File: __init__.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def get_default_cache():
    """
    Return the ``PYTHON_EGG_CACHE`` environment variable
    or a platform-relevant user cache dir for an app
    named "Python-Eggs".
    """
    return (
        os.environ.get('PYTHON_EGG_CACHE')
        or appdirs.user_cache_dir(appname='Python-Eggs')
    )