Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 2 | # Copyright (c) 2016 Comcast Cable Communications Management, LLC. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at: |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # Filter for .siphon files that are generated by other filters. |
| 17 | # The idea is to siphon off certain initializers so that we can better |
| 18 | # auto-document the contents of that initializer. |
| 19 | |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 20 | import argparse |
| 21 | import logging |
| 22 | import os |
| 23 | import sys |
| 24 | |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 25 | import siphon |
| 26 | |
| 27 | DEFAULT_LOGFILE = None |
| 28 | DEFAULT_LOGLEVEL = "info" |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 29 | DEFAULT_SIPHON = "clicmd" |
Chris Luke | c3f92ad | 2016-10-05 15:45:19 -0400 | [diff] [blame] | 30 | DEFAULT_FORMAT = "markdown" |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 31 | DEFAULT_OUTPUT = None |
| 32 | DEFAULT_TEMPLATES = os.path.dirname(__file__) + "/siphon_templates" |
| 33 | |
| 34 | ap = argparse.ArgumentParser() |
| 35 | ap.add_argument("--log-file", default=DEFAULT_LOGFILE, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 36 | help="Log file [%s]" % DEFAULT_LOGFILE) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 37 | ap.add_argument("--log-level", default=DEFAULT_LOGLEVEL, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 38 | choices=["debug", "info", "warning", "error", "critical"], |
| 39 | help="Logging level [%s]" % DEFAULT_LOGLEVEL) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 40 | |
| 41 | ap.add_argument("--type", '-t', metavar="siphon_type", default=DEFAULT_SIPHON, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 42 | choices=siphon.process.siphons.keys(), |
| 43 | help="Siphon type to process [%s]" % DEFAULT_SIPHON) |
Chris Luke | c3f92ad | 2016-10-05 15:45:19 -0400 | [diff] [blame] | 44 | ap.add_argument("--format", '-f', default=DEFAULT_FORMAT, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 45 | choices=siphon.process.formats.keys(), |
| 46 | help="Output format to generate [%s]" % DEFAULT_FORMAT) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 47 | ap.add_argument("--output", '-o', metavar="file", default=DEFAULT_OUTPUT, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 48 | help="Output file (uses stdout if not defined) [%s]" % |
| 49 | DEFAULT_OUTPUT) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 50 | ap.add_argument("--templates", metavar="directory", default=DEFAULT_TEMPLATES, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 51 | help="Path to render templates directory [%s]" % |
| 52 | DEFAULT_TEMPLATES) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 53 | ap.add_argument("input", nargs='+', metavar="input_file", |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 54 | help="Input .siphon files") |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 55 | args = ap.parse_args() |
| 56 | |
| 57 | logging.basicConfig(filename=args.log_file, |
Paul Vinciguerra | 464e5e0 | 2019-11-01 15:07:32 -0400 | [diff] [blame] | 58 | level=getattr(logging, args.log_level.upper(), None)) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 59 | log = logging.getLogger("siphon_process") |
| 60 | |
| 61 | # Determine where to send the generated output |
| 62 | if args.output is None: |
| 63 | out = sys.stdout |
| 64 | else: |
| 65 | out = open(args.output, "w+") |
| 66 | |
| 67 | # Get our processor |
| 68 | klass = siphon.process.siphons[args.type] |
Chris Luke | c3f92ad | 2016-10-05 15:45:19 -0400 | [diff] [blame] | 69 | processor = klass(template_directory=args.templates, format=args.format) |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 70 | |
| 71 | # Load the input files |
| 72 | processor.load_json(args.input) |
| 73 | |
| 74 | # Process the data |
| 75 | processor.process(out=out) |
| 76 | |
| 77 | # All done |