]> www.average.org Git - loctrkd.git/commitdiff
googlemaps: add code for manual testing
authorEugene Crosser <crosser@average.org>
Thu, 4 Aug 2022 14:44:43 +0000 (16:44 +0200)
committerEugene Crosser <crosser@average.org>
Thu, 4 Aug 2022 14:44:43 +0000 (16:44 +0200)
loctrkd/googlemaps.py
loctrkd/opencellid.py

index 5aace471a21c1be18827a7a80a6ef8fa10390f3c..6838324bd1c95062df2b4db4a62eb532bc41c8f4 100644 (file)
@@ -1,10 +1,15 @@
+"""
+Google Maps location service lookaside backend
+"""
+
+from configparser import ConfigParser
 import googlemaps as gmaps
-from typing import Any, Dict, List, Tuple
+from typing import Any, Callable, Dict, List, Tuple
 
 gclient = None
 
 
-def init(conf: Dict[str, Any]) -> None:
+def init(conf: ConfigParser) -> None:
     global gclient
     with open(conf["googlemaps"]["accesstoken"], encoding="ascii") as fl:
         token = fl.read().rstrip()
@@ -15,12 +20,12 @@ def shut() -> None:
     return
 
 
-def lookup(
+def _lookup(
     mcc: int,
     mnc: int,
     gsm_cells: List[Tuple[int, int, int]],
     wifi_aps: List[Tuple[str, int]],
-) -> Tuple[float, float]:
+) -> Any:
     assert gclient is not None
     kwargs = {
         "home_mobile_country_code": mcc,
@@ -40,8 +45,47 @@ def lookup(
             {"macAddress": mac, "signalStrength": sig} for mac, sig in wifi_aps
         ],
     }
-    result = gclient.geolocate(**kwargs)
+    return gclient.geolocate(**kwargs)
+
+
+def lookup(
+    mcc: int,
+    mnc: int,
+    gsm_cells: List[Tuple[int, int, int]],
+    wifi_aps: List[Tuple[str, int]],
+) -> Tuple[float, float]:
+    result = _lookup(mcc, mnc, gsm_cells, wifi_aps)
     if "location" in result:
         return result["location"]["lat"], result["location"]["lng"]
     else:
         raise ValueError("google geolocation: " + str(result))
+
+
+if __name__.endswith("__main__"):
+    from getopt import getopt
+    from json import loads
+    from logging import getLogger
+    from sys import argv
+    from . import common
+
+    def cell_list(s: str) -> List[Tuple[int, int, int]]:
+        return [(int(ac), int(ci), int(sg)) for [ac, ci, sg] in loads(s)]
+
+    def ap_list(s: str) -> List[Tuple[str, int]]:
+        return [(mac, int(sg)) for [mac, sg] in loads(s)]
+
+    log = getLogger("loctrkd/googlemaps")
+    opts, args = getopt(argv[1:], "c:d")
+    conf = common.init(log, opts=opts)
+    init(conf)
+    parms = {}
+    needed: Dict[str, Callable[[Any], Any]] = {
+        "mcc": int,
+        "mnc": int,
+        "gsm_cells": cell_list,
+        "wifi_aps": ap_list,
+    }
+    parms = {k: needed.pop(k)(v) for k, v in [arg.split("=") for arg in args]}
+    if needed:
+        raise ValueError(f"still needed: {needed}")
+    print(_lookup(**parms))
index 20fd3ae1a2fc91723a2c5c211e5ad9971307a3c9..ff193fd27a5f511787c9b3b671a22ce217f06a58 100644 (file)
@@ -2,6 +2,7 @@
 Lookaside backend to query local opencellid database
 """
 
+from configparser import ConfigParser
 from sqlite3 import connect
 from typing import Any, Dict, List, Tuple
 
@@ -10,7 +11,7 @@ __all__ = "init", "lookup"
 ldb = None
 
 
-def init(conf: Dict[str, Any]) -> None:
+def init(conf: ConfigParser) -> None:
     global ldb
     ldb = connect(conf["opencellid"]["dbfn"])