Skip to content

Python development

Joachim Metz edited this page Jul 15, 2022 · 1 revision

libmodi comes with Python-bindings named pymodi.

Below are examples how use pymodi. They assume you have a working version of pymodi on your system. To build pymodi see Building.

Import

To be able to use pymodi in your Python scripts add the following import:

import pymodi

Get version

The get_version() module function can be used to retrieve the version of the pymodi.

pymodi.get_version()

This will return a textual string (Unicode) that contains the libmodi version. Since pymodi is a wrapper around libmodi it does not have a separate version.

Open handle

Open a handle by path

modi_handle = pymodi.handle()

modi_handle.open("image.sparseimage")

...

modi_handle.close()

The explicit call to modi_handle.close() is not required. Close only must be called once all operations on the handle have been completed.

Open a handle using a file-like object

file_object = open("image.sparseimage", "rb")

modi_handle = pymodi.handle()

modi_handle.open_file_object(file_object)

...

modi_handle.close()

The explicit call to modi_handle.close() is not required. Close only must be called once all operations on the handle have been completed and will not close the file-like object itself.

Examples

Combining pymodi with pytsk3

The following additional import is required:

import pytsk3
class modi_Img_Info(pytsk3.Img_Info):
  def __init__(self, modi_handle):
    self._modi_handle = modi_handle
    super(modi_Img_Info, self).__init__(
        url="", type=pytsk3.TSK_IMG_TYPE_EXTERNAL)

  def close(self):
    self._modi_handle.close()

  def read(self, offset, size):
    self._modi_handle.seek(offset)
    return self._modi_handle.read(size)

  def get_size(self):
    return self._modi_handle.get_media_size()


modi_handle = pymodi.handle()

modi_handle.open("image.sparseimage")

img_info = modi_Img_Info(modi_handle)

fs_info = pytsk3.FS_Info(img_info, offset=63 * 512)

Also see

import pymodi

help(pymodi)
help(pymodi.handle)

Clone this wiki locally