Skip to content

Commit 9d2cb3b

Browse files
committed
Path permission fix
1 parent e79e619 commit 9d2cb3b

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

docs/source/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ v2.7
4545
- Invite link tracking
4646
- :class:`~daf.guild.GUILD`: ``invite_track`` parameter for tracking invite links
4747

48+
- File outputs:
49+
50+
- Changed all paths' defaults to be stored under /<user-home-dir>/daf/ folder to prevent permission problems
51+
4852
- :class:`~daf.guild.AutoGUILD` ``interval`` default changed to ``timedelta(minutes=1)``
4953
- xMESSAGE ``start_in`` now accepts :class:`datetime.datetime` - send at specific datetime.
5054
- GUI:

src/daf/core.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
CORE_TASK_SLEEP_SEC = 0.1
3838
ACCOUNT_CLEANUP_DELAY = 10
3939
SCHEMA_BACKUP_DELAY = 120
40-
SHILL_LIST_BACKUP_PATH = Path("objects.sbf") # sbf -> Schema Backup File
40+
DAF_PATH = Path.home().joinpath("daf")
41+
SHILL_LIST_BACKUP_PATH = DAF_PATH.joinpath("objects.sbf") # sbf -> Schema Backup File
4142
# ---------------------------------------
4243

4344

@@ -79,6 +80,7 @@ async def schema_backup_task():
7980
loop.call_later(SCHEMA_BACKUP_DELAY, event.set)
8081
await event.wait()
8182
event.clear()
83+
DAF_PATH.mkdir(parents=True, exist_ok=True)
8284
tmp_path = str(SHILL_LIST_BACKUP_PATH) + ".1"
8385
trace("Saving objects to file.", TraceLEVELS.DEBUG)
8486
try:
@@ -118,7 +120,7 @@ async def schema_load_from_file() -> None:
118120
async def initialize(user_callback: Optional[Union[Callable, Coroutine]] = None,
119121
debug: Optional[Union[TraceLEVELS, int, str]] = TraceLEVELS.NORMAL,
120122
logger: Optional[logging.LoggerBASE] = None,
121-
accounts: List[client.ACCOUNT] = [],
123+
accounts: List[client.ACCOUNT] = None,
122124
save_to_file: bool = False) -> None:
123125
"""
124126
The main initialization function.
@@ -132,6 +134,8 @@ async def initialize(user_callback: Optional[Union[Callable, Coroutine]] = None,
132134
Parameters are the same as in :func:`daf.core.run`.
133135
"""
134136
loop = asyncio.get_event_loop()
137+
if accounts is None:
138+
accounts = []
135139
# ------------------------------------------------------------
136140
# Initialize tracing
137141
# ------------------------------------------------------------
@@ -143,7 +147,7 @@ async def initialize(user_callback: Optional[Union[Callable, Coroutine]] = None,
143147
# Initialize logging
144148
# ------------------------------------------------------------
145149
if logger is None:
146-
logger = logging.LoggerJSON(path="History")
150+
logger = logging.LoggerJSON(path=str(Path.home().joinpath("daf/History")))
147151

148152
await logging.initialize(logger)
149153
# ------------------------------------------------------------
@@ -401,7 +405,7 @@ def _shutdown_clean(loop: asyncio.AbstractEventLoop) -> None:
401405
def run(user_callback: Optional[Union[Callable, Coroutine]] = None,
402406
debug: Optional[Union[TraceLEVELS, int, str, bool]] = TraceLEVELS.NORMAL,
403407
logger: Optional[logging.LoggerBASE] = None,
404-
accounts: Optional[List[client.ACCOUNT]] = [],
408+
accounts: Optional[List[client.ACCOUNT]] = None,
405409
save_to_file: bool = False) -> None:
406410
"""
407411
.. versionchanged:: 2.7
@@ -428,7 +432,7 @@ def run(user_callback: Optional[Union[Callable, Coroutine]] = None,
428432
The higher value this option is, the more will be displayed.
429433
logger: Optional[loggers.LoggerBASE]
430434
The logging class to use.
431-
If this is not provided, JSON is automatically used.
435+
If this is not provided, JSON is automatically used with the ``path`` parameter set to /<user-home-dir>/daf/History
432436
accounts: Optional[List[client.ACCOUNT]]
433437
List of :class:`~daf.client.ACCOUNT` (Discord accounts) to use.
434438
.. versionadded:: v2.4

src/daf/logging/_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ async def _save_log(
292292
.joinpath("{:02d}".format(timestruct.month))
293293
.joinpath("{:02d}".format(timestruct.day)))
294294

295-
logging_output.mkdir(parents=True,exist_ok=True)
295+
logging_output.mkdir(parents=True, exist_ok=True)
296296
logging_output = logging_output.joinpath("".join(char if char not in C_FILE_NAME_FORBIDDEN_CHAR
297297
else "#" for char in guild_context["name"]) + ".json")
298298
# Create file if it doesn't exist

src/daf/logging/sql/mgr.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import datetime, date
88
from typing import Callable, Dict, List, Literal, Any, Union, Optional, Tuple
99
from contextlib import suppress
10+
from pathlib import Path
1011
from typeguard import typechecked
1112

1213
from ..tracing import TraceLEVELS, trace
@@ -209,7 +210,9 @@ def exists(self, key: Any) -> bool:
209210
class LoggerSQL(logging.LoggerBASE):
210211
"""
211212
.. versionchanged:: v2.7
212-
Invite link tracking.
213+
214+
- Invite link tracking.
215+
- Default database file output set to /<user-home-dir>/daf/messages
213216
214217
Used for controlling the SQL database used for message logs.
215218
@@ -270,11 +273,12 @@ def __init__(self,
270273
self.password = password
271274
self.server = server
272275
self.port = port
273-
self.database = database if database is not None else "messages"
276+
self.database = database if database is not None else str(Path.home().joinpath("daf/messages"))
274277
self.dialect = dialect
275278

276279
if self.dialect == "sqlite":
277280
self.database += ".db"
281+
Path(self.database).parent.mkdir(parents=True, exist_ok=True)
278282

279283
# Set in ._begin_engine
280284
self.engine: sqa.engine.Engine = None
@@ -706,6 +710,9 @@ async def _handle_error(self,
706710
res = False
707711
await self._reconnect_after(SQL_RECONNECT_TIME)
708712
else:
713+
if self.dialect == "sqlite":
714+
Path(self.database).parent.mkdir(parents=True, exist_ok=True)
715+
709716
await self._create_tables()
710717
self._clear_caches()
711718
await self._generate_lookup_values()

src/daf/web.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class GLOBALS:
6565
WD_TIMEOUT_LONG = 90
6666
WD_RD_CLICK_UPPER_N = 5
6767
WD_RD_CLICK_LOWER_N = 2
68-
WD_OUTPUT_PATH = pathlib.Path("./daf_web_data")
68+
WD_OUTPUT_PATH = pathlib.Path.home().joinpath("daf/daf_web_data")
6969
WD_TOKEN_PATH = WD_OUTPUT_PATH.joinpath("tokens.json")
7070
WD_PROFILES_PATH = WD_OUTPUT_PATH.joinpath("chrome_profiles")
7171
WD_QUERY_SLEEP_S = 5
@@ -436,7 +436,7 @@ async def initialize(self) -> None:
436436
Any
437437
Raised in :py:meth:`~SeleniumCLIENT.login` method.
438438
"""
439-
WD_OUTPUT_PATH.mkdir(exist_ok=True)
439+
WD_OUTPUT_PATH.mkdir(exist_ok=True, parents=True)
440440
web_data_path = pathlib.Path(WD_PROFILES_PATH, self._username)
441441

442442
opts = Options()

src/daf_gui/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66

77
from importlib.util import find_spec
8+
from pathlib import Path
89

910
installed = find_spec("ttkbootstrap") is not None
1011

@@ -258,9 +259,9 @@ def import_accounts():
258259
self.combo_tracing.pack(fill=tk.X, side="left", expand=True)
259260

260261
self.combo_logging_mgr["values"] = [
261-
ObjectInfo(daf.LoggerJSON, {"path": "History"}),
262-
ObjectInfo(daf.LoggerSQL, {}),
263-
ObjectInfo(daf.LoggerCSV, {"path": "History", "delimiter": ";"}),
262+
ObjectInfo(daf.LoggerJSON, {"path": str(Path.home().joinpath("daf/History"))}),
263+
ObjectInfo(daf.LoggerSQL, {"database": str(Path.home().joinpath("daf/messages")), "dialect": "sqlite"}),
264+
ObjectInfo(daf.LoggerCSV, {"path": str(Path.home().joinpath("daf/History")), "delimiter": ";"}),
264265
]
265266

266267
self.combo_tracing["values"] = [en for en in daf.TraceLEVELS]

0 commit comments

Comments
 (0)