forked from nocproject/noc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstencil.py
48 lines (38 loc) · 1.51 KB
/
stencil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# ----------------------------------------------------------------------
# ./noc stencil
# ----------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
import sys
from collections import defaultdict
# Third-party modules
from jinja2 import Template
# NOC modules
from noc.core.management.base import BaseCommand
from noc.core.stencil import stencil_registry
class Command(BaseCommand):
INDEX_TEMPLATE_PATH = "templates/stencil_index.html.j2"
def add_arguments(self, parser):
subparsers = parser.add_subparsers(dest="cmd")
#
html_parser = subparsers.add_parser("htmlindex")
html_parser.add_argument("-o", "--out", help="Output file")
def handle(self, cmd, *args, **options):
return getattr(self, "handle_%s" % cmd)(*args, **options)
def handle_htmlindex(self, out=None, *args, **options):
stencils = defaultdict(list)
for stencil_id in sorted(stencil_registry.stencils):
coll, name = stencil_id.split("/", 1)
stencils[coll] += [stencil_registry.get(stencil_id)]
with open(self.INDEX_TEMPLATE_PATH) as f:
tpl = Template(f.read())
r = tpl.render({"stencils": stencils})
if out:
with open(out, "w") as f:
f.write(r)
else:
sys.stdout.write(r)
if __name__ == "__main__":
Command().run()