pylint: utils.py

pySim/utils.py:903:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens)
pySim/utils.py:153:16: R1719: The if expression can be replaced with 'bool(test)' (simplifiable-if-expression)
pySim/utils.py:158:16: R1719: The if expression can be replaced with 'bool(test)' (simplifiable-if-expression)
pySim/utils.py:166:16: R1719: The if expression can be replaced with 'bool(test)' (simplifiable-if-expression)
pySim/utils.py:222:19: R1719: The if expression can be replaced with 'not test' (simplifiable-if-expression)
pySim/utils.py:237:18: R1719: The if expression can be replaced with 'bool(test)' (simplifiable-if-expression)
pySim/utils.py:246:19: R1719: The if expression can be replaced with 'not test' (simplifiable-if-expression)
pySim/utils.py:279:11: W0612: Unused variable 'remainder' (unused-variable)
pySim/utils.py:541:7: R1714: Consider merging these comparisons with 'in' by using 'eutran_bits in (16384, 28672)'. Use a set instead if elements are hashable. (consider-using-in)
pySim/utils.py:550:7: R1714: Consider merging these comparisons with 'in' by using 'gsm_bits in (128, 140)'. Use a set instead if elements are hashable. (consider-using-in)
pySim/utils.py:614:7: C0121: Comparison 'imsi == None' should be 'imsi is None' (singleton-comparison)
pySim/utils.py:627:7: C0121: Comparison 'imsi == None' should be 'imsi is None' (singleton-comparison)
pySim/utils.py:733:7: R1714: Consider merging these comparisons with 'in' by using 'msisdn in ('', '+')'. Use a set instead if elements are hashable. (consider-using-in)
pySim/utils.py:774:8: W0612: Unused variable 'try_encode' (unused-variable)
pySim/utils.py:803:16: W0707: Consider explicitly re-raising using 'except ValueError as exc' and 'raise ValueError('PIN-ADM needs to be hex encoded using this option') from exc' (raise-missing-from)
pySim/utils.py:801:16: W0612: Unused variable 'try_encode' (unused-variable)
pySim/utils.py:821:7: C1802: Do not use `len(SEQUENCE)` without comparison to determine if a sequence is empty (use-implicit-booleaness-not-len)
pySim/utils.py:836:4: W0612: Unused variable 'e' (unused-variable)
pySim/utils.py:892:7: C0121: Comparison 'str_list == None' should be 'str_list is None' (singleton-comparison)
pySim/utils.py:991:11: R1701: Consider merging these isinstance calls to isinstance(o, (BytesIO, bytearray, bytes)) (consider-merging-isinstance)

Change-Id: I190ae75964ef6e0ed43fae994693a8bccd21c7f7
This commit is contained in:
Harald Welte 2024-02-05 01:02:34 +01:00
parent 472165f20f
commit f2b20bf6ca
1 changed files with 21 additions and 23 deletions

View File

@ -150,12 +150,12 @@ def comprehensiontlv_parse_tag(binary: bytes) -> Tuple[dict, bytes]:
# three-byte tag # three-byte tag
tag = (binary[1] & 0x7f) << 8 tag = (binary[1] & 0x7f) << 8
tag |= binary[2] tag |= binary[2]
compr = True if binary[1] & 0x80 else False compr = bool(binary[1] & 0x80)
return ({'comprehension': compr, 'tag': tag}, binary[3:]) return ({'comprehension': compr, 'tag': tag}, binary[3:])
else: else:
# single byte tag # single byte tag
tag = binary[0] & 0x7f tag = binary[0] & 0x7f
compr = True if binary[0] & 0x80 else False compr = bool(binary[0] & 0x80)
return ({'comprehension': compr, 'tag': tag}, binary[1:]) return ({'comprehension': compr, 'tag': tag}, binary[1:])
@ -163,7 +163,7 @@ def comprehensiontlv_encode_tag(tag) -> bytes:
"""Encode a single Tag according to ETSI TS 101 220 Section 7.1.1""" """Encode a single Tag according to ETSI TS 101 220 Section 7.1.1"""
# permit caller to specify tag also as integer value # permit caller to specify tag also as integer value
if isinstance(tag, int): if isinstance(tag, int):
compr = True if tag < 0xff and tag & 0x80 else False compr = bool(tag < 0xff and tag & 0x80)
tag = {'tag': tag, 'comprehension': compr} tag = {'tag': tag, 'comprehension': compr}
compr = tag.get('comprehension', False) compr = tag.get('comprehension', False)
if tag['tag'] in [0x00, 0x80, 0xff] or tag['tag'] > 0xff: if tag['tag'] in [0x00, 0x80, 0xff] or tag['tag'] > 0xff:
@ -219,7 +219,7 @@ def bertlv_parse_tag_raw(binary: bytes) -> Tuple[int, bytes]:
i = 1 i = 1
last = False last = False
while not last: while not last:
last = False if binary[i] & 0x80 else True last = not bool(binary[i] & 0x80)
tag <<= 8 tag <<= 8
tag |= binary[i] tag |= binary[i]
i += 1 i += 1
@ -234,7 +234,7 @@ def bertlv_parse_tag(binary: bytes) -> Tuple[dict, bytes]:
Tuple of ({class:int, constructed:bool, tag:int}, remainder:bytes) Tuple of ({class:int, constructed:bool, tag:int}, remainder:bytes)
""" """
cls = binary[0] >> 6 cls = binary[0] >> 6
constructed = True if binary[0] & 0x20 else False constructed = bool(binary[0] & 0x20)
tag = binary[0] & 0x1f tag = binary[0] & 0x1f
if tag <= 30: if tag <= 30:
return ({'class': cls, 'constructed': constructed, 'tag': tag}, binary[1:]) return ({'class': cls, 'constructed': constructed, 'tag': tag}, binary[1:])
@ -243,7 +243,7 @@ def bertlv_parse_tag(binary: bytes) -> Tuple[dict, bytes]:
i = 1 i = 1
last = False last = False
while not last: while not last:
last = False if binary[i] & 0x80 else True last = not bool(binary[i] & 0x80)
tag <<= 7 tag <<= 7
tag |= binary[i] & 0x7f tag |= binary[i] & 0x7f
i += 1 i += 1
@ -276,7 +276,7 @@ def bertlv_encode_tag(t) -> bytes:
if isinstance(t, int): if isinstance(t, int):
# first convert to a dict representation # first convert to a dict representation
tag_size = count_int_bytes(t) tag_size = count_int_bytes(t)
t, remainder = bertlv_parse_tag(t.to_bytes(tag_size, 'big')) t, _remainder = bertlv_parse_tag(t.to_bytes(tag_size, 'big'))
tag = t['tag'] tag = t['tag']
constructed = t['constructed'] constructed = t['constructed']
cls = t['class'] cls = t['class']
@ -538,7 +538,7 @@ def dec_act(twohexbytes: Hexstr) -> List[str]:
sel.add(a['name']) sel.add(a['name'])
# TS 31.102 Section 4.2.5 Table 4.2.5.1 # TS 31.102 Section 4.2.5 Table 4.2.5.1
eutran_bits = u16t & 0x7000 eutran_bits = u16t & 0x7000
if eutran_bits == 0x4000 or eutran_bits == 0x7000: if eutran_bits in [0x4000, 0x7000]:
sel.add("E-UTRAN WB-S1") sel.add("E-UTRAN WB-S1")
sel.add("E-UTRAN NB-S1") sel.add("E-UTRAN NB-S1")
elif eutran_bits == 0x5000: elif eutran_bits == 0x5000:
@ -547,7 +547,7 @@ def dec_act(twohexbytes: Hexstr) -> List[str]:
sel.add("E-UTRAN WB-S1") sel.add("E-UTRAN WB-S1")
# TS 31.102 Section 4.2.5 Table 4.2.5.2 # TS 31.102 Section 4.2.5 Table 4.2.5.2
gsm_bits = u16t & 0x008C gsm_bits = u16t & 0x008C
if gsm_bits == 0x0080 or gsm_bits == 0x008C: if gsm_bits in [0x0080, 0x008C]:
sel.add("GSM") sel.add("GSM")
sel.add("EC-GSM-IoT") sel.add("EC-GSM-IoT")
elif u16t & 0x008C == 0x0084: elif u16t & 0x008C == 0x0084:
@ -611,7 +611,7 @@ def mcc_from_imsi(imsi: str) -> Optional[str]:
""" """
Derive the MCC (Mobile Country Code) from the first three digits of an IMSI Derive the MCC (Mobile Country Code) from the first three digits of an IMSI
""" """
if imsi == None: if imsi is None:
return None return None
if len(imsi) > 3: if len(imsi) > 3:
@ -624,7 +624,7 @@ def mnc_from_imsi(imsi: str, long: bool = False) -> Optional[str]:
""" """
Derive the MNC (Mobile Country Code) from the 4th to 6th digit of an IMSI Derive the MNC (Mobile Country Code) from the 4th to 6th digit of an IMSI
""" """
if imsi == None: if imsi is None:
return None return None
if len(imsi) > 3: if len(imsi) > 3:
@ -730,7 +730,7 @@ def enc_msisdn(msisdn: str, npi: int = 0x01, ton: int = 0x03) -> Hexstr:
""" """
# If no MSISDN is supplied then encode the file contents as all "ff" # If no MSISDN is supplied then encode the file contents as all "ff"
if msisdn == "" or msisdn == "+": if msisdn in ["", "+"]:
return "ff" * 14 return "ff" * 14
# Leading '+' indicates International Number # Leading '+' indicates International Number
@ -771,7 +771,7 @@ def is_hex(string: str, minlen: int = 2, maxlen: Optional[int] = None) -> bool:
# Try actual encoding to be sure # Try actual encoding to be sure
try: try:
try_encode = h2b(string) _try_encode = h2b(string)
return True return True
except: except:
return False return False
@ -799,12 +799,10 @@ def sanitize_pin_adm(pin_adm, pin_adm_hex=None) -> Hexstr:
# Ensure that it's hex-encoded # Ensure that it's hex-encoded
try: try:
try_encode = h2b(pin_adm) try_encode = h2b(pin_adm)
except ValueError: except ValueError as exc:
raise ValueError( raise ValueError("PIN-ADM needs to be hex encoded using this option") from exc
"PIN-ADM needs to be hex encoded using this option")
else: else:
raise ValueError( raise ValueError("PIN-ADM needs to be exactly 16 digits (hex encoded)")
"PIN-ADM needs to be exactly 16 digits (hex encoded)")
return pin_adm return pin_adm
@ -818,7 +816,7 @@ def get_addr_type(addr):
""" """
# Empty address string # Empty address string
if not len(addr): if len(addr) == 0:
return None return None
addr_list = addr.split('.') addr_list = addr.split('.')
@ -833,7 +831,7 @@ def get_addr_type(addr):
return 0x01 return 0x01
elif ipa.version == 6: elif ipa.version == 6:
return 0x02 return 0x02
except Exception as e: except Exception:
invalid_ipv4 = True invalid_ipv4 = True
for i in addr_list: for i in addr_list:
# Invalid IPv4 may qualify for a valid FQDN, so make check here # Invalid IPv4 may qualify for a valid FQDN, so make check here
@ -889,7 +887,7 @@ def tabulate_str_list(str_list, width: int = 79, hspace: int = 2, lspace: int =
Returns: Returns:
multi-line string containing formatted table multi-line string containing formatted table
""" """
if str_list == None: if str_list is None:
return "" return ""
if len(str_list) <= 0: if len(str_list) <= 0:
return "" return ""
@ -900,7 +898,7 @@ def tabulate_str_list(str_list, width: int = 79, hspace: int = 2, lspace: int =
table = [] table = []
for i in iter(range(rows)): for i in iter(range(rows)):
str_list_row = str_list[i::rows] str_list_row = str_list[i::rows]
if (align_left): if align_left:
format_str_cell = '%%-%ds' format_str_cell = '%%-%ds'
else: else:
format_str_cell = '%%%ds' format_str_cell = '%%%ds'
@ -988,7 +986,7 @@ class JsonEncoder(json.JSONEncoder):
"""Extend the standard library JSONEncoder with support for more types.""" """Extend the standard library JSONEncoder with support for more types."""
def default(self, o): def default(self, o):
if isinstance(o, BytesIO) or isinstance(o, bytes) or isinstance(o, bytearray): if isinstance(o, (BytesIO, bytes, bytearray)):
return b2h(o) return b2h(o)
elif isinstance(o, datetime.datetime): elif isinstance(o, datetime.datetime):
return o.isoformat() return o.isoformat()