From b6117e99456e7fd5a9efd4b87aaaae9da76e859e Mon Sep 17 00:00:00 2001 From: Ron Anderson Date: Wed, 22 Aug 2012 14:47:37 -0500 Subject: [PATCH] Issue #436 Added update script to relocate and update MakeHazardConfig for local effect based hazzards. Change-Id: Ie311cf19850cf4205f30282741a96c0cdc9f3c9c Former-commit-id: 82e45b58fa7b446bbb88c3a957d679de1d3a0e1e [formerly 5abacc273d7b0d3e6f620555bc8f8e52cdfd0085] [formerly 576ccd6a382c876a0c5bf4c94fcd635bc8ef1a84 [formerly 9c439cc43ffe38fcecaae552ad820f310b7f4267]] Former-commit-id: 576ccd6a382c876a0c5bf4c94fcd635bc8ef1a84 Former-commit-id: 19f956b93ff6de0d178ea3b19e82f9978579ed77 --- .../12.10.1/edex/UpdateMakeHazardConfig.py | 187 ++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 deltaScripts/12.10.1/edex/UpdateMakeHazardConfig.py diff --git a/deltaScripts/12.10.1/edex/UpdateMakeHazardConfig.py b/deltaScripts/12.10.1/edex/UpdateMakeHazardConfig.py new file mode 100644 index 0000000000..393ac912ce --- /dev/null +++ b/deltaScripts/12.10.1/edex/UpdateMakeHazardConfig.py @@ -0,0 +1,187 @@ +#!/usr/bin/env python +# Issue #436 +# Update script to move and modify MakeHazardConfig.py for local effects based hazards work assignment + +import os, re, string, sys + +configStartPattern = """########################################################### +############## ############### +############## CONFIGURATION SECTION ############### +############## ############### +""" + +configEndPattern = "#################### END CONFIGURATION SECTION #################" + +localEffectsAdditions = """# Dictionary mapping Hazard Types to applicable local effect areas +# that can be intersected with the zone edit areas. +# You should not define localEffectAreas entries for Tropical Cyclone +# or Convective Watches. +localEffectAreas = {} + +#localEffectAreas = { +# 'Winter Weather' : ["Below_1000","Below_1500","Below_2000","Below_2500","Below_3000","Below_3500","Below_4000", +# "Above_1000","Above_1500","Above_2000","Above_2500","Above_3000","Above_3500"], +# } + +# Dictionary associating local Effect Area names with a corresponding +# segment number, display name, and list of zones to be auto-selected +# If you do not wish to auto-select zones you should supply an empty list +# +# The display name allows you to display a "pretty" string in the UI rather +# than the edit area name. If the display name is empty ("") the edit area +# name will be used. +localAreaData = {} + +#localAreaData = { +# "Below_1000" : ( 999, "", []), +# "Below_1500" : (1499, "", []), +# "Below_2000" : (1999, "", []), +# "Below_2500" : (2499, "", []), +# "Below_3000" : (2999, "", []), +# "Below_3500" : (3499, "", []), +# "Below_4000" : (3999, "", []), +# "Above_1000" : (1000, "", []), +# "Above_1500" : (1500, "", []), +# "Above_2000" : (2000, "", []), +# "Above_2500" : (2500, "", []), +# "Above_3000" : (3000, "", []), +# "Above_3500" : (3500, "", []), +# } +""" + +hazardDictPattern = re.compile("hazardDict\s*=\s*\{(.*?)}", re.DOTALL) +entryPattern = re.compile("('.*?')\s*:\s*(\[.*?\])", re.DOTALL) +gumPattern = re.compile('\nsiteID = DataManager.getCurrentInstance\(\).getSiteID\(\)\nif siteID == "GUM":\n(.*?)\nelse:\n(.*?\n)\n', re.DOTALL) +sitePattern = re.compile("/awips2/edex/data/utility/cave_static/site/(.*?)/") +leadingSpacePattern = re.compile("(\s*?)\S") +commentPattern = re.compile("^", re.MULTILINE) + +orderdDictImport = "\n\nfrom collections import OrderedDict\n" +gumComment = "\n# for GUM use comment out the above definition and uncomment the one below\n\n" + +fixed = [] +status = 0 + +def fixEntry(m): + newEntry = "(" + m.string[m.start(1):m.end(1)] + ", " + m.string[m.start(2):m.end(2)] + ")" + return newEntry + +def fixHazardDict(m): + newDict = "hazardDict = OrderedDict([" + m.string[m.start(1):m.end(1)] + "])" + newDict = re.sub(entryPattern, fixEntry, newDict) + return newDict + +def unindent(s): + m = re.match(leadingSpacePattern, s) + if m is not None: + p = re.compile("^"+m.string[m.start(1):m.end(1)], re.MULTILINE) + s = re.sub(p, "", s) + return s + +def comment(s): + s = re.sub(commentPattern, "#", s) + return s + +def processDir(arg, srcDir, names): + global fixed, status + if arg in names: + src = os.path.join(srcDir, arg) + + # skip if already fixed + if src in fixed: + return + + print + if "userPython/procedures" in srcDir: + destDir = string.replace(srcDir, "userPython/procedures", "userPython/utilities") + if not os.path.exists(destDir): + os.makedirs(destDir) + dest = os.path.join(destDir, arg) + if os.path.exists(dest): + print "MakeHazardConfig.py exists in both\n\t"+srcDir+" and\n\t"+destDir+"\nPlease update this file manually" + status = 1 + return + else: + print "moving", src, "to utilities" + try: + os.rename(src, dest) + src = dest + except: + print "Error moving file\nPlease update this file manually" + status = 1 + return + + try: + f = open(src, 'r') + contents = f.read() + except: + print "Error reading file: ",src + print sys.exc_info()[0] + print "Please update this file manually" + status = 1 + return + finally: + try: + f.close() + except: + pass # if we read the file successfully don't care about close + + # skip if doesn't contain the old hazardDict pattern + if re.search(hazardDictPattern, contents) is None: + print src, "cannot be automatically updated.\nPlease manually update this file if required" + status = 1 + return + + print "updating", src + m = re.search(gumPattern, contents) + if m is not None: + # determine site ID + m1 = re.match(sitePattern, srcDir) + siteId = srcDir[m1.start(1):m1.end(1)] + + gumBlock = unindent(contents[m.start(1):m.end(1)]) + regBlock = unindent(contents[m.start(2):m.end(2)]) + if siteId == "GUM": + contents = contents[:m.start(0)] + orderdDictImport + comment(regBlock) + gumComment + gumBlock + "\n" + contents[m.end(0):] + else: + contents = contents[:m.start(0)] + orderdDictImport + regBlock + gumComment + comment(gumBlock) + "\n" + contents[m.end(0):] + + newContents = re.sub(hazardDictPattern, fixHazardDict, contents) + newContents = re.sub(configStartPattern, "", newContents) + newContents = re.sub(configEndPattern, "", newContents) + newContents += localEffectsAdditions + + try: + back = src+".BAK" + os.rename(src,back) + except: + print "Unable to create backup file, Please update this file manually" + status = 1 + return + + try: + f = open(src, 'w') + f.write(newContents) + except: + print "Error writing updated file:" + print sys.exc_info[0] + print "Please restore from", back, "and update manually" + status = 1 + finally: + try: + f.close() + fixed.append(src) + except: + print "Error closing updated file:" + print sys.exc_info[0] + print "Please restore from", back, "and update manually" + status = 1 + +def main(): + os.path.walk("/awips2/edex/data/utility/cave_static/site", processDir, "MakeHazardConfig.py") + if status != 0: + print "1 or more errors were encountered. Please review the log carefully." + return status + +if __name__ == "__main__": + sys.exit(main())