Browse Source

minor cleanups (9 files)

The MMGen Project 2 months ago
parent
commit
1ecde8880b
9 changed files with 28 additions and 26 deletions
  1. 1 1
      mmgen/addrfile.py
  2. 5 1
      mmgen/baseconv.py
  3. 1 1
      mmgen/filename.py
  4. 6 7
      mmgen/main_msg.py
  5. 6 6
      mmgen/main_regtest.py
  6. 3 5
      mmgen/proto/btc/tw/ctl.py
  7. 1 0
      test/clean.py
  8. 3 4
      test/objtest.py
  9. 2 1
      test/tooltest2.py

+ 1 - 1
mmgen/addrfile.py

@@ -343,7 +343,7 @@ class PasswordFile(AddrFile):
 		p = self.parent
 		p = self.parent
 
 
 		if p.pw_fmt in ('bip39', 'xmrseed'):
 		if p.pw_fmt in ('bip39', 'xmrseed'):
-			ret = lines.pop(0).split(None, p.pw_len+1)
+			ret = lines.pop(0).split(None, p.pw_len + 1)
 			if len(ret) > p.pw_len+1:
 			if len(ret) > p.pw_len+1:
 				m1 = f'extraneous text {ret[p.pw_len+1]!r} found after password'
 				m1 = f'extraneous text {ret[p.pw_len+1]!r} found after password'
 				m2 = '[bare comments not allowed in BIP39 password files]'
 				m2 = '[bare comments not allowed in BIP39 password files]'

+ 5 - 1
mmgen/baseconv.py

@@ -120,6 +120,10 @@ class baseconv:
 		If 'seed', output length will be mapped from input length using data in seedlen_map.
 		If 'seed', output length will be mapped from input length using data in seedlen_map.
 		If an integer, the string, hex string or byte output will be padded to this length.
 		If an integer, the string, hex string or byte output will be padded to this length.
 		"""
 		"""
+
+		def do_die():
+			die('BaseConversionPadError', f"{pad!r}: illegal value for 'pad' (must be None, 'seed' or int)")
+
 		if pad is None:
 		if pad is None:
 			return 0
 			return 0
 		elif type(pad) is int:
 		elif type(pad) is int:
@@ -127,7 +131,7 @@ class baseconv:
 		elif pad == 'seed':
 		elif pad == 'seed':
 			return seed_pad_func()
 			return seed_pad_func()
 		else:
 		else:
-			die('BaseConversionPadError', f"{pad!r}: illegal value for 'pad' (must be None, 'seed' or int)")
+			do_die()
 
 
 	def tohex(self, words_arg, /, *, pad=None):
 	def tohex(self, words_arg, /, *, pad=None):
 		"convert string or list data of instance base to a hexadecimal string"
 		"convert string or list data of instance base to a hexadecimal string"

+ 1 - 1
mmgen/filename.py

@@ -42,7 +42,7 @@ class File:
 
 
 		import stat
 		import stat
 		if stat.S_ISBLK(st.st_mode):
 		if stat.S_ISBLK(st.st_mode):
-			if sys.platform in ('win32',):
+			if sys.platform == 'win32':
 				die(2, 'Access to raw block devices not supported on platform {sys.platform!r}')
 				die(2, 'Access to raw block devices not supported on platform {sys.platform!r}')
 			mode = (os.O_RDONLY, os.O_RDWR)[bool(write)]
 			mode = (os.O_RDONLY, os.O_RDWR)[bool(write)]
 			try:
 			try:

+ 6 - 7
mmgen/main_msg.py

@@ -207,23 +207,22 @@ if len(cmd_args) < 2:
 	cfg._usage()
 	cfg._usage()
 
 
 op = cmd_args.pop(0)
 op = cmd_args.pop(0)
+arg1 = cmd_args.pop(0)
 
 
 if cfg.msghash_type and op != 'create':
 if cfg.msghash_type and op != 'create':
 	die(1, '--msghash-type option may only be used with the "create" command')
 	die(1, '--msghash-type option may only be used with the "create" command')
 
 
 async def main():
 async def main():
 	if op == 'create':
 	if op == 'create':
-		if len(cmd_args) < 2:
+		if not cmd_args:
 			cfg._usage()
 			cfg._usage()
-		MsgOps.create(cmd_args[0], ' '.join(cmd_args[1:]))
+		MsgOps.create(arg1, ' '.join(cmd_args))
 	elif op == 'sign':
 	elif op == 'sign':
-		if len(cmd_args) < 1:
-			cfg._usage()
-		await MsgOps.sign(cmd_args[0], cmd_args[1:])
+		await MsgOps.sign(arg1, cmd_args[:])
 	elif op in ('verify', 'export'):
 	elif op in ('verify', 'export'):
-		if len(cmd_args) not in (1, 2):
+		if len(cmd_args) not in (0, 1):
 			cfg._usage()
 			cfg._usage()
-		await getattr(MsgOps, op)(cmd_args[0], addr=cmd_args[1] if len(cmd_args) == 2 else None)
+		await getattr(MsgOps, op)(arg1, addr=cmd_args[0] if cmd_args else None)
 	else:
 	else:
 		die(1, f'{op!r}: unrecognized operation')
 		die(1, f'{op!r}: unrecognized operation')
 
 

+ 6 - 6
mmgen/main_regtest.py

@@ -64,17 +64,17 @@ cmd_args = cfg._args
 from .proto.btc.regtest import MMGenRegtest
 from .proto.btc.regtest import MMGenRegtest
 
 
 def check_num_args():
 def check_num_args():
-	m = getattr(MMGenRegtest, cmd_args[0])
+	cmd, *args = cmd_args
+	m = getattr(MMGenRegtest, cmd)
 	margs = m.__code__.co_varnames[1:m.__code__.co_argcount]
 	margs = m.__code__.co_varnames[1:m.__code__.co_argcount]
 	mdfls = m.__defaults__ or ()
 	mdfls = m.__defaults__ or ()
 	amin = len(margs) - len(mdfls)
 	amin = len(margs) - len(mdfls)
 	amax = len(margs)
 	amax = len(margs)
-	args = cmd_args[1:]
-	m = "{}: too {} arguments for command '%s' (must have no {} than {})" % cmd_args[0]
+	fs = '{}: too {} arguments for command ‘{}’ (must have no {} than {})'
 	if len(args) < amin:
 	if len(args) < amin:
-		die(1, m.format(args, 'few', 'less', amin))
-	elif len(cmd_args[1:]) > amax:
-		die(1, m.format(args, 'many', 'more', amax))
+		die(1, fs.format(args, 'few', cmd, 'less', amin))
+	elif len(args) > amax:
+		die(1, fs.format(args, 'many', cmd, 'more', amax))
 
 
 if not cmd_args:
 if not cmd_args:
 	cfg._usage()
 	cfg._usage()

+ 3 - 5
mmgen/proto/btc/tw/ctl.py

@@ -77,15 +77,13 @@ class BitcoinTwCtl(TwCtl):
 			return b if res else tip
 			return b if res else tip
 
 
 		def gen_chunks(start, stop, tip):
 		def gen_chunks(start, stop, tip):
-			n = start
 			if endless:
 			if endless:
 				stop = tip
 				stop = tip
 			elif stop > tip:
 			elif stop > tip:
 				die(1, f'{stop}: stop value is higher than chain tip')
 				die(1, f'{stop}: stop value is higher than chain tip')
-
-			while n <= stop:
-				yield (n, min(n+99, stop))
-				n += 100
+			while start <= stop:
+				yield (start, min(start + 99, stop))
+				start += 100
 
 
 		last_block = await do_scan(gen_chunks(start, stop, self.rpc.blockcount), self.rpc.blockcount)
 		last_block = await do_scan(gen_chunks(start, stop, self.rpc.blockcount), self.rpc.blockcount)
 
 

+ 1 - 0
test/clean.py

@@ -33,6 +33,7 @@ opts_data = {
 
 
 cfg = Config(
 cfg = Config(
 	opts_data = opts_data,
 	opts_data = opts_data,
+	need_proto = False,
 	init_opts = {'skip_cfg_file': True})
 	init_opts = {'skip_cfg_file': True})
 
 
 from test.overlay import get_overlay_tree_dir
 from test.overlay import get_overlay_tree_dir

+ 3 - 4
test/objtest.py

@@ -65,10 +65,9 @@ from test.include.common import set_globals
 set_globals(cfg)
 set_globals(cfg)
 
 
 def run_test(mod, test, arg, input_data, arg1, exc_name):
 def run_test(mod, test, arg, input_data, arg1, exc_name):
-	arg_copy = arg
-	kwargs = {}
-	ret_chk = arg
-	ret_idx = None
+
+	arg_copy, ret_chk, ret_idx, kwargs = (arg, arg, None, {})
+
 	if input_data == 'good' and isinstance(arg, tuple):
 	if input_data == 'good' and isinstance(arg, tuple):
 		arg, ret_chk = arg
 		arg, ret_chk = arg
 	if isinstance(arg, dict): # pass one arg + kwargs to constructor
 	if isinstance(arg, dict): # pass one arg + kwargs to constructor

+ 2 - 1
test/tooltest2.py

@@ -157,12 +157,13 @@ def check_output(out, chk):
 		out = str(out).encode()
 		out = str(out).encode()
 	if isinstance(out, str):
 	if isinstance(out, str):
 		out = out.encode()
 		out = out.encode()
-	err_fs = "Output ({!r}) doesn't match expected output ({!r})"
 	try:
 	try:
 		outd = out.decode()
 		outd = out.decode()
 	except:
 	except:
 		outd = None
 		outd = None
 
 
+	err_fs = "Output ({!r}) doesn't match expected output ({!r})"
+
 	if type(chk).__name__ == 'function':
 	if type(chk).__name__ == 'function':
 		assert chk(outd), f'{chk.__name__}({outd}) failed!'
 		assert chk(outd), f'{chk.__name__}({outd}) failed!'
 	elif isinstance(chk, dict):
 	elif isinstance(chk, dict):