Skip to content

Commit

Permalink
Add ability to override template in configuration, add json output
Browse files Browse the repository at this point in the history
- Template key can be overridden by `template_override=key` in config section
- Added --json to output json.  Structured like
```
[
  {"arg": "argument", "data": {"pluginname": plugindata}}
]

eg:

[{"arg": "[email protected]", "data": {"uidresolve": {"username": "email"}}}]
```
  • Loading branch information
Goggin committed May 4, 2023
1 parent b4134f1 commit 6092c7e
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions ninfo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
__version__ = "0.9.0"

import memcache

import logging

logger = logging.getLogger("ninfo")
Expand All @@ -15,6 +14,8 @@
except ImportError:
import configparser as ConfigParser

import json

from mako.template import Template

try:
Expand Down Expand Up @@ -43,7 +44,6 @@ class PluginInitError(PluginError):


class PluginBase(object):

cache_timeout = 60 * 60
local = True
remote = True
Expand All @@ -61,6 +61,11 @@ def __init__(self, config=None, plugin_config=None):
self.plugin_config = plugin_config
self.initialized = False
self._name = self.name
self._template = self.name

if "template_override" in plugin_config:
self._template = plugin_config["template_override"]

if "disabled" in plugin_config:
return

Expand Down Expand Up @@ -136,7 +141,7 @@ def render_template(self, output_type, arg, result):
def get_template(self, output_type):
code = getsourcefile(self.__class__)
path = os.path.dirname(code)
filename = "%s_template_%s.mako" % (self._name, output_type)
filename = "%s_template_%s.mako" % (self._template, output_type)
template = os.path.join(path, filename)
if os.path.exists(template):
return template
Expand Down Expand Up @@ -352,9 +357,9 @@ def get_info_iter(self, arg, plugins=None, options={}):
except PluginError:
logger.exception("Error running plugin %s", p.name)

def get_info_dict(self, arg):
def get_info_dict(self, arg, plugins=None, options={}):
res = {}
for p, result in self.get_info_iter(arg):
for p, result in self.get_info_iter(arg, plugins, options):
res[p.name] = result
return res

Expand Down Expand Up @@ -383,7 +388,22 @@ def main():
from optparse import OptionParser

parser = OptionParser(usage="usage: %prog [options] [addresses]")
parser.add_option("-p", "--plugin", dest="plugins", action="append", default=None)
parser.add_option(
"-p",
"--plugin",
dest="plugins",
action="append",
help="The plugin to run",
default=None,
)
parser.add_option(
"-j",
"--json",
dest="json",
action="store_true",
help="Output json instead of rendering",
default=False,
)
parser.add_option("-l", "--list", dest="list", action="store_true", default=False)
(options, complete_args) = parser.parse_args()

Expand All @@ -404,10 +424,17 @@ def main():
args.append(arg)

plugins = options.plugins or None
json_output = []
for arg in args:
if len(args) != 1:
print("=== %s === " % (arg,))
p.show_info(arg, plugins=plugins, options=context_options)
if options.json:
json_output.append({"arg": arg, "data": p.get_info_dict(arg, plugins=plugins, options=context_options)})
else:
p.show_info(arg, plugins=plugins, options=context_options)

if options.json:
print(json.dumps(json_output))


if __name__ == "__main__":
Expand Down

0 comments on commit 6092c7e

Please sign in to comment.