Closed bakerkj closed 7 years ago
diff --git a/bin/user/rtgd.py b/bin/user/rtgd.py
index 3f2cf67..ce4de60 100644
--- a/bin/user/rtgd.py
+++ b/bin/user/rtgd.py
@@ -311,6 +311,7 @@ import syslog
import threading
import time
import urllib2
+import distutils.dir_util
# weeWX imports
import weedb
@@ -614,11 +615,14 @@ class RealtimeGaugeDataThread(threading.Thread):
_html_root = os.path.join(config_dict['WEEWX_ROOT'],
config_dict['StdReport'].get('HTML_ROOT', ''))
- rtgd_path = os.path.join(_html_root, _path)
- self.rtgd_path_file = os.path.join(rtgd_path,
+ self.rtgd_path = os.path.join(_html_root, _path)
+ self.rtgd_path_file = os.path.join(self.rtgd_path,
rtgd_config_dict.get('rtgd_file_name',
'gauge-data.txt'))
+ # should we create the directory needed to place gauge-data file
+ self.rtgd_path_create = to_bool(rtgd_config_dict.get('rtgd_path_create', 'False'))
+
# get the remote server URL if it exists, if it doesn't set it to None
self.remote_server_url = rtgd_config_dict.get('remote_server_url', None)
# timeout to be used for remote URL posts
@@ -1014,6 +1018,9 @@ class RealtimeGaugeDataThread(threading.Thread):
data: dictionary of gauge-data.txt data elements
"""
+ if self.rtgd_path_create and not os.path.exists(self.rtgd_path):
+ distutils.dir_util.mkpath(self.rtgd_path)
+
with open(self.rtgd_path_file, 'w') as f:
json.dump(data, f, separators=(',', ':'), sort_keys=True)
I can see the value in creating the directory if it does not exist; that is the weeWX behaviour when generating reports. I am not so sure I see the need for a separate config option to create the directory, it seems like extra complexity for little benefit. We are running a service that creates a file, if we tell the service not to create the directory and it turns out the directory does not exist then the service can't do its job. I say just create the directory (if it doesn't exist), if the user has taken the trouble to alter the (benign) default path then I think we have to take him at his word.
I would also tend to use os.makedirs
rather than distutils.dir_util.mkpath
; I believe that distutils.dir_util.mkpath
caches the directory tree (or similar) and if the directory disappears after the first call to distutils.dir_util.mkpath
the directory concerned will be treated as if it still exists.
I have no objection to the directory creating it by default and switching to os.makedirs sounds entirely reasonable.
Implemented through use of os.makedirs
. No option to disable. Commit d270aead20249838c360d597ee1c46122c8b101a refers.
It would be great if there were an option to create the directory for gauge-data.txt if it doesn't exist (I use this option to create a directory under /dev/shm).