]> git.ipfire.org Git - location/libloc.git/blobdiff - src/python/location-query.in
location-query: Require at least one flag
[location/libloc.git] / src / python / location-query.in
index c138644cd1c35a8acd206233b53741716bea7056..dfdff8c2b804b08732d7734c310a1614cc35d3e9 100644 (file)
@@ -138,6 +138,11 @@ class CLI(object):
                        default="@databasedir@/signing-key.pem", help=_("Public Signing Key"),
                )
 
+               # Show the database version
+               version = subparsers.add_parser("version",
+                       help=_("Show database version"))
+               version.set_defaults(func=self.handle_version)
+
                # lookup an IP address
                lookup = subparsers.add_parser("lookup",
                        help=_("Lookup one or multiple IP addresses"),
@@ -241,7 +246,13 @@ class CLI(object):
                                args.family = 0
 
                # Call function
-               ret = args.func(db, args)
+               try:
+                       ret = args.func(db, args)
+
+               # Catch invalid inputs
+               except ValueError as e:
+                       sys.stderr.write("%s\n" % e)
+                       ret = 2
 
                # Return with exit code
                if ret:
@@ -250,6 +261,16 @@ class CLI(object):
                # Otherwise just exit
                sys.exit(0)
 
+       def handle_version(self, db, ns):
+               """
+                       Print the version of the database
+               """
+               t = time.strftime(
+                       "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(db.created_at),
+               )
+
+               print(t)
+
        def handle_lookup(self, db, ns):
                ret = 0
 
@@ -277,7 +298,12 @@ class CLI(object):
 
                        # Print country
                        if network.country_code:
-                               print(format % (_("Country"), network.country_code))
+                               country = db.get_country(network.country_code)
+
+                               print(format % (
+                                       _("Country"),
+                                       country.name if country else network.country_code),
+                               )
 
                        # Print AS information
                        if network.asn:
@@ -288,12 +314,33 @@ class CLI(object):
                                        autonomous_system or "AS%s" % network.asn),
                                )
 
+                       # Anonymous Proxy
+                       if network.has_flag(location.NETWORK_FLAG_ANONYMOUS_PROXY):
+                               print(format % (
+                                       _("Anonymous Proxy"), _("yes"),
+                               ))
+
+                       # Satellite Provider
+                       if network.has_flag(location.NETWORK_FLAG_SATELLITE_PROVIDER):
+                               print(format % (
+                                       _("Satellite Provider"), _("yes"),
+                               ))
+
+                       # Anycast
+                       if network.has_flag(location.NETWORK_FLAG_ANYCAST):
+                               print(format % (
+                                       _("Anycast"), _("yes"),
+                               ))
+
                return ret
 
        def handle_dump(self, db, ns):
                # Use output file or write to stdout
                f = ns.output or sys.stdout
 
+               # Format everything like this
+               format = "%-24s %s\n"
+
                # Write metadata
                f.write("#\n# Location Database Export\n#\n")
 
@@ -318,19 +365,30 @@ class CLI(object):
                # Iterate over all ASes
                for a in db.ases:
                        f.write("\n")
-                       f.write("aut-num:        AS%s\n" % a.number)
-                       f.write("name:           %s\n" % a.name)
+                       f.write(format % ("aut-num:", "AS%s" % a.number))
+                       f.write(format % ("name:", a.name))
+
+               flags = {
+                       location.NETWORK_FLAG_ANONYMOUS_PROXY    : "is-anonymous-proxy:",
+                       location.NETWORK_FLAG_SATELLITE_PROVIDER : "is-satellite-provider:",
+                       location.NETWORK_FLAG_ANYCAST            : "is-anycast:",
+               }
 
                # Iterate over all networks
                for n in db.networks:
                        f.write("\n")
-                       f.write("net:            %s\n" % n)
+                       f.write(format % ("net:", n))
 
                        if n.country_code:
-                               f.write("country:        %s\n" % n.country_code)
+                               f.write(format % ("country:", n.country_code))
 
                        if n.asn:
-                               f.write("autnum:         %s\n" % n.asn)
+                               f.write(format % ("aut-num:", n.asn))
+
+                       # Print all flags
+                       for flag in flags:
+                               if n.has_flag(flag):
+                                       f.write(format % (flags[flag], "yes"))
 
        def handle_get_as(self, db, ns):
                """
@@ -399,6 +457,9 @@ class CLI(object):
                if ns.anycast:
                        flags |= location.NETWORK_FLAG_ANYCAST
 
+               if not flags:
+                       raise ValueError(_("You must at least pass one flag"))
+
                with self.__get_output_formatter(ns) as f:
                        for n in db.search_networks(flags=flags, family=ns.family):
                                f.network(n)