home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / bencode.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  8.3 KB  |  489 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4.  
  5. def decode_int(x, f):
  6.     f += 1
  7.     newf = x.index('e', f)
  8.     
  9.     try:
  10.         n = int(x[f:newf])
  11.     except (OverflowError, ValueError):
  12.         n = long(x[f:newf])
  13.  
  14.     if x[f] == '-':
  15.         if x[f + 1] == '0':
  16.             raise ValueError
  17.         
  18.     elif x[f] == '0' and newf != f + 1:
  19.         raise ValueError
  20.     
  21.     return (n, newf + 1)
  22.  
  23.  
  24. def decode_string(x, f):
  25.     colon = x.index(':', f)
  26.     
  27.     try:
  28.         n = int(x[f:colon])
  29.     except (OverflowError, ValueError):
  30.         n = long(x[f:colon])
  31.  
  32.     if x[f] == '0' and colon != f + 1:
  33.         raise ValueError
  34.     
  35.     colon += 1
  36.     return (x[colon:colon + n], colon + n)
  37.  
  38.  
  39. def decode_list(x, f):
  40.     r = []
  41.     f = f + 1
  42.     while x[f] != 'e':
  43.         (v, f) = decode_func[x[f]](x, f)
  44.         r.append(v)
  45.     return (r, f + 1)
  46.  
  47.  
  48. def decode_dict(x, f):
  49.     r = { }
  50.     f = f + 1
  51.     lastkey = None
  52.     while x[f] != 'e':
  53.         (k, f) = decode_string(x, f)
  54.         if lastkey >= k:
  55.             raise ValueError
  56.         
  57.         lastkey = k
  58.         (r[k], f) = decode_func[x[f]](x, f)
  59.     return (r, f + 1)
  60.  
  61. decode_func = { }
  62. decode_func['l'] = decode_list
  63. decode_func['d'] = decode_dict
  64. decode_func['i'] = decode_int
  65. decode_func['0'] = decode_string
  66. decode_func['1'] = decode_string
  67. decode_func['2'] = decode_string
  68. decode_func['3'] = decode_string
  69. decode_func['4'] = decode_string
  70. decode_func['5'] = decode_string
  71. decode_func['6'] = decode_string
  72. decode_func['7'] = decode_string
  73. decode_func['8'] = decode_string
  74. decode_func['9'] = decode_string
  75.  
  76. def bdecode(x):
  77.     
  78.     try:
  79.         (r, l) = decode_func[x[0]](x, 0)
  80.     except (IndexError, KeyError):
  81.         raise ValueError
  82.  
  83.     if l != len(x):
  84.         raise ValueError
  85.     
  86.     return r
  87.  
  88.  
  89. def test_bdecode():
  90.     
  91.     try:
  92.         bdecode('0:0:')
  93.         if not 0:
  94.             raise AssertionError
  95.     except ValueError:
  96.         pass
  97.  
  98.     
  99.     try:
  100.         bdecode('ie')
  101.         if not 0:
  102.             raise AssertionError
  103.     except ValueError:
  104.         pass
  105.  
  106.     
  107.     try:
  108.         bdecode('i341foo382e')
  109.         if not 0:
  110.             raise AssertionError
  111.     except ValueError:
  112.         pass
  113.  
  114.     if not bdecode('i4e') == 0x4L:
  115.         raise AssertionError
  116.     if not bdecode('i0e') == 0x0L:
  117.         raise AssertionError
  118.     if not bdecode('i123456789e') == 0x75BCD15L:
  119.         raise AssertionError
  120.     if not bdecode('i-10e') == -0xAL:
  121.         raise AssertionError
  122.     
  123.     try:
  124.         bdecode('i-0e')
  125.         if not 0:
  126.             raise AssertionError
  127.     except ValueError:
  128.         pass
  129.  
  130.     
  131.     try:
  132.         bdecode('i123')
  133.         if not 0:
  134.             raise AssertionError
  135.     except ValueError:
  136.         pass
  137.  
  138.     
  139.     try:
  140.         bdecode('')
  141.         if not 0:
  142.             raise AssertionError
  143.     except ValueError:
  144.         pass
  145.  
  146.     
  147.     try:
  148.         bdecode('i6easd')
  149.         if not 0:
  150.             raise AssertionError
  151.     except ValueError:
  152.         pass
  153.  
  154.     
  155.     try:
  156.         bdecode('35208734823ljdahflajhdf')
  157.         if not 0:
  158.             raise AssertionError
  159.     except ValueError:
  160.         pass
  161.  
  162.     
  163.     try:
  164.         bdecode('2:abfdjslhfld')
  165.         if not 0:
  166.             raise AssertionError
  167.     except ValueError:
  168.         pass
  169.  
  170.     if not bdecode('0:') == '':
  171.         raise AssertionError
  172.     if not bdecode('3:abc') == 'abc':
  173.         raise AssertionError
  174.     if not bdecode('10:1234567890') == '1234567890':
  175.         raise AssertionError
  176.     
  177.     try:
  178.         bdecode('02:xy')
  179.         if not 0:
  180.             raise AssertionError
  181.     except ValueError:
  182.         pass
  183.  
  184.     
  185.     try:
  186.         bdecode('l')
  187.         if not 0:
  188.             raise AssertionError
  189.     except ValueError:
  190.         pass
  191.  
  192.     if not bdecode('le') == []:
  193.         raise AssertionError
  194.     
  195.     try:
  196.         bdecode('leanfdldjfh')
  197.         if not 0:
  198.             raise AssertionError
  199.     except ValueError:
  200.         pass
  201.  
  202.     if not bdecode('l0:0:0:e') == [
  203.         '',
  204.         '',
  205.         '']:
  206.         raise AssertionError
  207.     
  208.     try:
  209.         bdecode('relwjhrlewjh')
  210.         if not 0:
  211.             raise AssertionError
  212.     except ValueError:
  213.         pass
  214.  
  215.     if not bdecode('li1ei2ei3ee') == [
  216.         1,
  217.         2,
  218.         3]:
  219.         raise AssertionError
  220.     if not bdecode('l3:asd2:xye') == [
  221.         'asd',
  222.         'xy']:
  223.         raise AssertionError
  224.     if not bdecode('ll5:Alice3:Bobeli2ei3eee') == [
  225.         [
  226.             'Alice',
  227.             'Bob'],
  228.         [
  229.             2,
  230.             3]]:
  231.         raise AssertionError
  232.     
  233.     try:
  234.         bdecode('d')
  235.         if not 0:
  236.             raise AssertionError
  237.     except ValueError:
  238.         pass
  239.  
  240.     
  241.     try:
  242.         bdecode('defoobar')
  243.         if not 0:
  244.             raise AssertionError
  245.     except ValueError:
  246.         pass
  247.  
  248.     if not bdecode('de') == { }:
  249.         raise AssertionError
  250.     if not bdecode('d3:agei25e4:eyes4:bluee') == {
  251.         'age': 25,
  252.         'eyes': 'blue' }:
  253.         raise AssertionError
  254.     if not bdecode('d8:spam.mp3d6:author5:Alice6:lengthi100000eee') == {
  255.         'spam.mp3': {
  256.             'author': 'Alice',
  257.             'length': 100000 } }:
  258.         raise AssertionError
  259.     
  260.     try:
  261.         bdecode('d3:fooe')
  262.         if not 0:
  263.             raise AssertionError
  264.     except ValueError:
  265.         pass
  266.  
  267.     
  268.     try:
  269.         bdecode('di1e0:e')
  270.         if not 0:
  271.             raise AssertionError
  272.     except ValueError:
  273.         pass
  274.  
  275.     
  276.     try:
  277.         bdecode('d1:b0:1:a0:e')
  278.         if not 0:
  279.             raise AssertionError
  280.     except ValueError:
  281.         pass
  282.  
  283.     
  284.     try:
  285.         bdecode('d1:a0:1:a0:e')
  286.         if not 0:
  287.             raise AssertionError
  288.     except ValueError:
  289.         pass
  290.  
  291.     
  292.     try:
  293.         bdecode('i03e')
  294.         if not 0:
  295.             raise AssertionError
  296.     except ValueError:
  297.         pass
  298.  
  299.     
  300.     try:
  301.         bdecode('l01:ae')
  302.         if not 0:
  303.             raise AssertionError
  304.     except ValueError:
  305.         pass
  306.  
  307.     
  308.     try:
  309.         bdecode('9999:x')
  310.         if not 0:
  311.             raise AssertionError
  312.     except ValueError:
  313.         pass
  314.  
  315.     
  316.     try:
  317.         bdecode('l0:')
  318.         if not 0:
  319.             raise AssertionError
  320.     except ValueError:
  321.         pass
  322.  
  323.     
  324.     try:
  325.         bdecode('d0:0:')
  326.         if not 0:
  327.             raise AssertionError
  328.     except ValueError:
  329.         pass
  330.  
  331.     
  332.     try:
  333.         bdecode('d0:')
  334.         if not 0:
  335.             raise AssertionError
  336.     except ValueError:
  337.         pass
  338.  
  339.     
  340.     try:
  341.         bdecode('00:')
  342.         if not 0:
  343.             raise AssertionError
  344.     except ValueError:
  345.         pass
  346.  
  347.     
  348.     try:
  349.         bdecode('l-3:e')
  350.         if not 0:
  351.             raise AssertionError
  352.     except ValueError:
  353.         pass
  354.  
  355.     
  356.     try:
  357.         bdecode('i-03e')
  358.         if not 0:
  359.             raise AssertionError
  360.     except ValueError:
  361.         pass
  362.  
  363.     bdecode('d0:i3ee')
  364.  
  365. from types import StringType, IntType, LongType, DictType, ListType, TupleType
  366.  
  367. class Bencached(object):
  368.     __slots__ = [
  369.         'bencoded']
  370.     
  371.     def __init__(self, s):
  372.         self.bencoded = s
  373.  
  374.  
  375.  
  376. def encode_bencached(x, r):
  377.     r.append(x.bencoded)
  378.  
  379.  
  380. def encode_int(x, r):
  381.     r.extend(('i', str(x), 'e'))
  382.  
  383.  
  384. def encode_string(x, r):
  385.     r.extend((str(len(x)), ':', x))
  386.  
  387.  
  388. def encode_list(x, r):
  389.     r.append('l')
  390.     for i in x:
  391.         encode_func[type(i)](i, r)
  392.     
  393.     r.append('e')
  394.  
  395.  
  396. def encode_dict(x, r):
  397.     r.append('d')
  398.     ilist = x.items()
  399.     ilist.sort()
  400.     for k, v in ilist:
  401.         r.extend((str(len(k)), ':', k))
  402.         encode_func[type(v)](v, r)
  403.     
  404.     r.append('e')
  405.  
  406. encode_func = { }
  407. encode_func[type(Bencached(0))] = encode_bencached
  408. encode_func[IntType] = encode_int
  409. encode_func[LongType] = encode_int
  410. encode_func[StringType] = encode_string
  411. encode_func[ListType] = encode_list
  412. encode_func[TupleType] = encode_list
  413. encode_func[DictType] = encode_dict
  414.  
  415. try:
  416.     from types import BooleanType
  417.     encode_func[BooleanType] = encode_int
  418. except ImportError:
  419.     pass
  420.  
  421.  
  422. def bencode(x):
  423.     r = []
  424.     encode_func[type(x)](x, r)
  425.     return ''.join(r)
  426.  
  427.  
  428. def test_bencode():
  429.     if not bencode(4) == 'i4e':
  430.         raise AssertionError
  431.     if not bencode(0) == 'i0e':
  432.         raise AssertionError
  433.     if not bencode(-10) == 'i-10e':
  434.         raise AssertionError
  435.     if not bencode(0xAB54A98CEB1F0AD2L) == 'i12345678901234567890e':
  436.         raise AssertionError
  437.     if not bencode('') == '0:':
  438.         raise AssertionError
  439.     if not bencode('abc') == '3:abc':
  440.         raise AssertionError
  441.     if not bencode('1234567890') == '10:1234567890':
  442.         raise AssertionError
  443.     if not bencode([]) == 'le':
  444.         raise AssertionError
  445.     if not bencode([
  446.         1,
  447.         2,
  448.         3]) == 'li1ei2ei3ee':
  449.         raise AssertionError
  450.     if not bencode([
  451.         [
  452.             'Alice',
  453.             'Bob'],
  454.         [
  455.             2,
  456.             3]]) == 'll5:Alice3:Bobeli2ei3eee':
  457.         raise AssertionError
  458.     if not bencode({ }) == 'de':
  459.         raise AssertionError
  460.     if not bencode({
  461.         'age': 25,
  462.         'eyes': 'blue' }) == 'd3:agei25e4:eyes4:bluee':
  463.         raise AssertionError
  464.     if not bencode({
  465.         'spam.mp3': {
  466.             'author': 'Alice',
  467.             'length': 100000 } }) == 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee':
  468.         raise AssertionError
  469.     if not bencode(Bencached(bencode(3))) == 'i3e':
  470.         raise AssertionError
  471.     
  472.     try:
  473.         bencode({
  474.             1: 'foo' })
  475.     except TypeError:
  476.         return None
  477.  
  478.     if not 0:
  479.         raise AssertionError
  480.  
  481.  
  482. try:
  483.     import psyco
  484.     psyco.bind(bdecode)
  485.     psyco.bind(bencode)
  486. except ImportError:
  487.     pass
  488.  
  489.