From ec45c6888da9ac3abbea25454bf7e70487de888a Mon Sep 17 00:00:00 2001 From: Sascha Silbe Date: Fri, 10 Mar 2017 14:27:17 +0000 Subject: Adapt to python-sane 2.8.x API changes python-sane 2.8.x doesn't expose the constants for types and units anymore. Instead we'd need to look up the value against a dictionary (sane.TYPE_STR resp. sane.UNIT_STR) and perform a string comparison. For compatibility with python-sane < 2.8, we continue doing integer comparison. When running with python-sane 2.8+, we import an inverted version of the dictionary into the global namespace. For python-sane < 2.8, we then just import the constants into the global namespace. --- diff --git a/scan.py b/scan.py index 9398f2f..681ede8 100644 --- a/scan.py +++ b/scan.py @@ -49,6 +49,15 @@ from sugar.logger import trace import postproc +if hasattr(sane, 'TYPE_STR'): + # python-sane 2.8+ + for value, name in sane.TYPE_STR.items(): + globals()[name] = value +else: + from sane import TYPE_BOOL, TYPE_BUTTON, TYPE_FIXED, TYPE_GROUP, TYPE_INT + # python-sane < 2.8 + + class SettingsToolbar(gtk.ToolPalette): @trace() @@ -134,13 +143,14 @@ class SettingsToolbar(gtk.ToolPalette): groups = [(None, group)] options = self._scanner.get_options() for idx, name, title, dsc_, _type, unit_, siz_, cap_, cons_ in options: - if _type == sane.TYPE_GROUP: + if _type == TYPE_GROUP: group = [] groups.append((title, group)) continue - if not name and idx != 0: - logging.warning('ignoring unnamed option (index %d)', idx) + if not name: + if idx != 0: + logging.warning('ignoring unnamed option (index %d)', idx) continue group.append(name) @@ -154,6 +164,7 @@ class SettingsToolbar(gtk.ToolPalette): for py_name in self._scanner.optlist]) first_group = True + print(repr(groups)) for group_name, group in groups: if not group: continue @@ -171,15 +182,15 @@ class SettingsToolbar(gtk.ToolPalette): py_name = name_map[option_name] option = self._scanner[py_name] - if (option.type == sane.TYPE_BOOL): + if (option.type == TYPE_BOOL): widget = self._add_bool(option) - elif (option.type == sane.TYPE_INT): + elif (option.type == TYPE_INT): widget = self._add_integer(option) - elif (option.type == sane.TYPE_FIXED): + elif (option.type == TYPE_FIXED): widget = self._add_fixed_point(option) - elif (option.type == sane.TYPE_STRING): + elif (option.type == TYPE_STRING): widget = self._add_string(option) - elif (option.type == sane.TYPE_BUTTON): + elif (option.type == TYPE_BUTTON): widget = self._add_action(option) else: logging.warning('Skipping setting %r of unknown type %r', @@ -279,7 +290,7 @@ class SettingsToolbar(gtk.ToolPalette): spin.props.snap_to_ticks = True spin.set_digits(self._calc_digits(step)) - if option.type == sane.TYPE_INT: + if option.type == TYPE_INT: spin.connect('value-changed', lambda widget, option=option: self._spin_int_changed_cb(widget, option)) else: @@ -671,8 +682,8 @@ class ScanActivity(activity.Activity): for py_name in self._current_scanner.optlist: logging.debug('getting value for setting %r', py_name) option = self._current_scanner[py_name] - if option.type in [sane.TYPE_BOOL, sane.TYPE_INT, - sane.TYPE_FIXED, sane.TYPE_STRING] and option.is_active(): + if (option.type in [TYPE_BOOL, TYPE_INT, TYPE_FIXED, TYPE_STRING] + and option.is_active()): settings[py_name] = getattr(self._current_scanner, py_name) -- cgit v0.9.1