diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index 8b3052d..c1929bc 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -31,11 +31,9 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"): DEBUG_GUILD, DEBUG_CHANNEL, data_path: Path, - share_path: Path, ): super().__init__() self.data_path = data_path - self.share_path = share_path self.debug_channel = None self.bot = bot self._last_member_ = None @@ -59,7 +57,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"): self.awaiting_responses = [] try: - conversation_file_path = share_path / "conversation_starter_pretext.txt" + conversation_file_path = EnvService.find_shared_file("conversation_starter_pretext.txt") # Attempt to read a conversation starter text string from the file. with conversation_file_path.open("r") as f: self.CONVERSATION_STARTER_TEXT = f.read() @@ -68,9 +66,7 @@ class GPT3ComCon(commands.Cog, name="GPT3ComCon"): ) assert self.CONVERSATION_STARTER_TEXT is not None - conversation_file_path_minimal = ( - share_path / "conversation_starter_pretext_minimal.txt" - ) + conversation_file_path_minimal = EnvService.find_shared_file("conversation_starter_pretext_minimal.txt") with conversation_file_path_minimal.open("r") as f: self.CONVERSATION_STARTER_TEXT_MINIMAL = f.read() print( diff --git a/cogs/image_prompt_optimizer.py b/cogs/image_prompt_optimizer.py index 9b56af0..6c6eea0 100644 --- a/cogs/image_prompt_optimizer.py +++ b/cogs/image_prompt_optimizer.py @@ -35,9 +35,7 @@ class ImgPromptOptimizer(commands.Cog, name="ImgPromptOptimizer"): self.deletion_queue = deletion_queue try: - image_pretext_path = ( - self.converser_cog.share_path / "image_optimizer_pretext.txt" - ) + image_pretext_path = EnvService.find_shared_file("image_optimizer_pretext.txt") # Try to read the image optimizer pretext from # the file system with image_pretext_path.open("r") as file: diff --git a/gpt3discord.py b/gpt3discord.py index fb4ad05..f9e5395 100644 --- a/gpt3discord.py +++ b/gpt3discord.py @@ -64,7 +64,6 @@ async def on_application_command_error( async def main(): - share_path = EnvService.environment_path_with_fallback("SHARE_DIR", "share") data_path = EnvService.environment_path_with_fallback("DATA_DIR") debug_guild = int(os.getenv("DEBUG_GUILD")) debug_channel = int(os.getenv("DEBUG_CHANNEL")) @@ -72,9 +71,6 @@ async def main(): if not data_path.exists(): raise OSError(f"Data path: {data_path} does not exist ... create it?") - if not share_path.exists(): - raise OSError(f"Share path: {share_path} does not exist ... create it?") - # Load the main GPT3 Bot service bot.add_cog( GPT3ComCon( @@ -86,7 +82,6 @@ async def main(): debug_guild, debug_channel, data_path, - share_path, ) ) diff --git a/models/env_service_model.py b/models/env_service_model.py index 23a0e5f..99f6cb9 100644 --- a/models/env_service_model.py +++ b/models/env_service_model.py @@ -4,12 +4,14 @@ from pathlib import Path from dotenv import load_dotenv -# /../../ def app_root_path(): + app_path = Path(sys.argv[0]).resolve() try: - return Path(sys.argv[0]).resolve().parents[1] - except: - return Path() + if app_path.parent.name == "bin": # Installed in unixy hierachy + return app_path.parents[1] + except IndexError: + pass + return app_path.parent # None will let direnv do its' thing env_paths = [Path() / ".env", app_root_path() / "etc/environment", None] @@ -35,7 +37,21 @@ class EnvService: if app_relative.exists(): return app_relative - return Path() + return Path.cwd() + + @staticmethod + def find_shared_file(file_name): + share_file_paths = [] + share_dir = os.getenv("SHARE_DIR") + if share_dir != None: + share_file_paths.append(share_dir) + share_file_paths.extend([app_root_path() / "share" / file_name, app_root_path() / file_name, Path(file_name)]) + + for share_file_path in share_file_paths: + if share_file_path.exists(): + return share_file_path.resolve() + + raise ValueError(f"Unable to find shared data file {file_name}") @staticmethod def get_allowed_guilds():