Catch up merge with master

# Conflicts (resolved):
#	bin/weeutil/weeutil.py
This commit is contained in:
Tom Keffer
2019-02-23 14:50:46 -08:00
4 changed files with 66 additions and 45 deletions

View File

@@ -184,4 +184,29 @@ def delete_scalar(a_dict, key):
return 0
del a_dict[key]
return 1
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

View File

@@ -15,6 +15,7 @@ import calendar
import datetime
import math
import os
import shutil
import syslog
import time
import traceback
@@ -30,6 +31,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):
@@ -41,31 +43,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
@@ -1420,6 +1397,37 @@ def y_or_n(msg, noprompt=False):
return ans
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

View File

@@ -13,7 +13,6 @@ import datetime
import ftplib
import glob
import os.path
import shutil
import socket
import sys
import syslog
@@ -447,23 +446,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))

View File

@@ -28,7 +28,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