Set directory cache

This commit is contained in:
Schimon Jehudah 2024-01-22 15:27:01 +00:00
parent 0d7f67d721
commit 13d87e2be7
2 changed files with 48 additions and 16 deletions

View file

@ -178,9 +178,9 @@ def get_default_data_directory():
if sys.platform == 'win32': if sys.platform == 'win32':
data_home = os.environ.get('APPDATA') data_home = os.environ.get('APPDATA')
if data_home is None: if data_home is None:
return os.path.abspath('.') return os.path.abspath('.slixfeed/data')
else: else:
return os.path.abspath('.') return os.path.abspath('.slixfeed/data')
else: else:
data_home = os.path.join( data_home = os.path.join(
os.environ.get('HOME'), '.local', 'share' os.environ.get('HOME'), '.local', 'share'
@ -188,6 +188,37 @@ def get_default_data_directory():
return os.path.join(data_home, 'slixfeed') 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(): def get_default_config_directory():
""" """
Determine the directory path where configuration will be stored. Determine the directory path where configuration will be stored.

View file

@ -24,6 +24,7 @@ import os
import slixfeed.action as action import slixfeed.action as action
from slixfeed.config import ( from slixfeed.config import (
add_to_list, add_to_list,
get_default_cache_directory,
get_default_data_directory, get_default_data_directory,
get_value_default, get_value_default,
get_value, get_value,
@ -426,13 +427,13 @@ async def message(self, message):
).format(ex) ).format(ex)
send_status_message( send_status_message(
self, jid, status_type, status_message) self, jid, status_type, status_message)
data_dir = get_default_data_directory() cache_dir = get_default_cache_directory()
if not os.path.isdir(data_dir): if not os.path.isdir(cache_dir):
os.mkdir(data_dir) os.mkdir(cache_dir)
if not os.path.isdir(data_dir + '/' + ex): if not os.path.isdir(cache_dir + '/' + ex):
os.mkdir(data_dir + '/' + ex) os.mkdir(cache_dir + '/' + ex)
filename = os.path.join( filename = os.path.join(
data_dir, ex, "slixfeed_" + timestamp() + "." + ex) cache_dir, ex, "slixfeed_" + timestamp() + "." + ex)
db_file = get_pathname_to_database(jid) db_file = get_pathname_to_database(jid)
results = await sqlite.get_feeds(db_file) results = await sqlite.get_feeds(db_file)
match ex: match ex:
@ -469,7 +470,7 @@ async def message(self, message):
ext = " ".join(message_text.split(" ")[1:]) ext = " ".join(message_text.split(" ")[1:])
ext = ext if ext else 'pdf' ext = ext if ext else 'pdf'
url = None url = None
status = None error = None
if ext in ("html", "md", "pdf"): if ext in ("html", "md", "pdf"):
status_type = "dnd" status_type = "dnd"
status_message = ( status_message = (
@ -478,12 +479,12 @@ async def message(self, message):
send_status_message( send_status_message(
self, jid, status_type, status_message) self, jid, status_type, status_message)
db_file = get_pathname_to_database(jid) db_file = get_pathname_to_database(jid)
data_dir = get_default_data_directory() cache_dir = get_default_cache_directory()
if ix_url: if ix_url:
if not os.path.isdir(data_dir): if not os.path.isdir(cache_dir):
os.mkdir(data_dir) os.mkdir(cache_dir)
if not os.path.isdir(data_dir + '/readability'): if not os.path.isdir(cache_dir + '/readability'):
os.mkdir(data_dir + '/readability') os.mkdir(cache_dir + '/readability')
try: try:
ix = int(ix_url) ix = int(ix_url)
try: try:
@ -506,11 +507,11 @@ async def message(self, message):
for i in ("?", "'", "!"): for i in ("?", "'", "!"):
title = title.replace(i, "") title = title.replace(i, "")
filename = os.path.join( filename = os.path.join(
data_dir, "readability", cache_dir, "readability",
title + "_" + timestamp() + "." + ext) title + "_" + timestamp() + "." + ext)
error = action.generate_document( error = action.generate_document(
data, url, ext, filename) data, url, ext, filename)
if error or status: if error:
response = ( response = (
"Failed to export {}. Reason: {}" "Failed to export {}. Reason: {}"
).format(ext.upper(), error) ).format(ext.upper(), error)