Repository: akrherz/pyWWA

Issue:
Allow installed pywwa to find settings.json
Presently, if `pywwa` is installed into the python environment, the code that loads the `settings.json` file will fail due to confused paths.  Will need to boggle how to do this.

Discussion:

Patch:
diff --git a/README.md b/README.md
index 9e730fb..a6b935c 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,18 @@ This repository is an integration of [pyIEM backed](https://github.com/akrherz/p
 
 Python 3.6+ is required to use this code.
 
+Installation
+------------
+
+An opinionated installation is to clone this repo in the LDM user's `$HOME` directory and then update LDM's pqact.datadir-path setting (found in the `registry.xml`) to point to LDM's home directory.  It is then suggested that you create sym links from the LDM user's `etc/` directory to the `pqact.d` files found within this repo.  For example:
+
+```bash
+cd ~/etc
+ln -s ../pyWWA/pqact.d/pqact_iemingest.conf
+```
+
+This suggestion allows LDM's pqact state files to reside in a directory outside the pyWWA repo tree.  The `pyWWA` pqact files assume that the above has been done, such that `pqact` searches the LDM `$HOME` directory for the `pyWWA` folder.
+
 Command Line Options
 --------------------
 
@@ -55,3 +67,16 @@ Logging
 
 These parsers emit logs to the syslog `LOCAL2` facility via a wild mixture of
 Twisted Python and Stdlib Python log statements.
+
+Locating the settings.json file
+-------------------------------
+
+This is a bit of a hack yet within the codebase, but some crude logic is used attempting to figure out where the `pyWWA/settings.json` file resides.  Since the various scripts could be getting executed from various current working directories, the code checks the following locations.
+
+1. The file `pyWWA/settings.json` exists from the `cwd`
+
+2. The `cwd` of the process.
+
+3. One directory up from the calling script.
+
+4. Two directories up from the calling script.
diff --git a/parsers/pywwa/__init__.py b/parsers/pywwa/__init__.py
index c94ca5d..504d3e9 100644
--- a/parsers/pywwa/__init__.py
+++ b/parsers/pywwa/__init__.py
@@ -1,14 +1,33 @@
 """pyWWA local module."""
 # stdlib
+import inspect
 import json
 import os
 
 # Shared configuration
 SETTINGS = {}
 
-try:
-    CONFIG = json.load(
-        open(os.path.join(os.path.dirname(__file__), "../../settings.json"))
-    )
-except FileNotFoundError:
-    CONFIG = {}
+
+def load_config():
+    """Attempt to locate our configuration file."""
+    # 1. `cwd`/pyWWA/settings.json
+    # 2. `cwd`/settings.json
+    # 3. scriptloc/../settings.json
+    # 4. scriptloc/../../settings.json
+    for relpath in ["pyWWA", ""]:
+        testfn = os.path.join(os.getcwd(), relpath, "settings.json")
+        print(testfn)
+        if not os.path.isfile(testfn):
+            continue
+        return json.load(open(testfn))
+    scriptpath = os.path.dirname(os.path.abspath(inspect.stack()[-1].filename))
+    for relpath in ["..", "../.."]:
+        testfn = os.path.join(scriptpath, relpath, "settings.json")
+        print(testfn)
+        if not os.path.isfile(testfn):
+            continue
+        return json.load(open(testfn))
+    return {}
+
+
+CONFIG = load_config()


Test patch:
diff --git a/tests/test_init.py b/tests/test_init.py
new file mode 100644
index 0000000..4ba3e18
--- /dev/null
+++ b/tests/test_init.py
@@ -0,0 +1,17 @@
+"""Test pywwa/__init__.py"""
+# stdlin
+import os
+import shutil
+
+# Local
+import pywwa
+
+
+def test_load_settings():
+    """Test that we can load settings."""
+    if os.path.isfile("settings.json"):
+        shutil.move("settings.json", "settings.json.save")
+    res = pywwa.load_config()
+    assert isinstance(res, dict)
+    if os.path.isfile("settings.json.save"):
+        shutil.move("settings.json.save", "settings.json")