From 3209b4e1fbbd00846062e8868fd9bf4fd55cb663 Mon Sep 17 00:00:00 2001
From: David Sangrey <rixxan@hullseals.space>
Date: Thu, 6 Jun 2024 17:25:51 -0400
Subject: [PATCH] [2251] ResPath Update

---
 build.py     |  2 +-
 collate.py   |  6 +++++-
 companion.py |  4 ++--
 update.py    | 18 +++++++++++++++---
 4 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/build.py b/build.py
index 2483a4e0..c5236c5a 100644
--- a/build.py
+++ b/build.py
@@ -208,5 +208,5 @@ def build() -> None:
 
 
 if __name__ == "__main__":
-    check_for_fdev_updates()
+    check_for_fdev_updates(local=True)
     build()
diff --git a/collate.py b/collate.py
index caf5994f..380cb8ba 100755
--- a/collate.py
+++ b/collate.py
@@ -22,6 +22,7 @@ from traceback import print_exc
 
 import companion
 import outfitting
+from config import config
 from edmc_data import companion_category_map, ship_name_map
 
 
@@ -50,7 +51,10 @@ def addcommodities(data) -> None:  # noqa: CCR001
     if not data['lastStarport'].get('commodities'):
         return
 
-    commodityfile = pathlib.Path('FDevIDs/commodity.csv')
+    try:
+        commodityfile = pathlib.Path(config.app_dir_path / 'FDevIDs' / 'commodity.csv')
+    except FileNotFoundError:
+        commodityfile = pathlib.Path('FDevIDs/commodity.csv')
     commodities = {}
 
     # slurp existing
diff --git a/companion.py b/companion.py
index 934fc8bb..775da295 100644
--- a/companion.py
+++ b/companion.py
@@ -1203,10 +1203,10 @@ def fixup(data: CAPIData) -> CAPIData:  # noqa: C901, CCR001 # Can't be usefully
     if not commodity_map:
         # Lazily populate
         for f in ('commodity.csv', 'rare_commodity.csv'):
-            if not os.path.isfile(config.respath_path / 'FDevIDs/' / f):
+            if not os.path.isfile(config.app_dir_path / 'FDevIDs/' / f):
                 logger.warning(f'FDevID file {f} not found! Generating output without these commodity name rewrites.')
                 continue
-            with open(config.respath_path / 'FDevIDs' / f, 'r') as csvfile:
+            with open(config.app_dir_path / 'FDevIDs' / f, 'r') as csvfile:
                 reader = csv.DictReader(csvfile)
 
                 for row in reader:
diff --git a/update.py b/update.py
index d6364c2d..9dd6229a 100644
--- a/update.py
+++ b/update.py
@@ -8,6 +8,7 @@ See LICENSE file.
 from __future__ import annotations
 
 import pathlib
+import shutil
 import sys
 import threading
 from traceback import print_exc
@@ -26,21 +27,32 @@ if TYPE_CHECKING:
 logger = get_main_logger()
 
 
-def check_for_fdev_updates(silent: bool = False) -> None:  # noqa: CCR001
+def check_for_fdev_updates(silent: bool = False, local: bool = False) -> None:  # noqa: CCR001
     """Check for and download FDEV ID file updates."""
+    if local:
+        pathway = config.app_dir_path
+    else:
+        pathway = config.respath_path
+
     files_urls = [
         ('commodity.csv', 'https://raw.githubusercontent.com/EDCD/FDevIDs/master/commodity.csv'),
         ('rare_commodity.csv', 'https://raw.githubusercontent.com/EDCD/FDevIDs/master/rare_commodity.csv')
     ]
 
     for file, url in files_urls:
-        fdevid_file = pathlib.Path(config.respath_path / 'FDevIDs' / file)
+        fdevid_file = pathlib.Path(pathway / 'FDevIDs' / file)
         fdevid_file.parent.mkdir(parents=True, exist_ok=True)
         try:
             with open(fdevid_file, newline='', encoding='utf-8') as f:
                 local_content = f.read()
         except FileNotFoundError:
-            local_content = None
+            logger.info(f'File {file} not found. Writing from bundle...')
+            for localfile in files_urls:
+                filepath = f"FDevIDs/{localfile[0]}"
+                shutil.copy(filepath, pathway / 'FDevIDs')
+                fdevid_file = pathlib.Path(pathway / 'FDevIDs' / file)
+            with open(fdevid_file, newline='', encoding='utf-8') as f:
+                local_content = f.read()
 
         response = requests.get(url)
         if response.status_code != 200: