def handle_tokenizer(self, object=None, profile=None, config=None, *args, **kwargs): cfg = None if config: if not os.path.exists(config): self.die("File not found: %s" % config) with open(config) as f: cfg = f.read() if object: connect() mo = ManagedObject.get_by_id(object) if not mo: self.die("Managed Object not found") elif profile: p = loader.get_profile(profile) if not p: self.die("Invalid profile: %s" % profile) if not cfg: self.die("Specify config file with --config option") # Mock up tokenizer connect() mo = ManagedObject.mock_object(profile=profile) else: self.die("Eigther object or profile must be set") tokenizer = mo.iter_config_tokens(config=cfg) for token in tokenizer: self.print(token)
def handle_query(self, object=None, profile=None, config=None, query=None, *args, **kwargs): cfg = None if config: if not os.path.exists(config): self.die("File not found: %s" % config) with open(config) as f: cfg = f.read() if object: connect() from noc.sa.models.managedobject import ManagedObject mo = ManagedObject.objects.get(name=object) if not mo: self.die("Managed Object not found") elif profile: p = loader.get_profile(profile) if not p: self.die("Invalid profile: %s" % profile) if not cfg: self.die("Specify config file with --config option") # Mock up tokenizer connect() from noc.sa.models.managedobject import ManagedObject mo = ManagedObject.mock_object(profile=profile) else: self.die("Eigther object or profile must be set") confdb = mo.get_confdb() headers = [] table = [] width = [] for r in confdb.query(query): row = [] for key in r: if key not in headers: headers += [key] width += [40] row.insert(headers.count(key), r[key]) table += [row] if table: self.print("Result:\n", format_table(width, [headers] + table)) else: self.print("Result:")