aboutsummaryrefslogtreecommitdiff
path: root/src/test_export.py
diff options
context:
space:
mode:
authorgdamms <damguillotin@gmail.com>2026-07-24 12:44:20 +0200
committergdamms <damguillotin@gmail.com>2026-07-24 12:44:20 +0200
commit77aed43a99a295f3202063edefcb7ce01e011958 (patch)
treee34a042a75cdb52e2d30afc11dc82660c22f36d5 /src/test_export.py
downloaddog-friendly-data-77aed43a99a295f3202063edefcb7ce01e011958.tar.gz
dog-friendly-data-77aed43a99a295f3202063edefcb7ce01e011958.zip
first commit
Diffstat (limited to 'src/test_export.py')
-rw-r--r--src/test_export.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/test_export.py b/src/test_export.py
new file mode 100644
index 0000000..8f5ff38
--- /dev/null
+++ b/src/test_export.py
@@ -0,0 +1,100 @@
+import re
+import pathlib
+
+
+def check_duplicate_names(kml_file: str, quiet: bool = False) -> dict:
+ """Check for duplicate names in the exported KML file.
+
+ Args:
+ kml_file (str): Path to the KML file exported from Google My Maps.
+ quiet (bool, optional): If True, suppress output. Defaults to False.
+ Returns:
+ dict: A dictionary with names as keys and their counts as values.
+ """
+ if not quiet:
+ print("\nChecking for duplicate names in the exported KML file...")
+
+ with open(kml_file, "r", encoding="utf-8") as f:
+ content = f.read()
+
+ regex = r"<name>(<!\[CDATA\[)?(.*?)(\]\]>)?</name>"
+ matches = re.findall(regex, content)
+
+ names = {}
+ for match in matches:
+ name = match[1]
+ if name in names:
+ names[name] += 1
+ else:
+ names[name] = 1
+
+ if not quiet:
+ if any(count > 1 for count in names.values()):
+ print("Duplicate names found in the exported KML file:")
+ for name, count in names.items():
+ if count > 1:
+ print(f" - {name}: {count} times")
+ else:
+ print("No duplicate names found in the exported KML file.")
+
+ return names
+
+
+def check_missing_names(kml_file: str, kml_glob: str, quiet: bool = False) -> dict:
+ """Check for missing names in the exported KML file compared to other KML files.
+
+ Args:
+ kml_file (str): Path to the KML file exported from Google My Maps.
+ kml_glob (str): Glob pattern to match KML files to check against the exported KML file.
+ quiet (bool, optional): If True, suppress output. Defaults to False.
+ Returns:
+ { 'file_name': { 'missing': [list of missing names], 'total': total number of names in the file } }
+ """
+ if not quiet:
+ print("\nChecking for missing names in the exported KML file compared to other KML files...")
+
+ names = check_duplicate_names(kml_file, quiet=True)
+
+ missing_files = {}
+
+ for file in pathlib.Path().glob(kml_glob):
+ with open(file, "r", encoding="utf-8") as f:
+ content = f.read()
+
+ regex = r"<name>(<!\[CDATA\[)?(.*?)(\]\]>)?</name>"
+ matches = re.findall(regex, content)
+
+ current_names = set(match[1] for match in matches)
+
+ for name in current_names:
+ if not name in names:
+ if file.name not in missing_files:
+ missing_files[file.name] = {'missing': [], 'total': len(
+ current_names)-1} # because of the name of the document
+ missing_files[file.name]['missing'].append(name)
+
+ if not quiet:
+ if not missing_files:
+ print("All names in the KML files matching the glob pattern were found in the exported KML file.")
+ else:
+ print("Files with names not found in the exported KML file:")
+ for file, count in missing_files.items():
+ print(f" - {file}: missing {len(count['missing'])}/{count['total']}")
+ for name in count['missing']:
+ print(f" - {name}")
+
+ return missing_files
+
+
+if __name__ == "__main__":
+ import argparse
+
+ parser = argparse.ArgumentParser(
+ description="Check for duplicate or missing names in the KML file exported from Google My Maps.")
+ parser.add_argument("kml_file", type=str, help="Path to the KML file exported from Google My Maps.")
+ parser.add_argument("kml_glob", type=str,
+ help="Glob pattern to match KML files to check against the exported KML file.")
+ args = parser.parse_args()
+
+ check_duplicate_names(args.kml_file)
+ check_missing_names(args.kml_file, args.kml_glob)