summaryrefslogtreecommitdiff
path: root/chip/roun
diff options
context:
space:
mode:
Diffstat (limited to 'chip/roun')
-rwxr-xr-xchip/roun126
1 files changed, 77 insertions, 49 deletions
diff --git a/chip/roun b/chip/roun
index fb1624a..addbaf3 100755
--- a/chip/roun
+++ b/chip/roun
@@ -22,8 +22,9 @@ some point to be less odd than what proquints currently is. When I get time.
def _char_list_to_dict(char_list):
return {c: k for (c, k) in zip(char_list, range(len(char_list)))}
-UINT_TO_CONSONANT = 'bdfghjklmnprstvz'
-UINT_TO_VOWEL = 'aiou'
+
+UINT_TO_CONSONANT = "bdfghjklmnprstvz"
+UINT_TO_VOWEL = "aiou"
CONSONANT_TO_UINT = _char_list_to_dict(UINT_TO_CONSONANT)
VOWEL_TO_UINT = _char_list_to_dict(UINT_TO_VOWEL)
@@ -33,20 +34,21 @@ MASK_LAST2 = 0x3
CHARS_PER_CHUNK = 5
+
def _uint16_to_roun(uint16_val):
val = uint16_val
- res = ['?']*CHARS_PER_CHUNK
+ res = ["?"] * CHARS_PER_CHUNK
for i in range(CHARS_PER_CHUNK):
- if i&1:
+ if i & 1:
res[-i - 1] = UINT_TO_VOWEL[val & MASK_LAST2]
val >>= 2
else:
res[-i - 1] = UINT_TO_CONSONANT[val & MASK_LAST4]
val >>= 4
- return ''.join(res)
+ return "".join(res)
-def uint_to_roun(uint_val, separator='-'):
+def uint_to_roun(uint_val, separator="-"):
"""Convert 32-bit integer value into corresponding roun string identifier.
>>> uint_to_roun(0x7F000001, '-')
@@ -57,10 +59,8 @@ def uint_to_roun(uint_val, separator='-'):
:return: roun string identifier
"""
if uint_val < 0 or uint_val > 0xFFFFFFFF:
- raise ValueError('uint_val should be in range 0-0xFFFFFFFF')
- return _uint16_to_roun(uint_val >> 16) + \
- separator + \
- _uint16_to_roun(uint_val)
+ raise ValueError("uint_val should be in range 0-0xFFFFFFFF")
+ return _uint16_to_roun(uint_val >> 16) + separator + _uint16_to_roun(uint_val)
def roun_to_uint(roun):
@@ -74,7 +74,7 @@ def roun_to_uint(roun):
"""
nchar = len(roun)
if nchar < 10 or nchar > 11:
- raise ValueError('roun should be in form of two rounets + optional separator')
+ raise ValueError("roun should be in form of two rounets + optional separator")
res = 0
for i, c in enumerate(roun):
@@ -88,64 +88,71 @@ def roun_to_uint(roun):
res <<= 2
res += mag
elif i != 5:
- raise ValueError('bad roun format')
+ raise ValueError("bad roun format")
return res
+
def ip2uint_str(ipv4_str):
"""Convert IPv4 string to 32-bit integer value"""
- parts = ipv4_str.split('.')
+ parts = ipv4_str.split(".")
if len(parts) != 4:
- raise ValueError('Expected IPv4 address in form A.B.C.D, got {}'.
- format(ipv4_str))
- ip = [0]*4
+ raise ValueError(
+ "Expected IPv4 address in form A.B.C.D, got {}".format(ipv4_str)
+ )
+ ip = [0] * 4
for i, part in enumerate(parts):
try:
int_part = int(part)
except ValueError:
- raise ValueError('Part {} of IPv4 address is not an integer'.
- format(i))
+ raise ValueError("Part {} of IPv4 address is not an integer".format(i))
if int_part < 0 or int_part > 255:
- raise ValueError('Part {} of IPv4 address is not in range 0-255'.
- format(i))
+ raise ValueError("Part {} of IPv4 address is not in range 0-255".format(i))
ip[i] = int_part
return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]
+
def uint_to_ip_str(uint_val):
"Covert 32-bit integer value to IPv4 string"
- return '{}.{}.{}.{}'.format(
- (uint_val >> 24)&0xFF,
- (uint_val >> 16)&0xFF,
- (uint_val >> 8)&0xFF,
- uint_val&0xFF)
+ return "{}.{}.{}.{}".format(
+ (uint_val >> 24) & 0xFF,
+ (uint_val >> 16) & 0xFF,
+ (uint_val >> 8) & 0xFF,
+ uint_val & 0xFF,
+ )
+
-def uint_to_roun_str(uint_str, separator='-'):
+def uint_to_roun_str(uint_str, separator="-"):
return uint_to_roun(int(uint_str), separator)
+
def roun_to_uint_str(roun):
return str(roun_to_uint(roun))
-def hex2roun_str(hex_str, separator='-'):
+
+def hex2roun_str(hex_str, separator="-"):
return uint_to_roun(int(hex_str, 16), separator)
+
def roun2hex_str(roun):
return hex(roun_to_uint(roun))
+
def convert(str_val, target=None):
"""Convert between roun, integer, hex or IPv4 string representations.
Tries to guess the representation from input.
:param str_val: input representation (string)
:return: output representation (string)
"""
- if target is not None and target not in {'uint', 'hex', 'ip'}:
- raise ValueError('Convert target should be one of: uint, hex, ip')
+ if target is not None and target not in {"uint", "hex", "ip"}:
+ raise ValueError("Convert target should be one of: uint, hex, ip")
- if target == 'uint':
+ if target == "uint":
return roun_to_uint_str(str_val)
- if target == 'hex':
+ if target == "hex":
return roun2hex_str(str_val)
- if target == 'ip':
+ if target == "ip":
return uint_to_ip_str(roun_to_uint(str_val))
# try to guess the representation
@@ -169,31 +176,52 @@ def convert(str_val, target=None):
except ValueError:
pass
- raise ValueError('Unrecognized input format: {}'.format(str_val))
+ raise ValueError("Unrecognized input format: {}".format(str_val))
+
-if __name__ == '__main__':
+if __name__ == "__main__":
parser = argparse.ArgumentParser(
- description=DESC,
- formatter_class=argparse.RawDescriptionHelpFormatter)
- parser.add_argument('-n', '--uint', action='store_true',
- help='convert from roun to 32-bit integer', required=False)
- parser.add_argument('-x', '--hex', action='store_true',
- help='convert from roun to hexadecimal', required=False)
- parser.add_argument('-i', '--ip', action='store_true',
- help='convert from roun to IPv4', required=False)
- parser.add_argument('val', nargs='?', type=str, default=None,
- help='value to convert (if not specified, ' \
- 'IP address of the current host is printed)')
+ description=DESC, formatter_class=argparse.RawDescriptionHelpFormatter
+ )
+ parser.add_argument(
+ "-n",
+ "--uint",
+ action="store_true",
+ help="convert from roun to 32-bit integer",
+ required=False,
+ )
+ parser.add_argument(
+ "-x",
+ "--hex",
+ action="store_true",
+ help="convert from roun to hexadecimal",
+ required=False,
+ )
+ parser.add_argument(
+ "-i",
+ "--ip",
+ action="store_true",
+ help="convert from roun to IPv4",
+ required=False,
+ )
+ parser.add_argument(
+ "val",
+ nargs="?",
+ type=str,
+ default=None,
+ help="value to convert (if not specified, "
+ "IP address of the current host is printed)",
+ )
args = parser.parse_args()
target = None
if args.uint:
- target = 'uint'
+ target = "uint"
elif args.hex:
- target = 'hex'
+ target = "hex"
elif args.ip:
- target = 'ip'
+ target = "ip"
res = convert(args.val, target)
- print('{}'.format(res))
+ print("{}".format(res))