Skip to main content

mgtdisklib

A Python library to access to the contents of SAM Coupé and MGT +D disk images.

NOTE: the libary API may not be completely stable before version 1.0.

Homepage: https://github.com/simonowen/mgtdisklib
Module: https://pypi.org/project/mgtdisklib/

CI


Using the library

Installing the module

python -m pip install mgtdisklib

Importing the module

from mgtdisklib import Disk, Image, File

Opening a disk image

disk = Disk.open('image.mgt')

MGT/SAD/EDSK container files are supported, but only those containing a regular 80/2/10/512 or 80/2/9/512 format. The image file may be optionally compressed with gzip.

Saving the Disk contents to a new MGT image file

disk.save('image2.mgt')

Disk

Represents a logical SAM disk plus all its contents.

Disk Types

DiskType is one of the following values:

DiskType.SAMDOS         # SAMDOS (default).
DiskType.MASTERDOS      # MasterDOS.
DiskType.BDOS           # BDOS, used by Atom and Atom Lite.

Disk Class Functions

    def open(path: str) -> Disk:
        """Load disk from disk image file"""
    def from_image(image: Image) -> Disk:
        """Construct a Disk object from a disk image"""

Disk Instance Functions

    def save(self, path: str, *, compressed: bool = False) -> None:
        """Save disk content to disk image"""
    def to_image(self) -> Image:
        """Generate MGT disk image from current contents"""
    def add_code_file(self, path: str, *, filename: str | None = None, at_index: int | None = None) -> None:
        """Add CODE file from path"""
    def add_code_bytes(self, data: bytes, *, filename: str, at_index: int | None = None) -> None:
        """Add CODE file from bytes"""
    def delete(self, pattern: str) -> list[str]:
        """Delete files matching filename pattern, returns list of deleted names"""
    def dir(self) -> str:
        """Return directory listing"""

Disk Instance Properties

  • type - disk type (DiskType)
  • files - array of File objects in directory order (File[])
  • dir_tracks - number of directory tracks (usually 4) (int)
  • label - disk volume label string (str | None)
  • serial - MasterDOS unique disk number (int | None)
  • compressed - True if the source disk or image was gzipped (bool)
  • bootable - True if the disk is bootable (bool) [read-only]
  • sector_map - combined Bitmap Address Map for all files (bitarray) [read-only]

File

Represents a single file entry on a disk.

File Types

FileType is one of the following values:

FileType.NONE           # unused or deleted entry
FileType.ZX_BASIC       # ZX Spectrum BASIC
FileType.ZX_DATA        # ZX Spectrum numeric array
FileType.ZX_DATA_STR    # ZX Spectum string array
FileType.ZX_CODE        # ZX Spectrum code
FileType.ZX_SNP_48K     # ZX Spectrum 48K snapshot
FileType.ZX_MDRV        # ZX Spectrum microdrive file
FileType.ZX_SCREEN      # ZX Spectrum SCREEN$
FileType.SPECIAL        # Custom file entry
FileType.ZX_SNP_128K    # ZX Spectrum 128K snapshot
FileType.OPENTYPE       # ZX/SAM file stream
FileType.ZX_EXECUTE     # ZX interface executable
FileType.UNIDOS_DIR
FileType.UNIDOS_CREATE  
FileType.BASIC          # SAM Coupé BASIC
FileType.DATA           # SAM Coupé numeric array
FileType.DATA_STR       # SAM Coupé string array
FileType.CODE           # SAM Coupé code
FileType.SCREEN         # SAM Coupé SCREEN (mode 1-4)
FileType.DIR            # SAM Coupé MasterDOS directory
FileType.DRIVER_APP     # Driver application
FileType.DRIVER_BOOT    # Driver boot file
FileType.EDOS_NOMEN     # Entropy IDE DOS (abandoned)
FileType.EDOS_SYSTEM
FileType.EDOS_OVERLAY
FileType.HDOS_DOS       # SD IDE DOS
FileType.HDOS_DIR
FileType.HDOS_DISK
FileType.HDOS_TEMP

TimeFormat is one of the following values:

TimeFormat.MASTERDOS    # Format used by MasterDOS.
TimeFormat.BDOS         # Format used by most BDOS and AL-BDOS versions.
TimeFormat.BDOS17       # Packed format for used by BDOS 1.7 or later.

File Class Functions

    def from_code_path(path: str, *, filename: str = None, start: int = 0x8000, execute: int = None) -> File:
        """Create CODE file from path"""
    def from_code_bytes(data: bytes, filename: str, *, start: int = 0x8000, execute: int = None) -> File:
        """Create CODE file from bytes"""
    def from_dir(data: bytes) -> File:
        """Create from 256-byte directory entry data"""
    def from_path(path: str) -> File:
        """Import file entry exported using save()"""

File Instance Functions

    def save(self, path: str) -> None:
        """Export directory entry and file content for later"""
    def to_dir(self, disk_map: bitarray | None = None, timefmt: TimeFormat = TimeFormat.MASTERDOS) -> tuple[bytes, bitarray]:
        """Create directory entry, allocate sectors, return (entry, updated disk_map)"""

File Instance Properties

  • type - file type (FileType)
  • hidden - True if file is hidden from SAM directory listing (bool)
  • protected - True if file is protected from deletion (bool)
  • name - file name in ASCII without trailing spaces (str)
  • name_raw - original 10-byte name, which could contain special characters (bytes)
  • sectors - count of data sectors used (int) [read-only]
  • first_sector - first data (track, sector) tuple (tuple[int, int] | None) [read-only]
  • sector_map - bitmap of sectors used by this file, starting at track 4 sector 1 (bitarray) [read-only]
  • start - file start address (int | None)
  • length - file length in bytes (int) [read-only]
  • execute - auto-execute line (BASIC) or address (CODE) (int | None)
  • time - file date+time (datetime | None)
  • data_var - variable name for numeric/string DATA types (str | None)
  • entry - original 256-byte directory entry (bytes)
  • bootable - True if bootable in the first directory slot (bool) [read-only]
  • data - file data (bytes)

Properties marked [read-only] are derived from the file data. first_sector and sector_map are updated when a disk image is created containing the file.


Image

Represents a disk image container in MGT/SAD/EDSK format.

Image Class Functions

    def open(path):
        """Create Image object from disk image file"""

Creating an Image() object will give a standard 80/2/10/512 MGT disk image.

Image Instance Functions

    def save(self, path: str, *, compressed: bool = False) -> None:
        """Save disk data to image file"""
    def read_sector(self, track: int, sector: int) -> bytes:
        """Read sector data for given sector location"""
    def write_sector(self, track: int, sector: int, data: bytes) -> None:
        """Write sector data to given sector location"""
    def sector_offset(self, track: int, sector: int) -> int:
        """Calculate sector data offset in image data"""

MGT tracks are numbered are 0-79 for the first side and 128-207 for the second. Sectors are numbered 1-10, each being 512 bytes. Only regular 10-sector disk images are supported.

The first 4 tracks of the first side contain the disk directory, and the remainder of the disk holds data. The second side of the disk is only used once the first side is full. Track 4 sector 1 holds the boot sector.

MasterDOS disks can be formatted to use up to 39 directory tracks, allowing up to 778 files to be stored. This is 2 less than expected because the boot sector remains in the same place for all disks.

Image Instance Properties

  • path - full path of the disk image (str | None)
  • compressed - True if the source image was gzipped (bool)
  • data - raw disk data from image file (bytearray)

Metadata

Release files for mgtdisklib 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mgtdisklib 0.6.0
File Size Uploaded
mgtdisklib-0.6.0.tar.gz 33.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mgtdisklib 0.6.0
File Interpreter ABI Platform
mgtdisklib-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 54.9 kB

Release files / mgtdisklib-0.6.0.tar.gz

Download URL mgtdisklib-0.6.0.tar.gz
Size 33.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f97d65e9e5db41b4991c76a05dac9d40eb6ac566221b52360a5deb4e251d1c37
BLAKE2b-256 checksum
How to use checksums
3c5f39467da6e4946fb003278cfafc8b47853342a0267ec05358febec7b1f81d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / mgtdisklib-0.6.0-py3-none-any.whl

Download URL mgtdisklib-0.6.0-py3-none-any.whl
Size 21.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fae0d552a528177796b3773beeb117fa4f56b42a58064d5504958c23fca38ee7
BLAKE2b-256 checksum
How to use checksums
ad5ddea26124188b995200c57213603037e6fbccb59cc526a5364a84757a258b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.15 {"installer":{"name":"uv","version":"0.9.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page