[chirp_devel] [PATCH 2 of 4] Add FileWrapperMixin for pulling img data out of 3rd party formats. #3755

Tom Hayward
Fri Sep 16 16:23:38 PDT 2016


# HG changeset patch
# User Tom Hayward <tom at tomh.us>
# Date 1474068137 25200
#      Fri Sep 16 16:22:17 2016 -0700
# Node ID 45cfb92ddc164dbe01354f43cb68cd970c3eb6f3
# Parent  c55eeb0b9a3c898ab1a853272404ab247cccfa0f
Add FileWrapperMixin for pulling img data out of 3rd party formats. #3755

diff -r c55eeb0b9a3c -r 45cfb92ddc16 chirp/chirp_common.py
--- a/chirp/chirp_common.py	Fri Sep 16 16:21:51 2016 -0700
+++ b/chirp/chirp_common.py	Fri Sep 16 16:22:17 2016 -0700
@@ -1139,6 +1139,33 @@
         return self._mmap
 
 
+class FileWrapperMixin:
+    def load_mmap(self, filename):
+        """Load the radio's memory map from @filename"""
+        mapfile = file(filename, "rb")
+        self._head = mapfile.read(self._headsize)
+        self._mmap = memmap.MemoryMap(mapfile.read(self._mmapsize))
+        self._tail = mapfile.read()
+        if len(self._tail) != self._tailsize:
+            raise errors.RadioError("File wrapper is wrong length.")
+        mapfile.close()
+        self.process_mmap()
+
+    def save_mmap(self, filename):
+        """
+        try to open a file and write to it
+        If IOError raise a File Access Error Exception
+        """
+        try:
+            mapfile = file(filename, "wb")
+            mapfile.write(self._head)
+            mapfile.write(self._mmap.get_packed())
+            mapfile.write(self._tail)
+            mapfile.close()
+        except IOError:
+            raise Exception("File Access Error")
+
+
 class CloneModeRadio(FileBackedRadio):
     """A clone-mode radio does a full memory dump in and out and we store
     an image of the radio into an image file"""



More information about the chirp_devel mailing list