TransmissionScripts / transmissionrpc /

# -*- coding: utf-8 -*-
# Copyright (c) 2008-2010 Erik Svensson <>
# Licensed under the MIT license.

class Session(object):
    Session is a class holding the session data for a Transmission daemon.

    Access the session field can be done through attributes.
    The attributes available are the same as the session arguments in the
    Transmission RPC specification, but with underscore instead of hypen.
    ``download-dir`` -> ``download_dir``.

    def __init__(self, fields=None):
        self.fields = {}
        if fields != None:

    def update(self, other):
        """Update the session data from a session arguments dictinary"""

        fields = None
        if isinstance(other, dict):
            fields = other
        elif isinstance(other, Session):
            fields = other.fields
            raise ValueError('Cannot update with supplied data')

        for key, value in fields.iteritems():
            self.fields[key.replace('-', '_')] = value

    def __getattr__(self, name):
            return self.fields[name]
        except KeyError:
            raise AttributeError('No attribute %s' % name)

    def __str__(self):
        text = ''
        for key in sorted(self.fields.keys()):
            text += "% 32s: %s\n" % (key[-32:], self.fields[key])
        return text