# Path: lib/multibase.py
# class MultiBase(object):
#     """
#     """
#     def __init__(self, target, *args):
#         """
#         """
#         self.s = RTorrentXMLRPC(target)
#         self.m = xmlrpc.client.MultiCall(self.s)
#         self.target = target['name']
# 
#         # Stack to put commands on
#         self._groups = [[]]
# 
#         # We keep these purely for hash functionality
#         self._groups_args = [[]]
#         self._def_group_args = args
# 
#     def __hash__(self):
#         h = 42 ^ hash(self.target)
#         for y in zip(self._groups, self._groups_args):
#             for x in zip(y[0], y[1]):
#                 h ^= hash(x[0])
#                 for z in x[1]:
#                     h ^= hash(z)
# 
#         return h
# 
#     def __call__(self, attr, *args):
#         """
#         Add the attribute ``attr'' to the list we want to fetch.
# 
#         Return self so we can chain calls.
#         """
#         _attr = self._convert_command(attr)
# 
#         total_args = list(self._def_group_args)
#         total_args.extend(args)
#         getattr(self.m, _attr)(*total_args)
#         self._groups[-1].append(attr)
#         self._groups_args[-1].append(total_args)
# 
#         return self
# 
#     def __getattr__(self, attr):
#         """
#         Used to add commands.
#         """
#         return lambda *args: self(attr, *args) # calls __call__
# 
#     def new_group(self, *args):
#         """
#         Use this to create a new group. You can chain calls as well.
#         """
#         self._groups.append([])
#         self._groups_args.append([])
#         self._def_group_args = args
#         return self
# 
#     def all(self, _type=None):
#         """
#         Returns a list of the results.
# 
#         _type can be 'list' or AttributeDictMultiResult.
#         """
#         if _type is None:
#             _type = AttributeDictMultiResult
#         if _type not in (AttributeDictMultiResult, list):
#             raise InvalidTorrentCommandException('Invalid _type: %s' %
#                     str(_type))
# 
#         try:
#             xmlres = list(self.m())
#         except xmlrpc.client.Fault as e:
#             raise InvalidTorrentException(e)
#         except socket.error as s:
#             raise InvalidConnectionException(s)
# 
#         if _type is list:
#             self._flush()
#             return xmlres
# 
#         xmlres.reverse()
# 
#         result = []
#         for group in self._groups:
#             res = []
#             for command in group:
#                 res.append(xmlres.pop())
# 
#             result.append(AttributeDictMultiResult(list(zip(group, res))))
# 
#         self._flush()
# 
#         return result
# 
#     def first(self, _type=None):
#         """
#         Return the first result.
#         """
#         res = self.all(_type)
#         if len(res):
#             return res[0]
#         else:
#             return None
# 
#     def _flush(self):
#         pass
# 
#     def _convert_command(self, command):
#         """
#         Convert command based on self._rpc_methods to rtorrent command.
#         """
#         if command in self._rpc_methods:
#             return self._rpc_methods[command][0]
#         else:
#             raise InvalidTorrentCommandException("%s is not a valid command" %
#                     command)
# 
# Path: model/rtorrent.py
# class RTorrent(object):
#     def __init__(self, target):
#     def hacks(self):
#     def __repr__(self):
#     def get_download_list(self, _type=''):
#     def query(self):


from lib.multibase import MultiBase
from model import rtorrent

.. _rtorrentquery-class:

RTorrentQuery
=============

The RTorrent query class can be used to send multiple queries over one
XMLRPC command, thus heavily decreasing loading times.

It is created in RTorrent.query() or can be manually created using the
RTorrentQuery() constructor.

It extends the MultiBase class, which is used in all the *Query classes.
Head over to :ref:`multibase-class` to find more about the general *Query classes.

.. code-block:: python

    # Create a RTorrent class (or use an existing one)
    r = RTorrent(host, port, url)

    # Create the query.
    rq = r.query().get_upload_rate().get_download_rate(\
).get_librtorrent_version()

    res = rq.first()

    print res.get_upload_rate # Note that this is an attribute.

"""


# Path: lib/multibase.py
# class MultiBase(object):
#     """
#     """
#     def __init__(self, target, *args):
#         """
#         """
#         self.s = RTorrentXMLRPC(target)
#         self.m = xmlrpc.client.MultiCall(self.s)
#         self.target = target['name']
# 
#         # Stack to put commands on
#         self._groups = [[]]
# 
#         # We keep these purely for hash functionality
#         self._groups_args = [[]]
#         self._def_group_args = args
# 
#     def __hash__(self):
#         h = 42 ^ hash(self.target)
#         for y in zip(self._groups, self._groups_args):
#             for x in zip(y[0], y[1]):
#                 h ^= hash(x[0])
#                 for z in x[1]:
#                     h ^= hash(z)
# 
#         return h
# 
#     def __call__(self, attr, *args):
#         """
#         Add the attribute ``attr'' to the list we want to fetch.
# 
#         Return self so we can chain calls.
#         """
#         _attr = self._convert_command(attr)
# 
#         total_args = list(self._def_group_args)
#         total_args.extend(args)
#         getattr(self.m, _attr)(*total_args)
#         self._groups[-1].append(attr)
#         self._groups_args[-1].append(total_args)
# 
#         return self
# 
#     def __getattr__(self, attr):
#         """
#         Used to add commands.
#         """
#         return lambda *args: self(attr, *args) # calls __call__
# 
#     def new_group(self, *args):
#         """
#         Use this to create a new group. You can chain calls as well.
#         """
#         self._groups.append([])
#         self._groups_args.append([])
#         self._def_group_args = args
#         return self
# 
#     def all(self, _type=None):
#         """
#         Returns a list of the results.
# 
#         _type can be 'list' or AttributeDictMultiResult.
#         """
#         if _type is None:
#             _type = AttributeDictMultiResult
#         if _type not in (AttributeDictMultiResult, list):
#             raise InvalidTorrentCommandException('Invalid _type: %s' %
#                     str(_type))
# 
#         try:
#             xmlres = list(self.m())
#         except xmlrpc.client.Fault as e:
#             raise InvalidTorrentException(e)
#         except socket.error as s:
#             raise InvalidConnectionException(s)
# 
#         if _type is list:
#             self._flush()
#             return xmlres
# 
#         xmlres.reverse()
# 
#         result = []
#         for group in self._groups:
#             res = []
#             for command in group:
#                 res.append(xmlres.pop())
# 
#             result.append(AttributeDictMultiResult(list(zip(group, res))))
# 
#         self._flush()
# 
#         return result
# 
#     def first(self, _type=None):
#         """
#         Return the first result.
#         """
#         res = self.all(_type)
#         if len(res):
#             return res[0]
#         else:
#             return None
# 
#     def _flush(self):
#         pass
# 
#     def _convert_command(self, command):
#         """
#         Convert command based on self._rpc_methods to rtorrent command.
#         """
#         if command in self._rpc_methods:
#             return self._rpc_methods[command][0]
#         else:
#             raise InvalidTorrentCommandException("%s is not a valid command" %
#                     command)
# 
# Path: model/rtorrent.py
# class RTorrent(object):
#     def __init__(self, target):
#     def hacks(self):
#     def __repr__(self):
#     def get_download_list(self, _type=''):
#     def query(self):


# Path: lib/rtorrentquery.py
from lib.multibase import MultiBase
from model import rtorrent


.. _rtorrentquery-class:

RTorrentQuery
=============

The RTorrent query class can be used to send multiple queries over one
XMLRPC command, thus heavily decreasing loading times.

It is created in RTorrent.query() or can be manually created using the
RTorrentQuery() constructor.

It extends the MultiBase class, which is used in all the *Query classes.
Head over to :ref:`multibase-class` to find more about the general *Query classes.

.. code-block:: python

    # Create a RTorrent class (or use an existing one)
    r = RTorrent(host, port, url)

    # Create the query.
    rq = r.query().get_upload_rate().get_download_rate(\
).get_librtorrent_version()

    res = rq.first()

    print res.get_upload_rate # Note that this is an attribute.

"""


class RTorrentQuery(MultiBase):