]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/python/location-query.in
location-query: Use print to print errors, too
[people/ms/libloc.git] / src / python / location-query.in
index 933024eb1c030adf822238f60571d4bf7c8d7f67..1433f1aacd1a559bbcbc643da4cad6dc739b6fd8 100644 (file)
@@ -65,6 +65,13 @@ class CLI(object):
                get_as.add_argument("asn", nargs="+")
                get_as.set_defaults(func=self.handle_get_as)
 
+               # Search for AS
+               search_as = subparsers.add_parser("search-as",
+                       help=_("Search for Autonomous Systems that match the string"),
+               )
+               search_as.add_argument("query", nargs=1)
+               search_as.set_defaults(func=self.handle_search_as)
+
                return parser.parse_args()
 
        def run(self):
@@ -91,7 +98,7 @@ class CLI(object):
                        try:
                                n = self.db.lookup(address)
                        except ValueError:
-                               sys.stderr.write(_("Invalid IP address: %s") % address)
+                               print(_("Invalid IP address: %s") % address, file=sys.stderr)
 
                        args = {
                                "address" : address,
@@ -100,7 +107,7 @@ class CLI(object):
 
                        # Nothing found?
                        if not n:
-                               print(_("Nothing found for %(address)s") % args)
+                               print(_("Nothing found for %(address)s") % args, file=sys.stderr)
                                ret = 1
                                continue
 
@@ -131,7 +138,7 @@ class CLI(object):
                        try:
                                asn = int(asn)
                        except ValueError:
-                               sys.stderr.write("Invalid ASN: %s" %asn)
+                               print(_("Invalid ASN: %s") % asn, file=sys.stderr)
                                ret = 1
                                continue
 
@@ -140,7 +147,7 @@ class CLI(object):
 
                        # Nothing found
                        if not a:
-                               print(_("Could not find AS%s") % asn)
+                               print(_("Could not find AS%s") % asn, file=sys.stderr)
                                ret = 1
                                continue
 
@@ -148,6 +155,12 @@ class CLI(object):
 
                return ret
 
+       def handle_search_as(self, ns):
+               for query in ns.query:
+                       # Print all matches ASes
+                       for a in self.db.search_as(query):
+                               print(a)
+
 def main():
        # Run the command line interface
        c = CLI()