From e9556c1fbeaf333cc5cbdecf7d9750d2a31bb12a Mon Sep 17 00:00:00 2001 From: Martinez Date: Mon, 9 Oct 2017 23:51:38 +0200 Subject: [PATCH] - Options use a more robust int callback by default (handles hex and oct digits as well) - over.misc.hexdump extended with initial_offset --- over/app.py | 4 ++++ over/callback.py | 21 +++++++++++++++++++++ over/misc.py | 4 ++-- over/version.py | 4 ++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/over/app.py b/over/app.py index 98ff259..65fab46 100644 --- a/over/app.py +++ b/over/app.py @@ -126,6 +126,10 @@ class Option: else: self.is_boolean = is_boolean + # coerce a few Python types to our own + if callback == int: + self.callback = callback_module.integer + self.reset(True) # sets self.source and self._value_lists def reset(self, default=False): diff --git a/over/callback.py b/over/callback.py index aa55678..591af05 100644 --- a/over/callback.py +++ b/over/callback.py @@ -78,3 +78,24 @@ def directory(exists=False, writable=False, gio=False): return path return cb + +def integer(arg): + """ + Converts + - "0x2a" ⇒ 42 + - "0o52" ⇒ 42 + - "42" ⇒ 42 + + @while converting argument to int + """ + + if type(arg) == int: + return arg + + if len(arg) > 2: + if arg[1] == "x": + return int(arg, 16) + elif arg[1] == "o": + return int(arg, 8) + + return int(arg) diff --git a/over/misc.py b/over/misc.py index c7ff740..c855790 100644 --- a/over/misc.py +++ b/over/misc.py @@ -110,7 +110,7 @@ def debugger(): # -------------------------------------------------- -def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show_ascii=True, use_colors=True, output=sys.stdout): +def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show_ascii=True, use_colors=True, initial_offset=0, output=sys.stdout): """ Writes a hex dump of `data` to `output`. @@ -156,7 +156,7 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show output_io.write(" " * indent) if show_offsets: - output_io.write(format_str %(ptr)) + output_io.write(format_str %(initial_offset + ptr)) hex_bytes = [] ascii_bytes = [] diff --git a/over/version.py b/over/version.py index bc4a5b0..3c87226 100644 --- a/over/version.py +++ b/over/version.py @@ -4,5 +4,5 @@ major = 1 # VERSION_MAJOR_IDENTIFIER minor = 101 # VERSION_MINOR_IDENTIFIER # VERSION_LAST_MM 1.101 -patch = 1 # VERSION_PATCH_IDENTIFIER -str = "1.101.1" # VERSION_STRING_IDENTIFIER +patch = 2 # VERSION_PATCH_IDENTIFIER +str = "1.101.2" # VERSION_STRING_IDENTIFIER