Skip to content

Commit 1af8cab

Browse files
authored
Merge pull request #12 from A-Baji/dev
switch to pathlib
2 parents e8c1561 + 9156c07 commit 1af8cab

File tree

6 files changed

+30
-31
lines changed

6 files changed

+30
-31
lines changed

discordai/bot/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import asyncio
1010
import os
11+
import pathlib
1112
import platform
1213
import shutil
1314
import sys
@@ -119,18 +120,18 @@ async def load_cogs() -> None:
119120
"""
120121
if getattr(sys, 'frozen', False):
121122
# The code is being run as a frozen executable
122-
data_dir = appdirs.user_data_dir(appauthor="Adib Baji", appname="discordai")
123-
cogs_path = os.path.join(data_dir, "discordai/bot/cogs")
123+
data_dir = pathlib.Path(appdirs.user_data_dir(appname="discordai"))
124+
cogs_path = data_dir / "discordai" / "bot" / "cogs"
124125
if not os.path.exists(cogs_path):
125-
data_dir = sys._MEIPASS
126-
og_cogs_path = os.path.join(data_dir, "discordai/bot/cogs")
127-
shutil.copytree(og_cogs_path, os.path.join(data_dir, cogs_path))
126+
data_dir = pathlib.Path(sys._MEIPASS)
127+
og_cogs_path = data_dir / "discordai" / "bot" / "cogs"
128+
shutil.copytree(og_cogs_path, data_dir / cogs_path)
128129
for file in os.listdir(cogs_path):
129130
if file.endswith(".py"):
130131
extension = file[:-3]
131132
if extension != "__init__":
132133
try:
133-
module_path = os.path.join(cogs_path, f'{extension}.py')
134+
module_path = cogs_path / f'{extension}.py'
134135
spec = importlib.util.spec_from_file_location(extension, module_path)
135136
module = importlib.util.module_from_spec(spec)
136137
spec.loader.exec_module(module)
@@ -142,8 +143,8 @@ async def load_cogs() -> None:
142143
print(f"Failed to load extension {extension}\n{exception}")
143144
else:
144145
# The code is being run normally
145-
bot_dir = os.path.dirname(__file__)
146-
cogs_path = os.path.join(bot_dir, "cogs")
146+
bot_dir = pathlib.Path(os.path.dirname(__file__))
147+
cogs_path = bot_dir / "cogs"
147148
for file in os.listdir(cogs_path):
148149
if file.endswith(".py"):
149150
extension = file[:-3]

discordai/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import json
44
import appdirs
55

6-
config_dir = appdirs.user_data_dir(appauthor="Adib Baji", appname="discordai")
6+
config_dir = pathlib.Path(appdirs.user_data_dir(appname="discordai"))
77

88

99
def get():
1010
try:
11-
with open(f"{config_dir}/config.json", 'r') as file:
11+
with open(config_dir / "config.json", 'r') as file:
1212
config = json.load(file)
1313
except FileNotFoundError as err:
1414
print("No config found. Please follow the steps to create one:")
@@ -20,7 +20,7 @@ def get():
2020
with open(pathlib.Path(config_dir, "config.json"), "w") as f:
2121
f.write(json.dumps(config))
2222

23-
print(f"Your config has been saved to {config_dir}/config.json")
23+
print(f"Your config has been saved to {config_dir / 'config.json'}")
2424
return config
2525

2626

discordai/template.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import os
22
import pathlib
3-
import pkgutil
43
import shutil
54
import sys
6-
import tempfile
75
import appdirs
86

97

@@ -61,23 +59,23 @@ async def setup(bot):
6159
await bot.add_cog({class_name}(bot))
6260
"""
6361

64-
config_dir = appdirs.user_data_dir(appauthor="Adib Baji", appname="discordai")
62+
config_dir = pathlib.Path(appdirs.user_data_dir(appname="discordai"))
6563

6664

6765
def gen_new_command(model_id: str, command_name: str, temp_default: float, pres_default: float, freq_default: float,
6866
max_tokens_default: int, stop_default: bool, openai_key: str):
6967
if getattr(sys, 'frozen', False):
7068
# The code is being run as a frozen executable
71-
data_dir = appdirs.user_data_dir(appauthor="Adib Baji", appname="discordai")
72-
cogs_path = os.path.join(data_dir, "discordai/bot/cogs")
69+
data_dir = pathlib.Path(appdirs.user_data_dir(appname="discordai"))
70+
cogs_path = data_dir / "discordai" / "bot" / "cogs"
7371
if not os.path.exists(cogs_path):
74-
data_dir = sys._MEIPASS
75-
og_cogs_path = os.path.join(data_dir, "discordai/bot/cogs")
76-
shutil.copytree(og_cogs_path, os.path.join(data_dir, cogs_path))
72+
data_dir = pathlib.Path(sys._MEIPASS)
73+
og_cogs_path = data_dir / "discordai" / "bot" / "cogs"
74+
shutil.copytree(og_cogs_path, data_dir / cogs_path)
7775
else:
7876
# The code is being run normally
79-
template_dir = os.path.dirname(__file__)
80-
cogs_path = os.path.join(os.path.join(template_dir, "bot"), "cogs")
77+
template_dir = pathlib.Path(os.path.dirname(__file__))
78+
cogs_path = template_dir / "bot"/ "cogs"
8179
with open(pathlib.Path(cogs_path, f"{command_name}.py"), "w") as f:
8280
os.makedirs(cogs_path, exist_ok=True)
8381
f.write(
@@ -98,16 +96,16 @@ def delete_command(command_name: str):
9896
return
9997
if getattr(sys, 'frozen', False):
10098
# The code is being run as a frozen executable
101-
data_dir = appdirs.user_data_dir(appauthor="Adib Baji", appname="discordai")
102-
cogs_path = os.path.join(data_dir, "discordai/bot/cogs")
99+
data_dir = pathlib.Path(appdirs.user_data_dir(appname="discordai"))
100+
cogs_path = data_dir / "discordai" / "bot" / "cogs"
103101
if not os.path.exists(cogs_path):
104-
data_dir = sys._MEIPASS
105-
og_cogs_path = os.path.join(data_dir, "discordai/bot/cogs")
106-
shutil.copytree(og_cogs_path, os.path.join(data_dir, cogs_path))
102+
data_dir = pathlib.Path(sys._MEIPASS)
103+
og_cogs_path = data_dir / "discordai" / "bot" / "cogs"
104+
shutil.copytree(og_cogs_path, data_dir / cogs_path)
107105
else:
108106
# The code is being run normally
109-
template_dir = os.path.dirname(__file__)
110-
cogs_path = os.path.join(os.path.join(template_dir, "bot"), "cogs")
107+
template_dir = pathlib.Path(os.path.dirname(__file__))
108+
cogs_path = template_dir / "bot" / "cogs"
111109
try:
112110
os.remove(pathlib.Path(cogs_path, f"{command_name}.py"))
113111
print(f"Successfully deleted command: /{command_name}")

discordai/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.1.0"
1+
__version__ = "1.1.1"

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
discord.py
22
openai
3+
pandas
34
appdirs
4-
discordai_modelizer @ git+https://github.com/A-Baji/discordAI-modelizer.git#egg=discord-modelizer
5+
discordai_modelizer @ git+https://github.com/A-Baji/discordAI-modelizer.git@1.0.2

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
setuptools.setup(
1818
name=package.__name__,
1919
version=package.__version__,
20-
author="Adib Baji",
2120
author_email="[email protected]",
2221
description="A discord bot package that utilizes openAI to create custom AI models out of your chat history",
2322
long_description=pathlib.Path("README.md").read_text(),

0 commit comments

Comments
 (0)