Metadata-Version: 2.4
Name: bencode.py
Version: 4.0.0
Summary: Simple bencode parser (for Python 2, Python 3 and PyPy)
Home-page: https://github.com/fuzeman/bencode.py
Author: Dean Gardiner
Author-email: me@dgardiner.net
License: BitTorrent Open Source License
Keywords: bittorrent,bencode,bdecode
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: Other/Proprietary License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Software Development :: Libraries
License-File: LICENSE
License-File: AUTHORS
License-File: AUTHORS.in
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: license-file
Dynamic: summary

bencode.py
==========

.. image:: https://img.shields.io/pypi/v/bencode.py.svg?style=flat-square
   :target: https://pypi.python.org/pypi/bencode.py

.. image:: https://img.shields.io/travis/fuzeman/bencode.py.svg?style=flat-square
   :target: https://travis-ci.org/fuzeman/bencode.py

.. image:: https://img.shields.io/coveralls/fuzeman/bencode.py/master.svg?style=flat-square
   :target: https://coveralls.io/github/fuzeman/bencode.py

Simple bencode parser (for Python 2, Python 3 and PyPy), forked from the bencode__ package by Thomas Rampelberg.

__ https://pypi.python.org/pypi/bencode


Usage
-----

**Encode:**

.. code-block:: python

    >>> import bencodepy

    >>> bencodepy.encode({'title': 'Example'})
    b'd5:title7:Examplee'

    >>> bencodepy.encode(12)
    b'i12e'

**Decode:**

.. code-block:: python

    >>> import bencodepy

    >>> bencodepy.decode('d5:title7:Examplee')
    {b'title': b'Example'}

    >>> bencodepy.decode('i12e')
    12

**Decode to UTF-8:**

.. code-block:: python

    >>> import bencodepy

    >>> bc = bencodepy.Bencode(
        encoding='utf-8'
    )

    >>> bc.decode('d5:title7:Examplee')
    {'title': 'Example'}

:code:`bencode`
************************************************
*(legacy, backwards-compatible package)*

This package will continue to be provided for backwards-compatibility, but upgrading to ``bencodepy`` is recommended for more reliable decoding results.

Under-the-hood this just provides proxies to a ``Bencode`` instance created with:

.. code-block:: python

    Bencode(
        encoding='utf-8',
        encoding_fallback='value',
        dict_ordered=True,
        dict_ordered_sort=True
    )

**Encode:**

.. code-block:: python

    >>> import bencode

    >>> bencode.encode({'title': 'Example'})
    'd5:title7:Examplee'

    >>> bencode.encode(12)
    'i12e'

**Decode:**

.. code-block:: python

    >>> import bencode

    >>> bencode.decode('d5:title7:Examplee')
    OrderedDict([(u'title', u'Example')])

    >>> bencode.decode('i12e')
    12


API
---

``bencodepy.Bencode(encoding=None, encoding_fallback=None, dict_ordered=False, dict_ordered_sort=False)``

    Create instance

    - encoding
       Encoding to decode strings with (or ``None`` for binary)
    - encoding_fallback
       Fallback to binary when decoding fails on the specified string types.

       - ``key`` - dictionary keys
       - ``value`` - values
       - ``all`` - always fallback to binary
       - ``None`` - always raise decoding errors
    - dict_ordered
       Use ``OrderedDict``
    - dict_ordered_sort
       Ensure ``OrderedDict`` is sorted

    Methods:

    - ``decode(value)``
        Decode bencode string ``value``.

    - ``encode(value)``
        Encode ``value`` into a bencode string.

    - ``read(fd)``
        Decode bencode from file or path ``fd``.

    - ``write(data, fd)``
        Encode ``data`` to file or path ``fd``.

``bencodepy.BencodeDecoder(encoding=None, encoding_fallback=None, dict_ordered=False, dict_ordered_sort=False)``

    Create decoder

    - encoding
       Encoding to decode strings with (or ``None`` for binary)
    - encoding_fallback
       Fallback to binary when decoding fails on the specified string types.

       - ``key`` - dictionary keys
       - ``value`` - values
       - ``all`` - always fallback to binary
       - ``None`` - always raise decoding errors
    - dict_ordered
       Use ``OrderedDict``
    - dict_ordered_sort
       Ensure ``OrderedDict`` is sorted

    Methods:

    - ``decode(value)``
        Decode bencode string ``value``.

``bencodepy.BencodeEncoder()``

    Create encoder

    Methods:

    - ``encode(value)``
        Encode ``value`` into a bencode string.

``bencodepy.bencode(value)``

``bencodepy.encode(value)``

    Encode ``value`` into a bencode string with the default encoder.

``bencodepy.bdecode(value)``

``bencodepy.decode(value)``

    Decode bencode string ``value`` with the default decoder.

``bencodepy.bread(fd)``

    Decode bencode from file or path ``fd`` with the default decoder.

``bencodepy.bwrite(data, fd)``

    Encode ``data`` to file or path ``fd`` with the default encoder.

:code:`bencode`
************************************************

``bencode.bencode(value)``

``bencode.encode(value)``

    Encode ``value`` into the bencode format.

``bencode.bdecode(value)``

``bencode.decode(value)``

    Decode bencode formatted string ``value``.

``bencode.bread(fd)``

    Read bencode formatted string from file or path ``fd``.

``bencode.bwrite(data, fd)``

    Write ``data`` as a bencode formatted string to file or path ``fd``.

