Skip to content

Commit 0f99aaa

Browse files
authored
Import ensure_drafts_file from src/draft_uyils.py and use it
# Ensure DRAFTS_FILE path is resolved and the file exists, following XDG Base Directory Specification if necessary
1 parent 411d7e3 commit 0f99aaa

File tree

1 file changed

+3
-31
lines changed

1 file changed

+3
-31
lines changed

src/app.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,16 @@
1919
get_post_replies_count,
2020
)
2121
from .utils import convert_to_locale
22+
from .draft_utils import ensure_drafts_file
2223

2324
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
2425

2526
if not ACCESS_TOKEN:
2627
print("Error: The required ACCESS_TOKEN is not set. Please set it in your environment or in your .env file.")
2728
sys.exit(1)
2829

29-
# DRAFTS_FILE specifies the file name or path for saving draft data.
30-
# If DRAFTS_FILE does not exist and contains only a simple filename (without any path separator),
31-
# the application uses the XDG Base Directory Specification to determine the cache directory:
32-
# - It first checks for the XDG_CACHE_HOME environment variable.
33-
# - If not set, it defaults to HOME/.cache.
34-
# Then, a sub-folder named "threads-cli" is created within the cache directory,
35-
# and DRAFTS_FILE is placed inside that sub-folder.
36-
# If DRAFTS_FILE exists as a simple filename, it is used as provided.
37-
# If DRAFTS_FILE already contains a path separator (i.e., it's a complete path),
38-
# the application will use it exactly as given.
39-
40-
# Determine if DRAFTS_FILE not exist and contains a path separator.
41-
if not os.path.exists(DRAFTS_FILE) and os.path.sep not in DRAFTS_FILE:
42-
# Determine the cache directory
43-
# XDG_CACHE_HOME defaults to HOME/.cache if not set
44-
xdg_cache_home = os.getenv('XDG_CACHE_HOME')
45-
if not xdg_cache_home:
46-
home = os.getenv('HOME')
47-
if not home:
48-
raise EnvironmentError("HOME environment variable is not set.")
49-
xdg_cache_home = os.path.join(home, '.cache')
50-
# Define final path: XDG_CACHE_HOME/threads-cli/DRAFTS_FILE
51-
drafts_dir = os.path.join(xdg_cache_home, "threads-cli")
52-
os.makedirs(drafts_dir, exist_ok=True) # Create directory if it doesn't exist
53-
DRAFTS_FILE = os.path.join(drafts_dir, DRAFTS_FILE)
54-
# If it's not there, create an empty JSON file.
55-
if not os.path.exists(DRAFTS_FILE):
56-
with open(DRAFTS_FILE, "w") as f:
57-
# Create an empty list or dict, depending on your needs.
58-
# Here we write an empty dict.
59-
json.dump({}, f)
30+
# Ensure DRAFTS_FILE path is resolved and the file exists, following XDG Base Directory Specification if necessary.
31+
DRAFTS_FILE = ensure_drafts_file(DRAFTS_FILE)
6032

6133
HEADERS = {
6234
'Authorization': f'Bearer {ACCESS_TOKEN}'

0 commit comments

Comments
 (0)