Skip to content

Commit

Permalink
Get a CLI for each subcommand so asciidoc diagrams is easier
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismiller committed Oct 30, 2024
1 parent 5e6041d commit 7a58f1e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
42 changes: 2 additions & 40 deletions dbdiag/__main__.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
import sys
import argparse
from . import constants
from . import spans
from . import cli

def parse_args(argv):
parser = argparse.ArgumentParser()
parser.add_argument('file', help='file of operations')
parser.add_argument('-o', '--output', help='output file path')
parser.add_argument('--debug', action='store_true', help='print out each intermediate step')
parser.add_argument('--guidelines', action='store_true', help='add extra lines to debug alignment issues')
parser.add_argument('--embed', action='store_true', help='only use 12px font and px units')
return parser.parse_args()

def main(argv):
args = parse_args(argv)

if args.debug:
constants.DEBUG = True
if args.guidelines:
constants.GUIDELINES = True
if args.embed:
constants.EMBED = True

with open(args.file) as f:
text_input = f.read()

svg = spans.to_span_svg(text_input)

if args.output is None or args.output == '-':
sys.stdout.write(svg)
elif args.output.endswith('.svg'):
with open(args.output, 'w') as f:
f.write(svg)
elif args.output.endswith('.png'):
# A yet-to-be-released version of cairosvg is required to correctly
# render the SVGs produced, so make it a runtime requirement.
from cairosvg import svg2png
svg2png(bytestring=svg, write_to=args.output)

main(sys.argv)
cli.main()
53 changes: 53 additions & 0 deletions dbdiag/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sys
import argparse
from . import constants
from . import spans

def parse_spans_args(parser=None):
parser = parser or argparse.ArgumentParser()
parser.add_argument('file', help='file of operations')
parser.add_argument('-o', '--output', help='output file path')
parser.add_argument('--debug', action='store_true', help='print out each intermediate step')
parser.add_argument('--guidelines', action='store_true', help='add extra lines to debug alignment issues')
parser.add_argument('--embed', action='store_true', help='only use 12px font and px units')
return parser

def main_spans(args = None):
args = args or parse_spans_args().parse_args()

if args.debug:
constants.DEBUG = True
if args.guidelines:
constants.GUIDELINES = True
if args.embed:
constants.EMBED = True

with open(args.file) as f:
text_input = f.read()

svg = spans.to_span_svg(text_input)

if args.output is None or args.output == '-':
sys.stdout.write(svg)
elif args.output.endswith('.svg'):
with open(args.output, 'w') as f:
f.write(svg)
elif args.output.endswith('.png'):
# A yet-to-be-released version of cairosvg is required to correctly
# render the SVGs produced, so make it a runtime requirement.
from cairosvg import svg2png
svg2png(bytestring=svg, write_to=args.output)

def parse_main_args(parser=None):
parser = parser or argparse.ArgumentParser()
subparsers = parser.add_subparsers(required=True)

spans_parser = subparsers.add_parser('spans')
spans_parser.set_defaults(main_func=main_spans)
parse_spans_args(spans_parser)

return parser

def main():
args = parse_main_args().parse_args()
args.main_func(args)
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ name = "pypi-public"
url = "https://pypi.org/simple/"

[tool.poetry.scripts]
dbdiag = "src.ophistory:main"
dbdiag = "dbdiag.cli:main"
dbdiag-spans = "dbdiag.cli:main_spans"

[tool.coverage.paths]
source = ["src", "*/site-packages"]
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Test suite for the dbdiag package."""

0 comments on commit 7a58f1e

Please sign in to comment.