WIP add environment variable support to man generator

This commit is contained in:
lash 2022-02-22 14:47:35 +00:00
parent 3691f75f81
commit 92fb016014
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 156 additions and 58 deletions

View File

@ -4,6 +4,18 @@ from .base import (
argflag_std_target, argflag_std_target,
) )
def apply_groff(collection, v, arg=None):
s = ''
for flag in collection:
if len(s) > 0:
s += ', '
s += '\\fB' + flag
if arg != None:
s += ' \\fI' + arg
s += '\\fP'
s = "\n.TP\n" + s + "\n" + v
return s
class DocEntry: class DocEntry:
@ -36,20 +48,10 @@ class DocEntry:
def get_groff(self): def get_groff(self):
s = ''
for flag in self.flags:
if len(s) > 0:
s += ', '
s += '\\fB' + flag
if self.v != None:
s += ' \\fI' + self.v
s += '\\fP'
v = self.groff v = self.groff
if v == None: if v == None:
v = self.plain v = self.plain
s = apply_groff(self.flags, v, arg=self.v)
s = "\n.TP\n" + s + "\n" + self.groff
return s return s
@ -59,9 +61,11 @@ class DocEntry:
class DocGenerator: class DocGenerator:
def __init__(self, arg_flags): def __init__(self, arg_flags, config):
self.config = config
self.arg_flags = arg_flags self.arg_flags = arg_flags
self.docs = {} self.docs = {}
self.envs = {}
def __str__(self): def __str__(self):
@ -73,7 +77,43 @@ class DocGenerator:
return s return s
def process(self): def get_args(self):
s = ''
ks = list(self.docs.keys())
ks.sort()
for k in ks:
s += str(self.docs[k]) + "\n"
return s
def get_args(self):
s = ''
ks = list(self.docs.keys())
ks.sort()
for k in ks:
s += str(self.docs[k]) + "\n"
return s
def override_arg(self, k, v, args):
o = self.docs[k]
#g.docs[v[0]].groff = v[1].rstrip()
o.set_groff(v)
l = len(args)
if l > 0:
o.flags = []
for i in range(l):
o.flags.append(args[i])
def process_env(self):
for k in self.config.all():
if k[0] == '_':
continue
self.envs[k] = None
def process_arg(self):
if self.arg_flags & Flag.VERBOSE: if self.arg_flags & Flag.VERBOSE:
o = DocEntry('--no-logs') o = DocEntry('--no-logs')
o.set_groff('Turn of logging completely. Negates \\fB-v\\fP and \\fB-vv\\fP') o.set_groff('Turn of logging completely. Negates \\fB-v\\fP and \\fB-vv\\fP')
@ -98,7 +138,7 @@ class DocGenerator:
self.docs['n'] = o self.docs['n'] = o
o = DocEntry('--dumpconfig', argvalue='format') o = DocEntry('--dumpconfig', argvalue='format')
o.set_groff('Load given configuration namespace. Configuration will be loaded from the immediate configuration subdirectory with the same name.') o.set_groff('Output configuration settings rendered from environment and inputs. Valid arguments are \\fIini\\fP for ini file output, and \\fIenv\\fP for environment variable output')
self.docs['dumpconfig'] = o self.docs['dumpconfig'] = o
@ -211,3 +251,8 @@ class DocGenerator:
o = DocEntry('-a', '--recipient-address') o = DocEntry('-a', '--recipient-address')
o.set_groff('Network wallet address to operate on. For read calls, this will be the wallet address for which the query is anchored. For transaction calls, it will be the wallet address for which state will be changed.') o.set_groff('Network wallet address to operate on. For read calls, this will be the wallet address for which the query is anchored. For transaction calls, it will be the wallet address for which state will be changed.')
self.docs['a'] = o self.docs['a'] = o
def process(self):
self.process_arg()
self.process_env()

95
scripts/chainlib-man.py Normal file
View File

@ -0,0 +1,95 @@
#!/usr/bin/python3
import logging
import os
import sys
import argparse
import tempfile
import shutil
from hexathon import strip_0x, add_0x
from chainlib.cli.man import (
DocGenerator,
apply_groff,
)
from chainlib.cli.base import argflag_std_base
from chainlib.cli.arg import ArgumentParser as ChainlibArgumentParser
from chainlib.eth.cli.config import Config
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
argparser = argparse.ArgumentParser()
argparser.add_argument('-b', default=add_0x(hex(argflag_std_base)), help='argument flag bitmask')
argparser.add_argument('-c', help='config override directory')
argparser.add_argument('-n', help='tool name to use for man filename')
argparser.add_argument('-d', default='.', help='output directory')
argparser.add_argument('-v', action='store_true', help='turn on debug logging')
argparser.add_argument('--overrides-file', dest='overrides_file', help='load options description override from file')
argparser.add_argument('--overrides-env-file', dest='overrides_env_file', help='load envionment description overrides from file')
argparser.add_argument('header_file', help='groff file containing heading, synopsis and description')
args = argparser.parse_args(sys.argv[1:])
if args.v:
logg.setLevel(logging.DEBUG)
b = bytes.fromhex(strip_0x(args.b))
flags = int.from_bytes(b, byteorder='little')
empty_args = ChainlibArgumentParser(flags).parse_args([])
config = Config.from_args(empty_args, arg_flags=flags)
g = DocGenerator(flags, config)
toolname = args.n
if toolname == None:
parts = os.path.splitext(os.path.basename(args.header_file))
toolname = parts[0]
g.process()
if args.overrides_file != None:
f = open(args.overrides_file, 'r')
while True:
s = f.readline()
if len(s) == 0:
break
v = s.split('\t', maxsplit=2)
fargs = None
try:
fargs = v[2].rstrip().split(',')
except IndexError:
fargs = []
g.override_arg(v[0], v[1], fargs)
f.close()
s_env = ''
if args.overrides_env_file != None:
f = open(args.overrides_env_file, 'r')
while True:
s = f.readline()
if len(s) == 0:
break
(k, description) = s.split('\t', maxsplit=1)
v = config.get(k)
s_env += apply_groff([k], description)
print(s_env)
f = open(args.header_file)
head = f.read()
f.close()
(fd, fp) = tempfile.mkstemp()
f = os.fdopen(fd, 'w')
f.write(head)
f.write(str(g))
f.close()
dest = os.path.join(args.d, toolname + '.1')
shutil.copyfile(fp, dest)
os.unlink(fp)

View File

@ -1,42 +0,0 @@
import os
import sys
import argparse
import tempfile
import shutil
from hexathon import strip_0x, add_0x
from chainlib.cli.man import DocGenerator
from chainlib.cli.base import argflag_std_base
argparser = argparse.ArgumentParser()
argparser.add_argument('-b', default=add_0x(hex(argflag_std_base)), help='argument flag bitmask')
argparser.add_argument('-n', help='tool name to use for man filename')
argparser.add_argument('-d', default='.', help='output directory')
argparser.add_argument('header_file', help='groff file containing heading, synopsis and description')
args = argparser.parse_args(sys.argv[1:])
#b = bytes.fromhex(strip_0x(sys.argv[1]))
b = bytes.fromhex(strip_0x(args.b))
g = DocGenerator(int.from_bytes(b, byteorder='little'))
g.process()
f = open(args.header_file)
head = f.read()
f.close()
toolname = args.n
if toolname == None:
parts = os.path.splitext(os.path.basename(args.header_file))
toolname = parts[0]
(fd, fp) = tempfile.mkstemp()
f = os.fdopen(fd, 'w')
f.write(head)
f.write(str(g))
f.close()
dest = os.path.join(args.d, toolname + '.1')
shutil.copyfile(fp, dest)
os.unlink(fp)

View File

@ -3,7 +3,7 @@ name=chainlib
license=WTFPL2 license=WTFPL2
author_email=dev@holbrook.no author_email=dev@holbrook.no
description=Generic blockchain access library and tooling description=Generic blockchain access library and tooling
version=0.0.21 version=0.0.22
url=https://gitlab.com/chaintools/chainlib url=https://gitlab.com/chaintools/chainlib
author=Louis Holbrook author=Louis Holbrook

View File

@ -25,6 +25,6 @@ setup(
'chainlib.cli', 'chainlib.cli',
], ],
scripts = [ scripts = [
'scripts/man.py', 'scripts/chainlib-man.py',
], ],
) )