From c9b695a500463403b065d49af00a7412ed85841e Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Sat, 23 Feb 2019 05:38:43 -0800 Subject: [PATCH 1/2] Moved conditional_merge() to weeutil.config --- bin/weeutil/config.py | 27 ++++++++++++++++++++++++++- bin/weeutil/weeutil.py | 26 +------------------------- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/bin/weeutil/config.py b/bin/weeutil/config.py index c2f56897..4efe312e 100644 --- a/bin/weeutil/config.py +++ b/bin/weeutil/config.py @@ -174,4 +174,29 @@ def delete_scalar(a_dict, key): return 0 del a_dict[key] - return 1 \ No newline at end of file + return 1 + + +def conditional_merge(a_dict, b_dict): + """Merge fields from b_dict into a_dict, but only if they do not yet + exist in a_dict""" + # Go through each key in b_dict + for k in b_dict: + if isinstance(b_dict[k], dict): + if k not in a_dict: + # It's a new section. Initialize it... + a_dict[k] = {} + # ... and transfer over the section comments, if available + try: + a_dict.comments[k] = b_dict.comments[k] + except AttributeError: + pass + conditional_merge(a_dict[k], b_dict[k]) + elif k not in a_dict: + # It's a scalar. Transfer over the value... + a_dict[k] = b_dict[k] + # ... then its comments, if available: + try: + a_dict.comments[k] = b_dict.comments[k] + except AttributeError: + pass diff --git a/bin/weeutil/weeutil.py b/bin/weeutil/weeutil.py index 3f35e6dc..a8cbedfe 100644 --- a/bin/weeutil/weeutil.py +++ b/bin/weeutil/weeutil.py @@ -33,6 +33,7 @@ accumulateLeaves = config.accumulateLeaves merge_config = config.merge_config patch_config = config.patch_config comment_scalar = config.comment_scalar +conditional_merge= config.conditional_merge def convertToFloat(seq): @@ -44,31 +45,6 @@ def convertToFloat(seq): return res -def conditional_merge(a_dict, b_dict): - """Merge fields from b_dict into a_dict, but only if they do not yet - exist in a_dict""" - # Go through each key in b_dict - for k in b_dict: - if isinstance(b_dict[k], dict): - if k not in a_dict: - # It's a new section. Initialize it... - a_dict[k] = {} - # ... and transfer over the section comments, if available - try: - a_dict.comments[k] = b_dict.comments[k] - except AttributeError: - pass - conditional_merge(a_dict[k], b_dict[k]) - elif k not in a_dict: - # It's a scalar. Transfer over the value... - a_dict[k] = b_dict[k] - # ... then its comments, if available: - try: - a_dict.comments[k] = b_dict.comments[k] - except AttributeError: - pass - - def option_as_list(option): if option is None: return None From 2063dfa3aba65e747c1f47a222d43163b72d584a Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Sat, 23 Feb 2019 14:36:10 -0800 Subject: [PATCH 2/2] Fixed problem that prevented to CopyGenerator from handling nested directories. Fixes issue #379. --- bin/weeutil/weeutil.py | 33 +++++++++++++++++++++++++++++++++ bin/weewx/reportengine.py | 21 +++------------------ docs/changes.txt | 5 ++++- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/bin/weeutil/weeutil.py b/bin/weeutil/weeutil.py index a8cbedfe..2ad0c753 100644 --- a/bin/weeutil/weeutil.py +++ b/bin/weeutil/weeutil.py @@ -16,6 +16,7 @@ import datetime import math import os import struct +import shutil import syslog import time import traceback @@ -1415,6 +1416,38 @@ def int2byte(x): """Convert integer argument to signed byte string, under both Python 2 and 3""" return struct.pack('>b', x) + +def deep_copy_path(path, dest_dir): + """Copy a path to a destination, making any subdirectories along the way. + The source path is relative to the current directory. + + Returns the number of files copied + """ + + ncopy = 0 + # Are we copying a directory? + if os.path.isdir(path): + # Yes. Walk it + for dirpath, _, filenames in os.walk(path): + for f in filenames: + # For each source file found, call myself recursively: + ncopy += deep_copy_path(os.path.join(dirpath, f), dest_dir) + else: + # path is a file. Get the directory it's in. + d = os.path.dirname(os.path.join(dest_dir, path)) + # Make the destination directory, wrapping it in a try block in + # case it already exists: + try: + os.makedirs(d) + except OSError: + pass + # This version of copy does not copy over modification time, + # so it will look like a new file, causing it to be (for + # example) ftp'd to the server: + shutil.copy(path, d) + ncopy += 1 + return ncopy + if __name__ == '__main__': import doctest diff --git a/bin/weewx/reportengine.py b/bin/weewx/reportengine.py index e7ed57f7..d4d8f558 100644 --- a/bin/weewx/reportengine.py +++ b/bin/weewx/reportengine.py @@ -12,7 +12,6 @@ import datetime import ftplib import glob import os.path -import shutil import socket import sys import syslog @@ -444,23 +443,9 @@ class CopyGenerator(ReportGenerator): # list globbing any character expansions ncopy = 0 for pattern in copy_list: - # Glob this pattern; then go through each resultant filename: - for _file in glob.glob(pattern): - # Final destination is the join of the html destination - # directory and any relative subdirectory on the filename: - dest_dir = os.path.join(html_dest_dir, os.path.dirname(_file)) - # Make the destination directory, wrapping it in a try block in - # case it already exists: - try: - os.makedirs(dest_dir) - except OSError: - pass - # This version of copy does not copy over modification time, - # so it will look like a new file, causing it to be (for - # example) ftp'd to the server: - shutil.copy(_file, dest_dir) - ncopy += 1 - + # Glob this pattern; then go through each resultant path: + for path in glob.glob(pattern): + ncopy += weeutil.weeutil.deep_copy_path(path, html_dest_dir) if log_success: syslog.syslog(syslog.LOG_INFO, "copygenerator: " "copied %d files to %s" % (ncopy, html_dest_dir)) diff --git a/docs/changes.txt b/docs/changes.txt index 65c12957..af8bb8f0 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -16,7 +16,10 @@ record interval is zero. Fixes issue #375. Posts to the Weather Underground now use https, instead of http. Thanks to user mljenkins! PR #378. -Fixed problem with handling CWOP connection errors. +Fixed problem with handling CWOP connection errors. Commit 0a21a72 + +Fixed problem that prevented to CopyGenerator from handling nested +directories. Fixes issue #379. 3.9.1 02/06/2019