From 69a4a8bd32d262423a7b46e306ee6d47812aaf6b Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Fri, 7 Aug 2026 09:37:47 +0000 Subject: [PATCH] ruff SIM102 (use single `if` instead of nested `if`) --- mmgen/addrlist.py | 5 ++--- mmgen/autosign/__init__.py | 9 ++++----- mmgen/autosign/signable.py | 5 ++--- mmgen/base_obj.py | 9 ++++----- mmgen/cfgfile.py | 11 +++++------ mmgen/crypto.py | 7 +++---- mmgen/fileutil.py | 14 ++++++-------- mmgen/main_autosign.py | 5 ++--- mmgen/main_txsend.py | 12 +++++------- mmgen/proto/btc/rpc/local.py | 7 +++---- mmgen/swap/asset.py | 13 ++++++------- mmgen/tx/base.py | 7 +++---- mmgen/xmrwallet/ops/create.py | 5 ++--- test/cmdtest_d/autosign.py | 7 +++---- test/include/common.py | 5 ++--- 15 files changed, 52 insertions(+), 69 deletions(-) diff --git a/mmgen/addrlist.py b/mmgen/addrlist.py index 9fe766e3..21ec3b39 100755 --- a/mmgen/addrlist.py +++ b/mmgen/addrlist.py @@ -222,9 +222,8 @@ class AddrList(MMGenObject): # Address info for a single seed ID if self.al_id is None: return - if type(self) is ViewKeyAddrList: - if not 'viewkey' in self.al_id.mmtype.extra_attrs: - die(1, f'viewkeys not supported for address type {self.al_id.mmtype.desc!r}') + if type(self) is ViewKeyAddrList and not 'viewkey' in self.al_id.mmtype.extra_attrs: + die(1, f'viewkeys not supported for address type {self.al_id.mmtype.desc!r}') self.id_str = AddrListIDStr(self) diff --git a/mmgen/autosign/__init__.py b/mmgen/autosign/__init__.py index 6adca942..acbeb2da 100755 --- a/mmgen/autosign/__init__.py +++ b/mmgen/autosign/__init__.py @@ -73,11 +73,10 @@ class Autosign: def __init__(self, cfg, *, cmd=None): - if cfg.mnemonic_fmt: - if cfg.mnemonic_fmt not in self.mn_fmts: - die(1, '{!r}: invalid mnemonic format (must be one of: {})'.format( - cfg.mnemonic_fmt, - fmt_list(self.mn_fmts, fmt='no_spc'))) + if cfg.mnemonic_fmt and cfg.mnemonic_fmt not in self.mn_fmts: + die(1, '{!r}: invalid mnemonic format (must be one of: {})'.format( + cfg.mnemonic_fmt, + fmt_list(self.mn_fmts, fmt='no_spc'))) match gc.platform: case 'linux': diff --git a/mmgen/autosign/signable.py b/mmgen/autosign/signable.py index a6ebcca6..1ac0158d 100755 --- a/mmgen/autosign/signable.py +++ b/mmgen/autosign/signable.py @@ -198,9 +198,8 @@ class Signable: self.die_wrong_num_txs('unsent_raw', desc='unsent') if len(self.unsent) > 1: self.die_wrong_num_txs('unsent') - if self.unsent: - if self.unsent[0].stem != self.unsent_raw[0].stem: - die(1, f'{self.unsent[0]}, {self.unsent_raw[0]}: file mismatch') + if self.unsent and self.unsent[0].stem != self.unsent_raw[0].stem: + die(1, f'{self.unsent[0]}, {self.unsent_raw[0]}: file mismatch') return self.unsent_raw + self.unsent def shred_abortable(self): diff --git a/mmgen/base_obj.py b/mmgen/base_obj.py index a110486f..0fe12d0d 100755 --- a/mmgen/base_obj.py +++ b/mmgen/base_obj.py @@ -117,12 +117,11 @@ class Lockable(AttrCtrl): val = getattr(self, name) if name not in (self._set_ok + self._reset_ok): raise AttributeError(f'attribute {name!r} of {type(self).__name__} object is read-only') - elif name not in self._reset_ok: - if not ( + elif name not in self._reset_ok and not ( (val != 0 and not val) or (self._use_class_attr and name not in self.__dict__)): - raise AttributeError( - f'attribute {name!r} of {type(self).__name__} object is already set,' - + ' and resetting is forbidden') + raise AttributeError( + f'attribute {name!r} of {type(self).__name__} object is already set,' + + ' and resetting is forbidden') return AttrCtrl.__setattr__(self, name, value) diff --git a/mmgen/cfgfile.py b/mmgen/cfgfile.py index 7d011235..ab71af1a 100755 --- a/mmgen/cfgfile.py +++ b/mmgen/cfgfile.py @@ -226,12 +226,11 @@ class CfgFileSampleUsr(cfg_file_sample): self.copy_system_data(self.fn) def parse_metadata(self): - if self.data: - if m := re.match(r'# Version (\d+) ([a-f0-9]{40})$', self.data[-1]): - self.ver = m[1] - self.chksum = m[2] - self.data = self.data[:-1] # remove metadata line - return True + if self.data and (m := re.match(r'# Version (\d+) ([a-f0-9]{40})$', self.data[-1])): + self.ver = m[1] + self.chksum = m[2] + self.data = self.data[:-1] # remove metadata line + return True def diff(self, a_tup, b_tup): # a=user, b=system a = [i.name for i in a_tup]#[3:] # Debug diff --git a/mmgen/crypto.py b/mmgen/crypto.py index d628b3f4..bbcf1979 100755 --- a/mmgen/crypto.py +++ b/mmgen/crypto.py @@ -116,10 +116,9 @@ class Crypto: def decrypt_seed(self, enc_seed, key, *, seed_id, key_id): self.util.vmsg_r('Checking key...') chk1 = make_chksum_8(key) - if key_id: - if not self.util.compare_chksums(key_id, 'key ID', chk1, 'computed'): - msg('Incorrect passphrase or hash preset') - return False + if key_id and not self.util.compare_chksums(key_id, 'key ID', chk1, 'computed'): + msg('Incorrect passphrase or hash preset') + return False dec_seed = self.decrypt_data(enc_seed, key, desc='seed') chk2 = make_chksum_8(dec_seed) diff --git a/mmgen/fileutil.py b/mmgen/fileutil.py index 5ddee238..1253a779 100755 --- a/mmgen/fileutil.py +++ b/mmgen/fileutil.py @@ -40,11 +40,10 @@ def check_or_create_dir(path): try: os.listdir(path) except: - if os.getenv('MMGEN_TEST_SUITE'): - if os.path.exists(path): # path is a link or regular file - from subprocess import run - run(['rm', '-rf', str(path)]) - set_vt100() + if os.getenv('MMGEN_TEST_SUITE') and os.path.exists(path): # path is a link or regular file + from subprocess import run + run(['rm', '-rf', str(path)]) + set_vt100() try: os.makedirs(path, 0o700) except: @@ -83,9 +82,8 @@ def _check_file_type_and_access(fname, ftype, *, blkdev_ok=False): (stat.S_ISREG, 'regular file'), (stat.S_ISLNK, 'symbolic link') ] - if blkdev_ok: - if not gc.platform in ('win32',): - ok_types.append((stat.S_ISBLK, 'block device')) + if blkdev_ok and gc.platform != 'win32': + ok_types.append((stat.S_ISBLK, 'block device')) try: mode = os.stat(fname).st_mode diff --git a/mmgen/main_autosign.py b/mmgen/main_autosign.py index 2266495a..cde02fe7 100755 --- a/mmgen/main_autosign.py +++ b/mmgen/main_autosign.py @@ -135,9 +135,8 @@ if cmd in ('enable_swap', 'disable_swap', 'list_led', 'test_led'): if cmd not in Autosign.cmds + Autosign.util_cmds: die(1, f'‘{cmd}’: unrecognized command') -if cfg.xmrwallets: - if cmd not in ('setup', 'xmr_setup'): - die(1, '--xmrwallets is valid only for the ‘setup’ and ‘xmr_setup’ operations') +if cfg.xmrwallets and cmd not in ('setup', 'xmr_setup'): + die(1, '--xmrwallets is valid only for the ‘setup’ and ‘xmr_setup’ operations') if cmd != 'setup': for opt in ('seed_len', 'mnemonic_fmt', 'keys_from_file'): diff --git a/mmgen/main_txsend.py b/mmgen/main_txsend.py index 2df93583..14f79911 100755 --- a/mmgen/main_txsend.py +++ b/mmgen/main_txsend.py @@ -159,10 +159,9 @@ async def process_tx(tx): txcfg = Config({'_clone': cfg, 'proto': tx.proto, 'coin': tx.proto.coin}) - if not post_send_op: - if cfg.tx_proxy: - from .tx.tx_proxy import check_client - check_client(txcfg) + if (not post_send_op) and cfg.tx_proxy: + from .tx.tx_proxy import check_client + check_client(txcfg) from .rpc import rpc_init tx.rpc = await rpc_init(txcfg) @@ -177,9 +176,8 @@ async def process_tx(tx): if not cfg.yes: tx.info.view_with_prompt('View transaction details?') - if tx.add_comment(): # edits an existing comment, returns true if changed - if not cfg.autosign: - tx.file.write(ask_write_default_yes=True) + if tx.add_comment() and not cfg.autosign: # edits existing comment, returns true if changed + tx.file.write(ask_write_default_yes=True) return await tx.send(txcfg, asi, batch=batch) diff --git a/mmgen/proto/btc/rpc/local.py b/mmgen/proto/btc/rpc/local.py index 69738108..84629be1 100755 --- a/mmgen/proto/btc/rpc/local.py +++ b/mmgen/proto/btc/rpc/local.py @@ -316,10 +316,9 @@ class BitcoinRPCClient(RPCClient, metaclass=AsyncInit): self.auth = auth_data(user, passwd) return - if self.has_auth_cookie: - if cookie := self.get_daemon_auth_cookie(): - self.auth = auth_data(*cookie.split(':')) - return + if self.has_auth_cookie and (cookie := self.get_daemon_auth_cookie()): + self.auth = auth_data(*cookie.split(':')) + return die(1, '\n\n' + fmt(no_credentials_errmsg, strip_char='\t', indent=' ').format( proto_name = self.proto.name, diff --git a/mmgen/swap/asset.py b/mmgen/swap/asset.py index df8e922c..1cce74d3 100755 --- a/mmgen/swap/asset.py +++ b/mmgen/swap/asset.py @@ -31,13 +31,12 @@ class SwapAsset: fs = '%s{:10} {:23} {:9} {}' % indent yield fs.format('ASSET', 'DESCRIPTION', 'STATUS', 'CONTRACT ADDRESS') for k, v in self.assets_data.items(): - if not k in self.blacklisted: - if k in self.send or k in self.recv: - yield fs.format( - k, - v.desc, - 'tested' if v.tested else 'untested', - self.evm_contracts.get(k,'-')) + if not k in self.blacklisted and (k in self.send or k in self.recv): + yield fs.format( + k, + v.desc, + 'tested' if v.tested else 'untested', + self.evm_contracts.get(k,'-')) def gen_bad(): if self.blacklisted: diff --git a/mmgen/tx/base.py b/mmgen/tx/base.py index b9db2ea9..f41319f4 100755 --- a/mmgen/tx/base.py +++ b/mmgen/tx/base.py @@ -143,10 +143,9 @@ class Base(MMGenObject): return init_info(self.cfg, self) def check_correct_chain(self): - if hasattr(self, 'rpc'): - if self.chain != self.rpc.chain: - die('TransactionChainMismatch', - f'Transaction is for {self.chain}, but coin daemon chain is {self.rpc.chain}!') + if hasattr(self, 'rpc') and self.chain != self.rpc.chain: + die('TransactionChainMismatch', + f'Transaction is for {self.chain}, but coin daemon chain is {self.rpc.chain}!') def sum_inputs(self): return sum(e.amt for e in self.inputs) diff --git a/mmgen/xmrwallet/ops/create.py b/mmgen/xmrwallet/ops/create.py index 45c9b83b..d95e917e 100755 --- a/mmgen/xmrwallet/ops/create.py +++ b/mmgen/xmrwallet/ops/create.py @@ -24,9 +24,8 @@ class OpCreate(OpWallet): opts = ('restore_height',) def check_uopts(self): - if self.cfg.restore_height != 'current': - if int(self.cfg.restore_height or 0) < 0: - die(1, f'{self.cfg.restore_height}: invalid value for --restore-height (less than zero)') + if self.cfg.restore_height != 'current' and int(self.cfg.restore_height or 0) < 0: + die(1, f'{self.cfg.restore_height}: invalid value for --restore-height (less than zero)') if self.cfg.compat: self.cfg.wallet_dir.mkdir(parents=True, exist_ok=True) diff --git a/test/cmdtest_d/autosign.py b/test/cmdtest_d/autosign.py index 7815fc9f..43c62c44 100755 --- a/test/cmdtest_d/autosign.py +++ b/test/cmdtest_d/autosign.py @@ -98,10 +98,9 @@ class CmdTestAutosignBase(CmdTestBase): if hasattr(self, 'txdev'): del self.txdev - if not self.cfg.no_daemon_stop: - if gc.platform == 'darwin': - for label in (self.asi.dev_label, self.asi.macos_ramdisk.label): - self._macOS_eject_disk(label) + if (not self.cfg.no_daemon_stop) and gc.platform == 'darwin': + for label in (self.asi.dev_label, self.asi.macos_ramdisk.label): + self._macOS_eject_disk(label) def _create_autosign_instances(self, create_dirs): d = {'offline': {'name':'asi'}} diff --git a/test/include/common.py b/test/include/common.py index bda31ec4..29ddc30d 100755 --- a/test/include/common.py +++ b/test/include/common.py @@ -174,9 +174,8 @@ def clean(cfgs, tmpdir_ids=None, extra_dirs=[]): def clean_extra_dirs(): for d in extra_dirs: - if os.path.exists(d): - if cleandir(d): - yield os.path.relpath(d) + if os.path.exists(d) and cleandir(d): + yield os.path.relpath(d) for clean_func, list_fmt in ( (clean_tmpdirs, 'no_quotes'),