Add env/config descriptions from ini dir

This commit is contained in:
lash 2022-02-23 09:48:15 +00:00
parent 92fb016014
commit af7f977007
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 113 additions and 42 deletions

View File

@ -1,29 +1,50 @@
# standard imports
import os
# external imports
import confini
# local imports # local imports
from .base import ( from .base import (
Flag, Flag,
argflag_std_target, argflag_std_target,
) )
def apply_groff(collection, v, arg=None): script_dir = os.path.dirname(os.path.realpath(__file__))
data_dir = os.path.join(script_dir, '..', 'data')
def apply_groff(collection, v, arg=None, typ='arg'):
s = '' s = ''
for flag in collection: for flag in collection:
if len(s) > 0: if len(s) > 0:
s += ', ' s += ', '
s += '\\fB' + flag s += format_groff(flag, v, arg=arg, typ=typ)
if arg != None: return s
s += ' \\fI' + arg
s += '\\fP'
def format_groff(k, v, arg=None, typ='arg'):
s = ''
if typ == 'env':
s += '\\fI'
else:
s += '\\fB'
s += k
if arg != None:
s += ' \\fI' + arg
s += '\\fP'
s = "\n.TP\n" + s + "\n" + v s = "\n.TP\n" + s + "\n" + v
return s return s
class DocEntry: class DocEntry:
def __init__(self, *args, argvalue=None): def __init__(self, *args, argvalue=None, typ='arg'):
self.flags = args self.flags = args
self.v = argvalue self.v = argvalue
self.render = self.get_empty self.render = self.get_empty
self.groff = None self.groff = None
self.typ = typ
def __check_line_default(self, m): def __check_line_default(self, m):
@ -51,7 +72,7 @@ class DocEntry:
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 = apply_groff(self.flags, v, arg=self.v, typ=self.typ)
return s return s
@ -61,11 +82,12 @@ class DocEntry:
class DocGenerator: class DocGenerator:
def __init__(self, arg_flags, config): # def __init__(self, arg_flags, config):
self.config = config def __init__(self, arg_flags):
#self.config = config
self.arg_flags = arg_flags self.arg_flags = arg_flags
self.docs = {} self.docs = {}
self.envs = {} # self.envs = {}
def __str__(self): def __str__(self):
@ -86,15 +108,6 @@ class DocGenerator:
return s 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): def override_arg(self, k, v, args):
o = self.docs[k] o = self.docs[k]
#g.docs[v[0]].groff = v[1].rstrip() #g.docs[v[0]].groff = v[1].rstrip()
@ -105,12 +118,12 @@ class DocGenerator:
for i in range(l): for i in range(l):
o.flags.append(args[i]) o.flags.append(args[i])
#
def process_env(self): # def process_env(self):
for k in self.config.all(): # for k in self.config.all():
if k[0] == '_': # if k[0] == '_':
continue # continue
self.envs[k] = None # self.envs[k] = None
def process_arg(self): def process_arg(self):
@ -255,4 +268,54 @@ class DocGenerator:
def process(self): def process(self):
self.process_arg() self.process_arg()
self.process_env() # self.process_env()
class EnvDocGenerator:
def __init__(self, arg_flags, override=None):
self.arg_flags = arg_flags
self.envs = {}
env_dir = os.path.join(data_dir, 'env')
self.config = confini.Config(env_dir, override_dirs=override)
self.config.process()
def __add(self, k):
v = format_groff(k, self.config.get(k), None, typ='env')
self.envs[k] = v
def process(self):
ks = []
if self.arg_flags & Flag.PROVIDER:
ks += [
'RPC_PROVIDER',
'RPC_DIALECT',
]
if self.arg_flags & Flag.RPC_AUTH:
ks += [
'RPC_AUTH',
'RPC_CREDENTIALS',
]
if self.arg_flags & Flag.CHAIN_SPEC:
ks.append('CHAIN_SPEC')
if self.arg_flags & Flag.KEY_FILE:
ks += [
'WALLET_KEY_FILE',
'WALLET_PASSPHRASE',
]
for k in ks:
self.__add(k)
def __str__(self):
s = ''
ks = list(self.envs.keys())
ks.sort()
for k in ks:
s += str(self.envs[k]) + "\n"
return s

14
chainlib/data/env/env.ini vendored Normal file
View File

@ -0,0 +1,14 @@
[rpc]
provider = Fully-qualified URL to the RPC endpoint of the blockchain node.
auth = Authentication method to use for the \fIRPC_PROVIDER\fP. Currently only \fIbasic\fP is supported.
credentials = Authentication credentials to use for \fIRPC_AUTH\fP. For \fIbasic\fP authentication the value must be given as \fI<user>:<pass>\fP.
dialect = Enables translations of EVM node specific formatting and response codes.
scheme = (needs content)
verify = (needs content)
[chain]
spec = String specifying the type of chain connected to, in the format \fI<engine>:<fork>:<network_id>:<common_name>\fP. For EVM nodes the \fIengine\fP value will always be \fIevm\fP.
[wallet]
key_file = The wallet key file containing private key to use for transaction signing. Overridden by \fB-y\fP.
passphrase = Passphrase to unlock wallet. \fBWARNING:\fP it is \fBunsafe\fP to pass the passphrase as an environment variable. If the key unlocks something of value, the passphrase should rather be in a configuration file, preferably as an encrypted entry. Alternatively, a passphrase can be read from file using the \fB--passphrase-file\fP option. Files containing passphrases should only be accessible by the owner.

View File

@ -10,6 +10,7 @@ import shutil
from hexathon import strip_0x, add_0x from hexathon import strip_0x, add_0x
from chainlib.cli.man import ( from chainlib.cli.man import (
EnvDocGenerator,
DocGenerator, DocGenerator,
apply_groff, apply_groff,
) )
@ -28,7 +29,7 @@ argparser.add_argument('-n', help='tool name to use for man filename')
argparser.add_argument('-d', default='.', help='output directory') argparser.add_argument('-d', default='.', help='output directory')
argparser.add_argument('-v', action='store_true', help='turn on debug logging') 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-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('--overrides-env-dir', dest='overrides_env_dir', help='load envionment description override config from directory')
argparser.add_argument('header_file', help='groff file containing heading, synopsis and description') argparser.add_argument('header_file', help='groff file containing heading, synopsis and description')
args = argparser.parse_args(sys.argv[1:]) args = argparser.parse_args(sys.argv[1:])
@ -39,9 +40,10 @@ if args.v:
b = bytes.fromhex(strip_0x(args.b)) b = bytes.fromhex(strip_0x(args.b))
flags = int.from_bytes(b, byteorder='little') flags = int.from_bytes(b, byteorder='little')
empty_args = ChainlibArgumentParser(flags).parse_args([]) #empty_args = ChainlibArgumentParser(flags).parse_args([])
config = Config.from_args(empty_args, arg_flags=flags) #config = Config.from_args(empty_args, arg_flags=flags)
g = DocGenerator(flags, config) #g = DocGenerator(flags, config)
g = DocGenerator(flags)
toolname = args.n toolname = args.n
if toolname == None: if toolname == None:
@ -66,18 +68,8 @@ if args.overrides_file != None:
f.close() f.close()
s_env = '' ge = EnvDocGenerator(flags, override=args.overrides_env_dir)
if args.overrides_env_file != None: ge.process()
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) f = open(args.header_file)
head = f.read() head = f.read()
@ -87,6 +79,8 @@ f.close()
f = os.fdopen(fd, 'w') f = os.fdopen(fd, 'w')
f.write(head) f.write(head)
f.write(str(g)) f.write(str(g))
f.write(".SH ENVIRONMENT\n\n")
f.write(str(ge))
f.close() f.close()
dest = os.path.join(args.d, toolname + '.1') dest = os.path.join(args.d, toolname + '.1')