API

Messages

class dime.Message(records)
Parameters:records – iterable of Record objects
static load(stream)
Parameters:stream – binary file (or file-like) object
Returns:Message object

Load a message from the stream.

save(stream)
Parameters:stream – binary file (or file-like) object

Save the message to the stream.

as_mime()
Returns:email.mime.multipart.MIMEMultipart object

Records

class dime.Record(id=<random-uuid>, type=UnknownType(), data='', mb=0, me=0, cf=0, version=1)
Parameters:
  • id – unique identifier in the form of URI; the default is to assign a random UUID
  • type – payload type; the default is UnknownType
  • data – payload
  • mb – Message Begin flag
  • me – Message End flag
  • cf – Chunk Flag
  • version – DIME version
static load(stream)
Parameters:stream – binary file (or file-like) object
Returns:Record object

Load a records from the stream.

static load_all(stream)
Parameters:stream – binary file (or file-like) object
Returns:generator of Record objects

Load a series of records from the stream.

save(stream)
Parameters:stream – binary file (or file-like) object

Save the record to the stream.

as_mime()
Returns:email.MIMEBase.MIMEBase object
exception Record.FaultyRecord
exception Record.UnsupportedVersion

Payload types

class dime.Type

This is an abstract class. Don’t instantiate or subclass it. Use one of the existing subclasses described below instead.

as_mime()
Returns:email.MIMEBase.MIMEBase object
class dime.UnchangedType

This payload type must be used in all middle record chunks and terminating record chunks used in chunked payloads. It must not be used in any other record.

class dime.MediaType(media_type)

These payload types are identified by a MIME media type.

>>> t = dime.MediaType('image/jpeg')
>>> str(t)
'image/jpeg'
class dime.TypeByUri(uri)

These payload types are identified by an absolute URI.

>>> t = dime.TypeByUri('http://schemas.xmlsoap.org/soap/envelope/')
>>> str(t)
'http://schemas.xmlsoap.org/soap/envelope/'
class dime.UnknownType

This class indicates that the type of the payload is unknown.

class dime.NoneType

This class indicates that there is no type or payload associated with this record.

class dime.UnsupportedType

These payload types not supported by this software. Don’t use this class to construct DIME messages.

Further reading

H. Nielsen, H. Sanders, R. Butek, S. Nash, Direct Internet Message Encapsulation (DIME), 2002

Examples

Tiny DIME producer

#!/usr/bin/env python

import argparse
import os

import dime

ap = argparse.ArgumentParser()
ap.add_argument('-o', dest='outpath', metavar='OUTPUT', required=True)
ap.add_argument('paths', metavar='FILE', nargs='+')
options = ap.parse_args()
text_type = dime.MediaType('text/plain')
records = []
for path in options.paths:
    with open(path, 'r') as file:
        data = file.read()
    ident = os.path.basename(path)
    record = dime.Record(ident, text_type, data)
    records += [record]
message = dime.Message(records)
with open(options.outpath, 'w') as file:
    message.save(file)

DIME to MIME converter

#!/usr/bin/env python

import sys

import dime

msg = dime.Message.load(sys.stdin)
print msg.as_mime()