From 9e198a1d32cb84a59c9ecf82517ff65c9384a322 Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 17 Jan 2014 20:00:56 +0100 Subject: [PATCH] dvr-mangedb: Add -m command --- dvr-managedb | 89 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/dvr-managedb b/dvr-managedb index 001ce39..7db2daa 100755 --- a/dvr-managedb +++ b/dvr-managedb @@ -237,7 +237,10 @@ class Database: c = self.conn.cursor() c.execute("SELECT * FROM geolocations WHERE dvrid=? AND provider=?", \ (dvrid, provider)) - return c.fetchone() + res = c.fetchone() + if res is None: + return None + return res[2:4] def add_geolocation(self, dvrid, provider, lat, lon): """Add a geolocatoin for a given DVR-ID.""" @@ -292,8 +295,71 @@ def printDataset(db, id): return True +def processMap(db, query, providers): + """Print a map webpage to stdout of the datasets selected by query, using + the given geocoding providers.""" + + print(""" + + + + + + + + +
+ + + """) + + def processGeolocation(db, query, provider): """Fetch and add geolocations for IDs selected by query.""" + provider = providers.getIndexByName(provider) ids = db.query(query) for id in ids: @@ -432,6 +498,7 @@ USAGE: {0} -d FILE -a PATH [PATH...] {0} -d FILE -q COND -l {0} -d FILE -q COND -g PROVIDER [PROVIDER...] + {0} -d FILE -q COND -m PROVIDER [PROVIDER...] {0} -h COMMAND: @@ -443,6 +510,10 @@ COMMAND: -l List the selected datasets. -g Fetch and store geolocation of selected datasets from given provider, if not existent. Provider is either 'google' or 'bing'. + -m Write a map website of selected datasets to stdout using the + coordinates from the given provider. If multiple providers are + given, take the coordinates from the first provider for which + coordinates are known. OPTIONS: -d FILE Use given sqlite3 database. @@ -457,10 +528,10 @@ if __name__ == "__main__": cmd = None try: - opts, args = getopt.getopt(sys.argv[1:], "ad:ghq:l") + opts, args = getopt.getopt(sys.argv[1:], "ad:ghq:lm") for opt, arg in opts: - if opt in ["-a", "-l", "-g"]: + if opt in ["-a", "-l", "-g", "-m"]: cmd = opt elif opt == "-d": dbfn = arg @@ -492,7 +563,7 @@ if __name__ == "__main__": for arg in args: processAdd(db, arg) - if cmd in ["-l", "-g"]: + if cmd in ["-l", "-g", "-m"]: if query is None: print("No query option given.", file=sys.stderr) sys.exit(os.EX_USAGE) @@ -503,11 +574,17 @@ if __name__ == "__main__": if cmd == "-g": providers = GeolocationProviders for arg in args: - if not providers.isNameValid(arg): print("Unknown provider '%s'." % arg, file=sys.stderr) sys.exit(os.EX_USAGE) + processGeolocation(db, query, arg) - processGeolocation(db, query, providers.getIndexByName(arg)) + if cmd == "-m": + providers = GeolocationProviders + for arg in args: + if not providers.isNameValid(arg): + print("Unknown provider '%s'." % arg, file=sys.stderr) + sys.exit(os.EX_USAGE) + processMap(db, query, args) sys.exit(os.EX_OK) -- 2.39.5