From 13d87e2be7e6f1b46ec524d44822012c77ccaa9f Mon Sep 17 00:00:00 2001 From: Schimon Jehudah Date: Mon, 22 Jan 2024 15:27:01 +0000 Subject: [PATCH] Set directory cache --- slixfeed/config.py | 35 +++++++++++++++++++++++++++++++++-- slixfeed/xmpp/process.py | 29 +++++++++++++++-------------- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/slixfeed/config.py b/slixfeed/config.py index 2eb2fac..3995956 100644 --- a/slixfeed/config.py +++ b/slixfeed/config.py @@ -178,9 +178,9 @@ def get_default_data_directory(): if sys.platform == 'win32': data_home = os.environ.get('APPDATA') if data_home is None: - return os.path.abspath('.') + return os.path.abspath('.slixfeed/data') else: - return os.path.abspath('.') + return os.path.abspath('.slixfeed/data') else: data_home = os.path.join( os.environ.get('HOME'), '.local', 'share' @@ -188,6 +188,37 @@ def get_default_data_directory(): return os.path.join(data_home, 'slixfeed') +def get_default_cache_directory(): + """ + Determine the directory path where dbfile will be stored. + + * If $XDG_DATA_HOME is defined, use it; + * else if $HOME exists, use it; + * else if the platform is Windows, use %APPDATA%; + * else use the current directory. + + Returns + ------- + str + Path to cache directory. + """ +# data_home = xdg.BaseDirectory.xdg_data_home + data_home = os.environ.get('XDG_CACHE_HOME') + if data_home is None: + if os.environ.get('HOME') is None: + if sys.platform == 'win32': + data_home = os.environ.get('APPDATA') + if data_home is None: + return os.path.abspath('.slixfeed/cache') + else: + return os.path.abspath('.slixfeed/cache') + else: + data_home = os.path.join( + os.environ.get('HOME'), '.cache' + ) + return os.path.join(data_home, 'slixfeed') + + def get_default_config_directory(): """ Determine the directory path where configuration will be stored. diff --git a/slixfeed/xmpp/process.py b/slixfeed/xmpp/process.py index 5653223..5825587 100644 --- a/slixfeed/xmpp/process.py +++ b/slixfeed/xmpp/process.py @@ -24,6 +24,7 @@ import os import slixfeed.action as action from slixfeed.config import ( add_to_list, + get_default_cache_directory, get_default_data_directory, get_value_default, get_value, @@ -426,13 +427,13 @@ async def message(self, message): ).format(ex) send_status_message( self, jid, status_type, status_message) - data_dir = get_default_data_directory() - if not os.path.isdir(data_dir): - os.mkdir(data_dir) - if not os.path.isdir(data_dir + '/' + ex): - os.mkdir(data_dir + '/' + ex) + cache_dir = get_default_cache_directory() + if not os.path.isdir(cache_dir): + os.mkdir(cache_dir) + if not os.path.isdir(cache_dir + '/' + ex): + os.mkdir(cache_dir + '/' + ex) filename = os.path.join( - data_dir, ex, "slixfeed_" + timestamp() + "." + ex) + cache_dir, ex, "slixfeed_" + timestamp() + "." + ex) db_file = get_pathname_to_database(jid) results = await sqlite.get_feeds(db_file) match ex: @@ -469,7 +470,7 @@ async def message(self, message): ext = " ".join(message_text.split(" ")[1:]) ext = ext if ext else 'pdf' url = None - status = None + error = None if ext in ("html", "md", "pdf"): status_type = "dnd" status_message = ( @@ -478,12 +479,12 @@ async def message(self, message): send_status_message( self, jid, status_type, status_message) db_file = get_pathname_to_database(jid) - data_dir = get_default_data_directory() + cache_dir = get_default_cache_directory() if ix_url: - if not os.path.isdir(data_dir): - os.mkdir(data_dir) - if not os.path.isdir(data_dir + '/readability'): - os.mkdir(data_dir + '/readability') + if not os.path.isdir(cache_dir): + os.mkdir(cache_dir) + if not os.path.isdir(cache_dir + '/readability'): + os.mkdir(cache_dir + '/readability') try: ix = int(ix_url) try: @@ -506,11 +507,11 @@ async def message(self, message): for i in ("?", "'", "!"): title = title.replace(i, "") filename = os.path.join( - data_dir, "readability", + cache_dir, "readability", title + "_" + timestamp() + "." + ext) error = action.generate_document( data, url, ext, filename) - if error or status: + if error: response = ( "Failed to export {}. Reason: {}" ).format(ext.upper(), error)