def channel_getter_factory(cls, name, ch_cls): """ Factory function returning custom builder for channel instances. The factory function is bound to the calling class. Parameters ---------- cls : type Class to which bind the channel getter method. name : unicode Name of the channel, used for caching and naming purposes. ch_cls : type Class of the channel used for instantiation. Return ------ Bound """ def channel_getter(self, ch_id): return self._generic_get_channel(name, ch_cls, ch_id) f_name = 'get_' + name if not hasattr(cls, f_name): # In Python 2 needs the cast as we use unicode_litterals channel_getter.__name__ = str(f_name) bind_method(cls, f_name, channel_getter)
def __init__( self, targetFileName, file_start_offset=0, loading_address=0, pointer_size=4, endianity='='): self._file = io.open(targetFileName, 'rb+') MemReaderBase.__init__(self) self._POINTER_SIZE = pointer_size self._DEFAULT_DATA_SIZE = 4 self._ENDIANITY = endianity self._START = file_start_offset self._LOADING_ADDR = loading_address self._ADDR_DELTA = file_start_offset - loading_address # Find end of file self._file.seek(0, 2) self._file_size = self._file.tell() for readerName, (dataSize, packer) in MemReaderInterface.READER_DESC.items(): def readerCreator(dataSize, packer): def readerMethod(self, address): self._file.seek(address + self._ADDR_DELTA) return struct.unpack(self._ENDIANITY + packer, bytes(self._file.read(dataSize)))[0] return readerMethod def writerCreator(dataSize, packer): def writerMethod(self, address, value): self._file.seek(address + self._ADDR_DELTA) if isinstance(value, integer_types): data = pack(self._ENDIANITY + packer, value) self._file.write(value) return writerMethod bind_method(FileReader, 'read' + readerName, readerCreator(dataSize, packer)) bind_method(FileReader, 'write' + readerName, writerCreator(dataSize, packer))
def __init__(self): for readerName, (dataSize, packer) in MemReaderInterface.READER_DESC.items(): def readerCreator(dataSize, packer): def readerMethod(self, address): return struct.unpack(self._ENDIANITY + packer, self.readMemory(address, dataSize))[0] return readerMethod bind_method(MemReaderBase, 'read' + readerName, readerCreator(dataSize, packer))
if not hasattr(unittest.TestCase, 'assertRaisesRegex'): unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp # From Py3.3: def assertRegex(self, text, expected_regex, msg=None): """Fail the test unless the text matches the regular expression.""" if isinstance(expected_regex, (str, unicode)): assert expected_regex, "expected_regex must not be empty." expected_regex = re.compile(expected_regex) if not expected_regex.search(text): msg = msg or "Regex didn't match" msg = '%s: %r not found in %r' % (msg, expected_regex.pattern, text) raise self.failureException(msg) if not hasattr(unittest.TestCase, 'assertRegex'): bind_method(unittest.TestCase, 'assertRegex', assertRegex) class _AssertRaisesBaseContext(object): def __init__(self, expected, test_case, callable_obj=None, expected_regex=None): self.expected = expected self.test_case = test_case if callable_obj is not None: try: self.obj_name = callable_obj.__name__ except AttributeError: self.obj_name = str(callable_obj) else: self.obj_name = None if isinstance(expected_regex, (bytes, str)):
def freeze_method_kwargs(klass, method_name, **kwargs): method = getattr(klass, method_name) partialfunc = functools.partialmethod if PY3 else functools.partial bind_method(klass, method_name, partialfunc(method, **kwargs))
# From Py3.3: def assertRegex(self, text, expected_regex, msg=None): """Fail the test unless the text matches the regular expression.""" if isinstance(expected_regex, (str, unicode)): assert expected_regex, "expected_regex must not be empty." expected_regex = re.compile(expected_regex) if not expected_regex.search(text): msg = msg or "Regex didn't match" msg = '%s: %r not found in %r' % (msg, expected_regex.pattern, text) raise self.failureException(msg) if not hasattr(unittest.TestCase, 'assertRegex'): bind_method(unittest.TestCase, 'assertRegex', assertRegex) class _AssertRaisesBaseContext(object): def __init__(self, expected, test_case, callable_obj=None, expected_regex=None): self.expected = expected self.test_case = test_case if callable_obj is not None: try: self.obj_name = callable_obj.__name__ except AttributeError: self.obj_name = str(callable_obj)
# From Py3.3: def assertRegex(self, text, expected_regex, msg=None): """Fail the test unless the text matches the regular expression.""" if isinstance(expected_regex, (str, unicode)): assert expected_regex, "expected_regex must not be empty." expected_regex = re.compile(expected_regex) if not expected_regex.search(text): msg = msg or "Regex didn't match" msg = "%s: %r not found in %r" % (msg, expected_regex.pattern, text) raise self.failureException(msg) if not hasattr(unittest.TestCase, "assertRegex"): bind_method(unittest.TestCase, "assertRegex", assertRegex) class _AssertRaisesBaseContext(object): def __init__(self, expected, test_case, callable_obj=None, expected_regex=None): self.expected = expected self.test_case = test_case if callable_obj is not None: try: self.obj_name = callable_obj.__name__ except AttributeError: self.obj_name = str(callable_obj)
def update_members_from_preferences(self, parameters): """ Use the string values given in the parameters to update the members This function will call itself on any tagged HasPrefAtom member. """ for name, member in tagged_members(self, 'pref').items(): if name not in parameters or isinstance(member, Constant): continue old_val = getattr(self, name) if issubclass(type(old_val), HasPrefAtom): old_val.update_members_from_preferences(parameters[name]) # This is meant to prevent updating fields which expect a custom # instance elif old_val is None: pass else: value = parameters[name] converted = member_from_pref(self, member, value) setattr(self, name, converted) bind_method(HasPrefAtom, 'preferences_from_members', preferences_from_members) bind_method(HasPrefAtom, 'update_members_from_preferences', update_members_from_preferences)
""" Use the string values given in the parameters to update the members This function will call itself on any tagged HasPrefAtom member. """ for name, member in tagged_members(self, 'pref').items(): if name not in parameters or isinstance(member, Constant): continue old_val = getattr(self, name) if issubclass(type(old_val), HasPrefAtom): old_val.update_members_from_preferences(parameters[name]) # This is meant to prevent updating fields which expect a custom # instance elif old_val is None: pass else: value = parameters[name] converted = member_from_pref(self, member, value) try: setattr(self, name, converted) except Exception as e: msg = 'An exception occured when trying to set {} to {}' raise_from(ValueError(msg.format(name, converted)), e) bind_method(HasPrefAtom, 'preferences_from_members', preferences_from_members) bind_method(HasPrefAtom, 'update_members_from_preferences', update_members_from_preferences)