Skip to content

Commit

Permalink
Merge pull request giswqs#6 from cclauss/patch-4
Browse files Browse the repository at this point in the history
Simplify and sort keys using a triple quoted string
  • Loading branch information
giswqs authored Dec 30, 2019
2 parents 02a417f + b0224d6 commit 619f1a8
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions convert_js_to_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

def dict_key_str(line):

keys = ['bands', 'min', 'max', 'gain', 'bias', 'gamma', 'palette', 'opacity',
'format', 'radius', 'units', 'normalize', 'kernel', 'iterations', 'threshold',
'sigma', 'magnitude', 'size', 'connectedness', 'maxSize', 'eightConnected',
'reducer', 'labelBand', 'color', 'source', 'maxDistance', 'referenceImage',
'maxOffset', 'patchWidth', 'strokeWidth', 'width', 'geometry', 'scale', 'maxPixels',
'collection', 'selectors', 'leftField', 'rightField']
keys = """bands bias collection color connectedness eightConnected format gain gamma
geometry iterations kernel labelBand leftField magnitude max maxDistance
maxOffset maxPixels maxSize min normalize opacity palette patchWidth
radius reducer referenceImage rightField scale selectors sigma size source
strokeWidth threshold units width""".split()
for key in keys:
if ":" in line and key in line:
line = line.replace(key + ":", "'" + key + "':")
Expand All @@ -35,29 +34,24 @@ def js_to_python(in_file):
with open(in_file_path) as f:
lines = f.readlines()
for line in lines:
line = line.replace("//", "#")
line = line.replace(";", "")
line = line.replace("var ", "")
line = line.replace("true", "True")
line = line.replace("false", "False")
line = line.replace("//", "#").replace(";", "").replace("var ", "")
line = line.replace("true", "True").replace("false", "False")
line = line.replace("null", "{}")
# line = line.replace("or", "Or")
# line = line.replace("and", 'And')
line = dict_key_str(line)
line = line.rstrip()
line = dict_key_str(line).rstrip()

if "= function" in line:
line = line.replace(" = function", "")
line = line.replace("{", "")
line = line.replace(" = function", "").replace("{", "")
line = "def " + line.rstrip() + ":"
if line.strip() == "}":
line = line.replace("}", "")
line = ""

# print(line)
if line.lstrip().startswith("."):
output = output.rstrip() + " " + "\\" + "\n" + line + "\n"
else:
output = output + line + "\n"
output += line + "\n"

print(output)

Expand All @@ -72,4 +66,4 @@ def js_to_python(in_file):
# parser.add_argument('--image_path', type=str, help="Path to the input GeoTIFF image")
# parser.add_argument('--save_path', type=str, help="Path where the output map will be saved")
args = parser.parse_args()
js_to_python(args.input)
js_to_python(args.input)

0 comments on commit 619f1a8

Please sign in to comment.