From 6f8a3467553d2ee3834dafbb2571dc0ea4f15ed0 Mon Sep 17 00:00:00 2001 From: Samuel Lapointe Date: Fri, 12 Jun 2015 09:56:40 -0400 Subject: [PATCH 1/4] Added a _ delimiter between the layer name and layer number --- scribe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scribe.py b/scribe.py index 1039931..de008ec 100644 --- a/scribe.py +++ b/scribe.py @@ -248,7 +248,7 @@ def write(string, scales, files, indentation): indentation = substractIndentation(indentation, INDENTATION) elif re.match(r"^(NAME|MASK)", string, re.IGNORECASE): text = string - text = re.sub(r"'$", value + "'", text) + text = re.sub(r"'$", '_' + value + "'", text) text = re.sub(r"\"$", value + "\"", text) files[value].write(indentation + text + "\n") else: From db44a23d4ccd16987b7b78d7ab0b9f71e69c9106 Mon Sep 17 00:00:00 2001 From: Samuel Lapointe Date: Wed, 1 Jul 2015 08:34:54 -0400 Subject: [PATCH 2/4] Fix an issue where every NAME tag is changed --- scribe.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scribe.py b/scribe.py index de008ec..15379a5 100644 --- a/scribe.py +++ b/scribe.py @@ -246,10 +246,10 @@ def write(string, scales, files, indentation): files[value].write(indentation + "MINSCALEDENOM " + str(scales[value]) + "\n") indentation = substractIndentation(indentation, INDENTATION) - elif re.match(r"^(NAME|MASK)", string, re.IGNORECASE): + elif re.match(r"^(NAME|MASK)", string, re.IGNORECASE) and scales.itervalues().next() is not None: text = string text = re.sub(r"'$", '_' + value + "'", text) - text = re.sub(r"\"$", value + "\"", text) + text = re.sub(r"\"$", '_' + value + "\"", text) files[value].write(indentation + text + "\n") else: files[value].write(indentation + string + "\n") From 9ce55ac92599912fb3b4eccfe21e7d38c8f5a8e3 Mon Sep 17 00:00:00 2001 From: Samuel Lapointe Date: Mon, 20 Jul 2015 13:36:40 -0400 Subject: [PATCH 3/4] Fix trailing whitespaces --- scribe.py | 81 +++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/scribe.py b/scribe.py index 15379a5..6a26454 100644 --- a/scribe.py +++ b/scribe.py @@ -29,13 +29,13 @@ def parseDict(data, scales, files, indentation): if d == "VARIABLE": value = VAR[data[d]] vType = type(value) - + if vType == type(list()): parseList(data[d], value, scales, files, indentation, False) elif data[d][:9] == "VARIABLE:": value = parseVariable(data[d][9:]) vType = type(value) - + if vType == type(list()): parseList(d, value, scales, files, indentation) elif vType == type(unicode()): @@ -57,7 +57,7 @@ def parseDict(data, scales, files, indentation): if isScale(d) == False: write(d + " " + value, scales, files, indentation) elif dType == type(int()): - value = str(data[d]) + value = str(data[d]) if isScale(value) == False: write(d + " " + value, scales, files, indentation) @@ -92,7 +92,7 @@ def parseScaleList(d, data, files, minScale, maxScale, indentation, close=True): for item in data: for scale in item: scales = scaleToScaleList(scale, minScale, maxScale) - + if scale == "VARIABLE": key = item[scale] value = VAR[key] @@ -111,7 +111,7 @@ def parseScaleList(d, data, files, minScale, maxScale, indentation, close=True): elif vType == type(list()): write(d, scales, files, indentation) indentation = addIndentation(indentation, INDENTATION) - parseList(key, value, scales, files, indentation) + parseList(key, value, scales, files, indentation) elif vType == type(unicode()): write(d + " " + value, scales, files, indentation) elif vType == type(int()): @@ -119,7 +119,7 @@ def parseScaleList(d, data, files, minScale, maxScale, indentation, close=True): if closeTag(data) == True: indentation = substractIndentation(indentation, INDENTATION) - write("END", scales, files, indentation) + write("END", scales, files, indentation) def parseScale(scale, data, files, minScale, maxScale, indentation): @@ -136,11 +136,11 @@ def parseScale(scale, data, files, minScale, maxScale, indentation): value = parseVariable(d[item][9:]) else: key = item - value = d[key] + value = d[key] else: key = item value = d[key] - + vType = type(value) if vType == type(dict()): @@ -158,7 +158,7 @@ def parseVariable(value): var = VAR[value] if var[:9] == "VARIABLE:": var = parseVariable(var[9:]) - + return var @@ -170,9 +170,9 @@ def scaleToScaleList(scale, minScale, maxScale): for i in range(s1, s2 + 1): if (i >= minScale) and (i <= maxScale): scales[str(i)] = SCALES[str(i)] - + elif re.match(r"[0-9]+", scale): - if (int(scale) >= minScale) and (int(scale) <= maxScale): + if (int(scale) >= minScale) and (int(scale) <= maxScale): scales[scale] = SCALES[scale] return scales @@ -188,7 +188,7 @@ def isScale(string): def isScaleList(data): scale = False for value in data: - for d in value: + for d in value: if isScale(d) == True: scale = True else: @@ -267,7 +267,7 @@ def openLayerFiles(directory, scales): return layerFiles -def openMapFile(directory, name): +def openMapFile(directory, name): mapFile = codecs.open(directory + name + ".map", encoding='utf-8', mode="w+") return {"1": mapFile} @@ -295,7 +295,7 @@ def jsonToMap(content, outputDirectory, mapName, clean): mapFile = openMapFile(outputDirectory, mapName) parseList("MAP", MAP, {"1": None}, mapFile, "") mapFile["1"].seek(-4, 2) - + for value in range(1, len(SCALES) + 1): if clean == True: write("INCLUDE 'level" + str(value) + ".map'", {"1": None}, mapFile, "") @@ -318,7 +318,7 @@ def string2json(string): comments = re.findall(r"\#\#.*", t, flags=0) t = re.sub(r"\#\#.*", "VOID:FLAGCOMMENT\n", t, flags=0) #Remove tabs - t = re.sub(r"\t", " ", t) + t = re.sub(r"\t", " ", t) #Replace @ with VARIABLE: t = re.sub(r"@", "VARIABLE:", t) #Find and replace the text between {{ and }} (useful for blocks like PROJECTION, METADATA, PATTERN etc.) @@ -345,16 +345,16 @@ def string2json(string): #Replace line break with \""},\n t = re.sub(r"\"[\s\n]+", "\\\"\"},\n", t) #Replace spaces preceded by alphabetic characters or _ with "},\n - t = re.sub(r"(?<=[a-zA-Z_])\s", "\"},\n", t) + t = re.sub(r"(?<=[a-zA-Z_])\s", "\"},\n", t) #Replace , followed by a ine break and ] with ] - t = re.sub(r",\s+\n*\]", "]", t) + t = re.sub(r",\s+\n*\]", "]", t) #Replace ] not preceded by and alphanumeric chracter with ]} - t = re.sub(r"(?> sys.stderr, str(err) - sys.exit(2) - + print >> sys.stderr, str(err) + sys.exit(2) + for opt, arg in opts: if opt in ("-i", "--input"): inputDirectory = arg elif opt in ('-o', "--ouput"): - outputDirectory = arg + outputDirectory = arg elif opt in ("-n", "--name"): mapName = arg elif opt in ("-c", "--clean"): @@ -580,7 +580,7 @@ def main(): outputJSONFile = arg elif opt in ("-d", "--debug"): debugLevel = int(arg) - + if os.path.isfile(inputDirectory + "scales"): inputScalesFile = codecs.open(inputDirectory + "scales", encoding='utf-8') inputScalesContent = inputScalesFile.read() @@ -614,7 +614,7 @@ def main(): inputLayersContent = "LAYERS {\n" groupFiles = [""] * (len(jsonConfig["ORDER"]) + 1) groups= [] - + for i in range(0, len(jsonConfig["ORDER"])): for j in jsonConfig["ORDER"][i]: if jsonConfig["ORDER"][i][j][:1] == "/": @@ -626,7 +626,7 @@ def main(): if (int(j) > 0 and int(j) <= len(jsonConfig["ORDER"])): groupFiles[int(j)] = layerFilePath else: - error += "Index " + j + " out of bounds in config file.\n" + error += "Index " + j + " out of bounds in config file.\n" else: error += "File '" + layerFilePath + "' not found.\n" @@ -650,7 +650,7 @@ def main(): if outputJSONFile is not None: jFile = codecs.open(outputJSONFile, encoding='utf-8', mode="w+") jFile.write(jsonContent.encode('utf-8')) - + try: jsonToMap(jsonContent, outputDirectory, mapName, clean) if debugLevel >= 0 and debugLevel <= 5: #debug using shp2img @@ -662,8 +662,7 @@ def main(): else: print >> sys.stderr, error sys.exit(2) - + if __name__ == "__main__": main() - From 7cddc7e67c563e184b29496bc5f5565f43c1abcc Mon Sep 17 00:00:00 2001 From: Samuel Lapointe Date: Tue, 21 Jul 2015 08:27:01 -0400 Subject: [PATCH 4/4] Fix some issues with comments --- scribe.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scribe.py b/scribe.py index 6a26454..33de10d 100644 --- a/scribe.py +++ b/scribe.py @@ -310,13 +310,15 @@ def jsonToMap(content, outputDirectory, mapName, clean): def string2json(string): - #Remove the comments preceded by // + #Remove the comments (not between '' or "") preceded by // t = re.sub(ur'("(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\])*\'|(?:[^/\n"\']|/[^/*\n"\'])+|\n)|(/\* (?:[^*]|\*[^/])*\*/)|(?://(.*)$)$', lambda m: m.group(1), string, flags=re.MULTILINE) #Remove the comments between /* and */ t = re.sub(r"/\*.*?\t*?\*/", "", t, flags=re.DOTALL) #Find and replace the comments preceded by ## comments = re.findall(r"\#\#.*", t, flags=0) - t = re.sub(r"\#\#.*", "VOID:FLAGCOMMENT\n", t, flags=0) + t = re.sub(r"\#\#.*", "\n\nVOID:FLAGCOMMENT\n", t, flags=0) + #Take any comments located on any line with content on it and place it alone on a line above the original one. + t = re.sub(r"((?![ \t]).+)(VOID:FLAGCOMMENT.*)", r"\2\n\n\1", t, flags=0) #Remove tabs t = re.sub(r"\t", " ", t) #Replace @ with VARIABLE: