Skip to content

Commit 182fc0a

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent cf711d9 commit 182fc0a

File tree

1 file changed

+113
-69
lines changed

1 file changed

+113
-69
lines changed

geemap/conversion.py

Lines changed: 113 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from .common import *
2525

26+
2627
def find_matching_bracket(lines, start_line_index, start_char_index, matching_char="{"):
2728
"""Finds the position of the matching closing bracket from a list of lines.
2829
@@ -216,6 +217,7 @@ def convert_for_loop(line):
216217

217218
return new_line
218219

220+
219221
# Removes all indentation from file to reformat indentation for python later
220222
def remove_all_indentation(input_lines):
221223
"""Removes all indentation for reformatting according to python's indentation rules
@@ -228,7 +230,7 @@ def remove_all_indentation(input_lines):
228230
"""
229231
output_lines = []
230232
for index, line in enumerate(input_lines):
231-
output_lines.append(line.lstrip())
233+
output_lines.append(line.lstrip())
232234
return output_lines
233235

234236

@@ -244,14 +246,21 @@ def check_map_functions(input_lines):
244246
output_lines = []
245247
currentNumOfNestedFuncs = 0
246248
for index, line in enumerate(input_lines):
247-
if (line.strip().endswith(".map(") and input_lines[index+1].strip().replace(" ", "").startswith("function(")) or \
248-
(line.strip().endswith(".map(function(") and input_lines[index+1].strip().replace(" ", "").endswith("{")):
249-
input_lines[index+1] = line + input_lines[index+1]
249+
if (
250+
line.strip().endswith(".map(")
251+
and input_lines[index + 1].strip().replace(" ", "").startswith("function(")
252+
) or (
253+
line.strip().endswith(".map(function(")
254+
and input_lines[index + 1].strip().replace(" ", "").endswith("{")
255+
):
256+
input_lines[index + 1] = line + input_lines[index + 1]
250257
continue
251258

252-
if ".map(function" in line.replace(" ", "") or \
253-
"returnfunction" in line.replace(" ", "") or \
254-
"function(" in line.replace(" ", ""):
259+
if (
260+
".map(function" in line.replace(" ", "")
261+
or "returnfunction" in line.replace(" ", "")
262+
or "function(" in line.replace(" ", "")
263+
):
255264
try:
256265
bracket_index = line.index("{")
257266
matching_line_index, matching_char_index = find_matching_bracket(
@@ -272,13 +281,11 @@ def check_map_functions(input_lines):
272281

273282
new_lines = check_map_functions(new_lines)
274283

275-
for sub_index, tmp_line in enumerate(
276-
new_lines
277-
):
278-
output_lines.append((' ' * currentNumOfNestedFuncs) + tmp_line)
279-
if '{' in tmp_line:
284+
for sub_index, tmp_line in enumerate(new_lines):
285+
output_lines.append((" " * currentNumOfNestedFuncs) + tmp_line)
286+
if "{" in tmp_line:
280287
currentNumOfNestedFuncs += 1
281-
if '}' in tmp_line:
288+
if "}" in tmp_line:
282289
currentNumOfNestedFuncs -= 1
283290
input_lines[index + 1 + sub_index] = ""
284291

@@ -287,7 +294,9 @@ def check_map_functions(input_lines):
287294
header_line = line[:func_start_index] + func_name
288295
header_line = header_line.rstrip()
289296

290-
func_footer = input_lines[matching_line_index][: matching_char_index + 1]
297+
func_footer = input_lines[matching_line_index][
298+
: matching_char_index + 1
299+
]
291300
output_lines.append(func_footer)
292301

293302
footer_line = input_lines[matching_line_index][
@@ -300,7 +309,7 @@ def check_map_functions(input_lines):
300309
input_lines[matching_line_index] = footer_line
301310

302311
output_lines.append(header_line)
303-
#output_lines.append(footer_line)
312+
# output_lines.append(footer_line)
304313
except Exception as e:
305314
print(
306315
f"An error occurred: {e}. The closing curly bracket could not be found in Line {index+1}: {line}. Please reformat the function definition and make sure that both the opening and closing curly brackets appear on the same line as the function keyword. "
@@ -423,25 +432,28 @@ def js_to_python(
423432
matching_char_index,
424433
) = find_matching_bracket(lines, index, bracket_index)
425434

426-
if 'func_' not in line:
435+
if "func_" not in line:
427436
currentNumOfNestedFuncs += 1
428-
437+
429438
for sub_index, tmp_line in enumerate(
430439
lines[index + 1 : matching_line_index]
431440
):
432-
#lines[sub_index] = (' ' * currentNumOfNestedFuncs) + tmp_line
433-
if '{' in tmp_line and 'function' not in line:
441+
# lines[sub_index] = (' ' * currentNumOfNestedFuncs) + tmp_line
442+
if "{" in tmp_line and "function" not in line:
434443
currentNumOfNestedFuncs += 1
435-
if '}' in tmp_line and 'function' not in line:
444+
if "}" in tmp_line and "function" not in line:
436445
currentNumOfNestedFuncs -= 1
437-
lines[index + 1 + sub_index] = (' ' * currentNumOfNestedFuncs) + lines[index + 1 + sub_index]
446+
lines[index + 1 + sub_index] = (
447+
" " * currentNumOfNestedFuncs
448+
) + lines[index + 1 + sub_index]
438449

439450
currentNumOfNestedFuncs -= 1
440451

441452
line = line[:bracket_index] + line[bracket_index + 1 :]
442453
if matching_line_index == index:
443454
line = (
444-
line[:matching_char_index] + line[matching_char_index + 1 :]
455+
line[:matching_char_index]
456+
+ line[matching_char_index + 1 :]
445457
)
446458
else:
447459
tmp_line = lines[matching_line_index]
@@ -476,7 +488,7 @@ def js_to_python(
476488
+ line.strip()
477489
+ ":"
478490
)
479-
elif "{" in line and '({' not in line:
491+
elif "{" in line and "({" not in line:
480492
bracket_index = line.index("{")
481493
(
482494
matching_line_index,
@@ -488,24 +500,30 @@ def js_to_python(
488500
for sub_index, tmp_line in enumerate(
489501
lines[index + 1 : matching_line_index]
490502
):
491-
lines[index + 1 + sub_index] = (' ' * currentNumOfNestedFuncs) + lines[index + 1 + sub_index]
492-
if '{' in tmp_line and 'if' not in line and 'for' not in line:
503+
lines[index + 1 + sub_index] = (
504+
" " * currentNumOfNestedFuncs
505+
) + lines[index + 1 + sub_index]
506+
if "{" in tmp_line and "if" not in line and "for" not in line:
493507
currentNumOfNestedFuncs += 1
494-
if '}' in tmp_line and 'if' not in line and 'for' not in line:
508+
if "}" in tmp_line and "if" not in line and "for" not in line:
495509
currentNumOfNestedFuncs -= 1
496510

497511
currentNumOfNestedFuncs -= 1
498512

499513
if (matching_line_index == index) and (":" in line):
500514
pass
501-
elif ("for (" in line) or ("for(" in line) or \
502-
('if (' in line) or ('if(' in line):
503-
if 'if' not in line:
515+
elif (
516+
("for (" in line)
517+
or ("for(" in line)
518+
or ("if (" in line)
519+
or ("if(" in line)
520+
):
521+
if "if" not in line:
504522
line = convert_for_loop(line)
505523
else:
506524
start_index = line.index("(")
507525
end_index = line.index(")")
508-
line = 'if '+line[start_index:end_index]+'):{'
526+
line = "if " + line[start_index:end_index] + "):{"
509527
lines[index] = line
510528
bracket_index = line.index("{")
511529
(
@@ -539,86 +557,102 @@ def js_to_python(
539557
line = line.replace("= new", "=")
540558
line = line.replace("exports.", "")
541559
line = line.replace("Map.", f"{Map}.")
542-
line = line.replace("Export.table.toDrive", "geemap.ee_export_vector_to_drive")
543-
line = line.replace("Export.table.toAsset", "geemap.ee_export_vector_to_asset")
544-
line = line.replace("Export.image.toAsset", "geemap.ee_export_image_to_asset")
545-
line = line.replace("Export.video.toDrive", "geemap.ee_export_video_to_drive")
560+
line = line.replace(
561+
"Export.table.toDrive", "geemap.ee_export_vector_to_drive"
562+
)
563+
line = line.replace(
564+
"Export.table.toAsset", "geemap.ee_export_vector_to_asset"
565+
)
566+
line = line.replace(
567+
"Export.image.toAsset", "geemap.ee_export_image_to_asset"
568+
)
569+
line = line.replace(
570+
"Export.video.toDrive", "geemap.ee_export_video_to_drive"
571+
)
546572
line = line.replace("||", "or")
547-
line = line.replace('\****', '#')
548-
line = line.replace('def =', '_def =')
549-
line = line.replace(', def, ', ', _def, ')
550-
line = line.replace('(def, ', '(_def, ')
551-
line = line.replace(', def)', ', _def)')
552-
line = line.replace('===', '==')
553-
573+
line = line.replace("\****", "#")
574+
line = line.replace("def =", "_def =")
575+
line = line.replace(", def, ", ", _def, ")
576+
line = line.replace("(def, ", "(_def, ")
577+
line = line.replace(", def)", ", _def)")
578+
line = line.replace("===", "==")
579+
554580
# Replaces all javascript operators with python operators
555-
if '!' in line:
581+
if "!" in line:
556582
try:
557-
if (line.replace(" ", ""))[line.find('!') + 1] != '=':
583+
if (line.replace(" ", ""))[line.find("!") + 1] != "=":
558584
line = line.replace("!", "not ")
559585
except:
560586
print("continue...")
561587

562588
line = line.rstrip()
563589

564590
# If the function concat is used, replace it with python's concatenation
565-
if 'concat' in line:
566-
line = line.replace('.concat(', '+')
567-
line = line.replace(',', '+')
568-
line = line.replace(')', '')
591+
if "concat" in line:
592+
line = line.replace(".concat(", "+")
593+
line = line.replace(",", "+")
594+
line = line.replace(")", "")
569595

570596
# Checks if an equal sign is at the end of a line. If so, add backslashes
571597
if shouldCheckForEmptyLines:
572-
if line.strip() == '' or '#' in line:
573-
if line.strip().endswith('['):
574-
line = '['
598+
if line.strip() == "" or "#" in line:
599+
if line.strip().endswith("["):
600+
line = "["
575601
shouldCheckForEmptyLines = False
576602
else:
577-
line = '\\'
603+
line = "\\"
578604
else:
579605
shouldCheckForEmptyLines = False
580-
581-
if line.strip().endswith('='):
582-
line = line + ' \\'
606+
607+
if line.strip().endswith("="):
608+
line = line + " \\"
583609
shouldCheckForEmptyLines = True
584610

585611
# Adds getInfo at the end of print statements involving maps
586612
endOfPrintReplaced = False
587-
588-
if ('print(' in line and '=' not in line) or checkNextLineForPrint:
613+
614+
if ("print(" in line and "=" not in line) or checkNextLineForPrint:
589615
for i in range(len(line) - 1):
590-
if line[len(line) - i - 1] == ')':
591-
line = line[:len(line) - i - 1] + '.getInfo())'
616+
if line[len(line) - i - 1] == ")":
617+
line = line[: len(line) - i - 1] + ".getInfo())"
592618
print(line)
593619
endOfPrintReplaced = True
594620
break
595621
if endOfPrintReplaced:
596622
checkNextLineForPrint = False
597623
else:
598624
checkNextLineForPrint = True
599-
625+
600626
# Removes potential commas after imports. Causes tuple type errors
601627
if line.endswith(","):
602-
if '=' in lines[index + 1] and not lines[index + 1].strip().startswith("'"):
628+
if "=" in lines[index + 1] and not lines[
629+
index + 1
630+
].strip().startswith("'"):
603631
line = line[:-1]
604632

605633
# Changes object argument to individual parameters
606-
if line.strip().endswith("({") and not 'ee.Dictionary' in line and not '.set(' in line and '.addLayer' not in line and 'cast' not in line:
634+
if (
635+
line.strip().endswith("({")
636+
and not "ee.Dictionary" in line
637+
and not ".set(" in line
638+
and ".addLayer" not in line
639+
and "cast" not in line
640+
):
607641
line = line.rstrip()[:-1]
608642
numIncorrectParameters = numIncorrectParameters + 1
609643

610644
if numIncorrectParameters > 0:
611645
if line.strip().startswith("})"):
612-
line = line.replace('})', ')')
646+
line = line.replace("})", ")")
613647
numIncorrectParameters = numIncorrectParameters - 1
614648
else:
615649
if currentDictionaryScopeDepth < 1:
616650
line = line.replace(":", " =")
617651

618-
if '= {' in line and '({' not in line:
652+
if "= {" in line and "({" not in line:
619653
currentDictionaryScopeDepth += 1
620654

621-
if '}' in line and currentDictionaryScopeDepth > 0:
655+
if "}" in line and currentDictionaryScopeDepth > 0:
622656
currentDictionaryScopeDepth -= 1
623657

624658
# if ".style(" in line and ".style(**" not in line:
@@ -648,19 +682,24 @@ def js_to_python(
648682
):
649683
line = ""
650684

651-
if "#" in line and not line.strip().startswith("#") and not line[line.index("#") - 1] == "'":
685+
if (
686+
"#" in line
687+
and not line.strip().startswith("#")
688+
and not line[line.index("#") - 1] == "'"
689+
):
652690
line = line[: line.index("#")]
653691

654692
if line.lstrip().startswith("."):
655-
if lines[index - 1].strip().endswith('\\') and lines[index - 1].strip().startswith('#'):
656-
lines[index - 1] = '\\'
693+
if lines[index - 1].strip().endswith("\\") and lines[
694+
index - 1
695+
].strip().startswith("#"):
696+
lines[index - 1] = "\\"
657697
if "#" in line:
658698
line = line[: line.index("#")]
659699
output = output.rstrip() + " " + "\\" + "\n" + line + "\n"
660700
else:
661701
output += line + "\n"
662702

663-
664703
if show_map:
665704
output += "Map"
666705

@@ -762,7 +801,12 @@ def js_snippet_to_py(
762801

763802

764803
def js_to_python_dir(
765-
in_dir, out_dir=None, use_qgis=True, github_repo=None, import_geemap=False, Map="Map"
804+
in_dir,
805+
out_dir=None,
806+
use_qgis=True,
807+
github_repo=None,
808+
import_geemap=False,
809+
Map="Map",
766810
):
767811
"""Converts all Earth Engine JavaScripts in a folder recursively to Python scripts.
768812

0 commit comments

Comments
 (0)