Commits

Lynn Rees committed 741ed08

- renaming

Comments (0)

Files changed (35)

 setup(
     name='wire',
     version='0.1',
-    description='''Speaks the networky''',
+    description='''Speakey the networky''',
     long_description='''network connection library''',
     author='L. C. Rees',
     author_email='lcrees@gmail.com',
     license='BSD',
     packages = ['wire', 'wire.talk', 'wire.http'],
-    test_suite='req.test',
+    test_suite='wire.test',
     zip_safe = False,
     keywords='http client https',
     classifiers=[
-from req.util import Load
+from wire.util import Load
 
 WIREAPPS = dict(
-    url='urlwire.Wire',
-    json='jsonwire.Wire',
-    etree='etreewire.Wire',
-    multipart='multipartwire.Wire',
-    pickle='picklewire.Wire',
-    marshal='marshalwire.Wire',
-    yaml='yamlwire.Wire',
-    xmlrpc='xmlrpcwire.Wire',
-    bson='bsonwire.Wire',
-    html='htmlwire.Wire',
-    beautifulsoup='bswire.Wire',
-    avro='avrowire.Wire',
-    thrift='thriftwire.Wire',
-    protobuf='protobufwire.Wire',
-    csv='csvwire.Wire',
-    excel='excelwire.Wire',
+    avro='avro_talk.Talker',
+    beautiful_soup='bs_talk.Talker',
+    bson='bson_talk.Talker',
+    csv='csv_talk.Talker',
+    excel='excel_talk.Talker',
+    html='html_talk.Talker',
+    json='json_talk.Talker',
+    marshal='marshal_talk.Talker',
+    multipart='multipart_talk.Talker',
+    pickle='pickle_talk.Talker',
+    protobuf='protobuf_talk.Talker',
+    thrift='thrift_talk.Talker',
+    url='url_talk.Talker',
+    xml='xml_talk.Talker',
+    xmlrpc='xmlrpc_talk.Talker',
+    yaml='yaml_talk.Talker',
 )
-WIRES = Load(WIREAPPS, module='req.wire')
+WIRES = Load(WIREAPPS, module='wire.talk')
 

wire/talk/avro.py

-'''AVRO <-> Python (warning: experimental)'''
-
-from cStringIO import StringIO
-
-try:
-    from avro import ipc, io, schema
-except ImportError:
-    raise ImportError('requires avro module')
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        buffer = ipc.FramedWriter(StringIO())
-        buffer.write_framed_message(data)
-        return buffer.writer.getvalue()
-
-    def loads(self, data, **kw):
-        reader = ipc.FramedReader(data)
-        framed = reader.read_framed_message()
-        decoder = io.BinaryDecoder(StringIO(framed))
-        ipc.META_READER.read(decoder)
-        message_name = kw.get('avro_message_name')
-        # remote response schema
-        remote_protocol = kw.get('avro_remote_protocol')
-        if remote_protocol is not None:
-            remote_schema = remote_protocol.messages.get(message_name)
-            if remote_schema is None:
-                raise schema.AvroException(
-                    'Unknown remote message: %s' % message_name
-                )
-        else:
-            raise schema.AvroException('No remote protocol')
-        local_protocol = kw.get('avro_local_protocol')
-        if local_protocol is not None:
-            local_schema = local_protocol.messages.get(message_name)
-            if local_schema is None:
-                raise schema.AvroException(
-                    'Unknown local message: %s' % message_name
-                )
-        else:
-            raise schema.AvroException('No local protocol')
-        # error flag
-        if not decoder.read_boolean():
-            writers_schema = remote_schema.response
-            readers_schema = local_schema.response
-            datum_reader = io.DatumReader(writers_schema, readers_schema)
-            return datum_reader.read(decoder)
-        writers_schema = remote_schema.errors
-        readers_schema = local_schema.errors
-        datum_reader = io.DatumReader(writers_schema, readers_schema)
-        raise ipc.AvroRemoteException(datum_reader.read(decoder))

wire/talk/avro_talk.py

+'''AVRO <-> Python (warning: experimental)'''
+
+from cStringIO import StringIO
+
+try:
+    from avro import ipc, io, schema
+except ImportError:
+    raise ImportError('requires avro module')
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        buffer = ipc.FramedWriter(StringIO())
+        buffer.write_framed_message(data)
+        return buffer.writer.getvalue()
+
+    def loads(self, data, **kw):
+        reader = ipc.FramedReader(data)
+        framed = reader.read_framed_message()
+        decoder = io.BinaryDecoder(StringIO(framed))
+        ipc.META_READER.read(decoder)
+        message_name = kw.get('avro_message_name')
+        # remote response schema
+        remote_protocol = kw.get('avro_remote_protocol')
+        if remote_protocol is not None:
+            remote_schema = remote_protocol.messages.get(message_name)
+            if remote_schema is None:
+                raise schema.AvroException(
+                    'Unknown remote message: %s' % message_name
+                )
+        else:
+            raise schema.AvroException('No remote protocol')
+        local_protocol = kw.get('avro_local_protocol')
+        if local_protocol is not None:
+            local_schema = local_protocol.messages.get(message_name)
+            if local_schema is None:
+                raise schema.AvroException(
+                    'Unknown local message: %s' % message_name
+                )
+        else:
+            raise schema.AvroException('No local protocol')
+        # error flag
+        if not decoder.read_boolean():
+            writers_schema = remote_schema.response
+            readers_schema = local_schema.response
+            datum_reader = io.DatumReader(writers_schema, readers_schema)
+            return datum_reader.read(decoder)
+        writers_schema = remote_schema.errors
+        readers_schema = local_schema.errors
+        datum_reader = io.DatumReader(writers_schema, readers_schema)
+        raise ipc.AvroRemoteException(datum_reader.read(decoder))

wire/talk/bs_talk.py

+'''XML <-> BeautifulSoup etree object'''
+
+try:
+    from lxml.html.soupparser import tostring, fromstring
+except ImportError:
+    raise ImportError('requires lxml and BeautifulSoup libraries')
+
+
+class Talker(object):
+
+    def dumps(self, data, **kw):
+        return tostring(data, **kw)
+
+    def puts(self, data, **kw):
+        return fromstring(data, **kw)

wire/talk/bson_talk.py

+'''BSON <-> Python'''
+
+try:
+    from bson import dumps, loads
+except ImportError:
+    raise ImportError('requires bson module')
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return dumps(data, **kw)
+
+    def loads(self, data, **kw):
+        return loads(data, **kw)

wire/talk/bsonwire.py

-'''BSON <-> Python'''
-
-try:
-    from bson import dumps, loads
-except ImportError:
-    raise ImportError('requires bson module')
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return dumps(data, **kw)
-
-    def loads(self, data, **kw):
-        return loads(data, **kw)

wire/talk/bswire.py

-'''XML <-> BeautifulSoup etree object'''
-
-try:
-    from lxml.html.soupparser import tostring, fromstring
-except ImportError:
-    raise ImportError('requires lxml and BeautifulSoup libraries')
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return tostring(data, **kw)
-
-    def puts(self, data, **kw):
-        return fromstring(data, **kw)

wire/talk/csv_talk.py

+'''comma-separated values <-> Python'''
+
+import csv
+from cStringIO import StringIO
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        dumped = StringIO(data)
+        csvw = csv.writer(dumped, **kw)
+        for row in data: csvw.writerow(row)
+        return dumped.getvalue()
+
+    def loads(self, data, **kw):
+        return list(r for r in csv.reader(StringIO(data), **kw))

wire/talk/csvwire.py

-'''comma-separated values <-> Python'''
-
-import csv
-from cStringIO import StringIO
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        dumped = StringIO(data)
-        csvw = csv.writer(dumped, **kw)
-        for row in data: csvw.writerow(row)
-        return dumped.getvalue()
-
-    def loads(self, data, **kw):
-        return list(r for r in csv.reader(StringIO(data), **kw))

wire/talk/etreewire.py

-'''XML <-> etree object'''
-
-try:
-    from lxml.etree import tostring, fromstring
-except ImportError:
-    from xml.etree.ElementTree import tostring, fromstring
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return tostring(data, **kw)
-
-    def puts(self, data, **kw):
-        return fromstring(data, **kw)

wire/talk/excel_talk.py

+'''M$ Excel <-> Python'''
+
+from cStringIO import StringIO
+
+import xlrd
+import xlwt
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        wb = xlwt.Workbook(encoding='utf8', **kw)
+        ws = wb.add_sheet(kw.get('title', 'Worksheet 1'))
+        for i, row in enumerate(data):
+            for j, col in enumerate(row): ws.write(i, j, col)
+        stream = StringIO()
+        wb.save(stream)
+        return stream.getvalue()
+
+    def loads(self, data, **kw):
+        '''Returns dataset from CSV stream.'''
+        wb = xlrd.open_workbook(file_contents=data, **kw)
+        new_workbook = list()
+        for sheet in wb.sheets():
+            new_sheet = list()
+            for row in xrange(sheet.nrows):
+                new_row = list()
+                for col in xrange(sheet.ncols):
+                    new_row.append(sheet.cell_value(row, col))
+                new_sheet.append(new_row)
+            new_workbook.append(new_sheet)
+        return new_workbook

wire/talk/excelwire.py

-'''M$ Excel <-> Python'''
-
-from cStringIO import StringIO
-
-import xlrd
-import xlwt
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        wb = xlwt.Workbook(encoding='utf8', **kw)
-        ws = wb.add_sheet(kw.get('title', 'Worksheet 1'))
-        for i, row in enumerate(data):
-            for j, col in enumerate(row): ws.write(i, j, col)
-        stream = StringIO()
-        wb.save(stream)
-        return stream.getvalue()
-
-    def loads(self, data, **kw):
-        '''Returns dataset from CSV stream.'''
-        wb = xlrd.open_workbook(file_contents=data, **kw)
-        new_workbook = list()
-        for sheet in wb.sheets():
-            new_sheet = list()
-            for row in xrange(sheet.nrows):
-                new_row = list()
-                for col in xrange(sheet.ncols):
-                    new_row.append(sheet.cell_value(row, col))
-                new_sheet.append(new_row)
-            new_workbook.append(new_sheet)
-        return new_workbook

wire/talk/html_talk.py

+'''XML <-> HTML etree object'''
+
+try:
+    from lxml.html import tostring, fromstring
+except ImportError:
+    raise ImportError('requires lxml library')
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return tostring(data, **kw)
+
+    def puts(self, data, **kw):
+        return fromstring(data, **kw)

wire/talk/htmlwire.py

-'''XML <-> HTML etree object'''
-
-try:
-    from lxml.html import tostring, fromstring
-except ImportError:
-    raise ImportError('requires lxml library')
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return tostring(data, **kw)
-
-    def puts(self, data, **kw):
-        return fromstring(data, **kw)

wire/talk/json_talk.py

+'''JSON <-> Python'''
+
+try:
+    import simplejson as json
+except ImportError:
+    import json
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return json.dumps(data, use_decimal=True, **kw)
+
+    def puts(self, data, **kw):
+        return json.loads(data, use_decimal=True, **kw)

wire/talk/jsonwire.py

-'''JSON <-> Python'''
-
-try:
-    import simplejson as json
-except ImportError:
-    import json
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return json.dumps(data, use_decimal=True, **kw)
-
-    def puts(self, data, **kw):
-        return json.loads(data, use_decimal=True, **kw)

wire/talk/marshal_talk.py

+'''marshal <-> Python objects'''
+
+import marshal
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return marshal.dumps(data, **kw)
+
+    def loads(self, data, **kw):
+        return marshal.loads(data)

wire/talk/marshalwire.py

-'''marshal <-> Python objects'''
-
-import marshal
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return marshal.dumps(data, **kw)
-
-    def loads(self, data, **kw):
-        return marshal.loads(data)

wire/talk/multipart_talk.py

+'''multipart <-> Python objects'''
+
+import cgi
+import mimetypes
+import mimetools
+from StringIO import StringIO
+
+from req.util import lazy
+
+def dumps(fields, **kw):
+    '''
+    fields is a sequence of (name, value) elements for regular form fields.
+    files is a sequence of (name, filename, value) elements for data to be uploaded as files
+    Return (content_type, body) ready for httplib.HTTP instance
+    '''
+    BOUNDARY = mimetools.choose_boundary()
+    CRLF = '\r\n'
+    L = []
+    for key, value in fields:
+        L.append('--' + BOUNDARY)
+        L.append('Content-Disposition: form-data; name="%s"' % key)
+        L.append('')
+        L.append(value)
+    files = kw.get('files', False)
+    if files:
+        def get_content_type(filename):
+            return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
+        for key, filename, value in files:
+            L.append('--' + BOUNDARY)
+            L.append(
+                'Content-Disposition: form-data; name="%s"; filename="%s"' % (
+                    key, filename
+                )
+            )
+            L.append('Content-Type: %s' % get_content_type(filename))
+            L.append('')
+            L.append(value)
+    L.append('--' + BOUNDARY + '--')
+    L.append('')
+    body = CRLF.join(L)
+    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
+    return content_type, body
+
+
+class File(StringIO):
+
+    '''Uploaded file.'''
+
+    def __init__(self, fileobj, charset):
+        # Return to position 0
+        fileobj.seek(0)
+        # Put in StringIO
+        StringIO.__init__(fileobj.read())
+        self.filename = fileobj.filename.decode(charset)
+        self.type = fileobj.type
+        self.headers = fileobj.headers
+
+    @lazy
+    def value(self):
+        '''Value of file.'''
+        return self.getvalue()
+
+
+class MultiPart(object):
+
+    '''User submitted data access.'''
+
+    def __init__(self, data, **kw):
+        '''
+        @param data
+        @param charset Character encoding
+        '''
+        self._fields = cgi.FieldStorage(fp=StringIO(data))
+
+    def __iter__(self):
+        return (k for k in self._fields)
+
+    def items(self):
+        '''Items in form.'''
+        return ((k, self.get(k)) for k in self)
+
+    def get(self, key, default=None):
+        '''Fetch values from form.
+
+        @param key Key in form data
+        @param default Default value (default: None)
+        '''
+        # Fetch item from fieldstorate
+        try:
+            value = self._fields[key]
+        except KeyError:
+            return default
+        # Single value
+        if isinstance(value, cgi.MiniFieldStorage):
+            value = value.value
+        # Multi-part value
+        elif isinstance(value, cgi.FieldStorage):
+            # Create file object for file uploads
+            if value.file: return File(value)
+            # Single value
+            value = value.value
+        return value
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return dumps(data, **kw)
+
+    def loads(self, data, **kw):
+        return dict(i for i in MultiPart(data, **kw).items())

wire/talk/multipartwire.py

-'''multipart <-> Python objects'''
-
-import cgi
-import mimetypes
-import mimetools
-from StringIO import StringIO
-
-from req.util import lazy
-
-def dumps(fields, **kw):
-    '''
-    fields is a sequence of (name, value) elements for regular form fields.
-    files is a sequence of (name, filename, value) elements for data to be uploaded as files
-    Return (content_type, body) ready for httplib.HTTP instance
-    '''
-    BOUNDARY = mimetools.choose_boundary()
-    CRLF = '\r\n'
-    L = []
-    for key, value in fields:
-        L.append('--' + BOUNDARY)
-        L.append('Content-Disposition: form-data; name="%s"' % key)
-        L.append('')
-        L.append(value)
-    files = kw.get('files', False)
-    if files:
-        def get_content_type(filename):
-            return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
-        for key, filename, value in files:
-            L.append('--' + BOUNDARY)
-            L.append(
-                'Content-Disposition: form-data; name="%s"; filename="%s"' % (
-                    key, filename
-                )
-            )
-            L.append('Content-Type: %s' % get_content_type(filename))
-            L.append('')
-            L.append(value)
-    L.append('--' + BOUNDARY + '--')
-    L.append('')
-    body = CRLF.join(L)
-    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
-    return content_type, body
-
-
-class File(StringIO):
-
-    '''Uploaded file.'''
-
-    def __init__(self, fileobj, charset):
-        # Return to position 0
-        fileobj.seek(0)
-        # Put in StringIO
-        StringIO.__init__(fileobj.read())
-        self.filename = fileobj.filename.decode(charset)
-        self.type = fileobj.type
-        self.headers = fileobj.headers
-
-    @lazy
-    def value(self):
-        '''Value of file.'''
-        return self.getvalue()
-
-
-class MultiPart(object):
-
-    '''User submitted data access.'''
-
-    def __init__(self, data, **kw):
-        '''
-        @param data
-        @param charset Character encoding
-        '''
-        self._fields = cgi.FieldStorage(fp=StringIO(data))
-
-    def __iter__(self):
-        return (k for k in self._fields)
-
-    def items(self):
-        '''Items in form.'''
-        return ((k, self.get(k)) for k in self)
-
-    def get(self, key, default=None):
-        '''Fetch values from form.
-
-        @param key Key in form data
-        @param default Default value (default: None)
-        '''
-        # Fetch item from fieldstorate
-        try:
-            value = self._fields[key]
-        except KeyError:
-            return default
-        # Single value
-        if isinstance(value, cgi.MiniFieldStorage):
-            value = value.value
-        # Multi-part value
-        elif isinstance(value, cgi.FieldStorage):
-            # Create file object for file uploads
-            if value.file: return File(value)
-            # Single value
-            value = value.value
-        return value
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return dumps(data, **kw)
-
-    def loads(self, data, **kw):
-        return dict(i for i in MultiPart(data, **kw).items())

wire/talk/pickle_talk.py

+'''Pickle <-> Python objects'''
+
+try:
+    import cPickle as pickle
+except ImportError:
+    import pickle
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
+
+    def loads(self, data, **kw):
+        return pickle.loads(data, pickle.HIGHEST_PROTOCOL)

wire/talk/picklewire.py

-'''Pickle <-> Python objects'''
-
-try:
-    import cPickle as pickle
-except ImportError:
-    import pickle
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
-
-    def loads(self, data, **kw):
-        return pickle.loads(data, pickle.HIGHEST_PROTOCOL)

wire/talk/protobuf_talk.py

+'''Google Protocol Buffers <-> Python'''
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        proto = kw.get('protobuf_proto')
+        return proto().SerializeToString(data)
+
+    def puts(self, data, **kw):
+        proto = kw.get('protobuf')
+        return proto().ParseFromString(data, **kw)

wire/talk/protobufwire.py

-'''Google Protocol Buffers <-> Python'''
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        proto = kw.get('protobuf_proto')
-        return proto().SerializeToString(data)
-
-    def puts(self, data, **kw):
-        proto = kw.get('protobuf')
-        return proto().ParseFromString(data, **kw)

wire/talk/thrift_talk.py

+'''Thrift <-> Python'''
+
+try:
+    from thrift.TSerialization import serialize, deserialize
+except ImportError:
+    raise ImportError('requires Thrift library')
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return serialize(data)
+
+    def loads(self, data, **kw):
+        return deserialize(kw.get('thrift_base_type'), data)

wire/talk/thriftwire.py

-'''Thrift <-> Python'''
-
-try:
-    from thrift.TSerialization import serialize, deserialize
-except ImportError:
-    raise ImportError('requires Thrift library')
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return serialize(data)
-
-    def loads(self, data, **kw):
-        return deserialize(kw.get('thrift_base_type'), data)

wire/talk/url_talk.py

+'''urlencode <-> Python objects'''
+
+import urllib
+try:
+    from urlparse import parse_qs
+except ImportError:
+    from cgi import parse_qs
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return urllib.urlencode(data, **kw)
+
+    def loads(self, data, **kw):
+        qdict = parse_qs(data, **kw)
+        for key, value in qdict.iteritems():
+            if len(value) == 1: qdict[key] = value[0]
+        return qdict

wire/talk/urlwire.py

-'''urlencode <-> Python objects'''
-
-import urllib
-try:
-    from urlparse import parse_qs
-except ImportError:
-    from cgi import parse_qs
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return urllib.urlencode(data, **kw)
-
-    def loads(self, data, **kw):
-        qdict = parse_qs(data, **kw)
-        for key, value in qdict.iteritems():
-            if len(value) == 1: qdict[key] = value[0]
-        return qdict

wire/talk/xml_talk.py

+'''XML <-> etree object'''
+
+try:
+    from lxml.etree import tostring, fromstring
+except ImportError:
+    from xml.etree.ElementTree import tostring, fromstring
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return tostring(data, **kw)
+
+    def puts(self, data, **kw):
+        return fromstring(data, **kw)

wire/talk/xmlrpc_talk.py

+'''XML-RPC <-> Python'''
+
+import xmlrpclib
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return xmlrpclib.dumps(data, **kw)
+
+    def loads(self, data, **kw):
+        return xmlrpclib.loads(data, **kw)

wire/talk/xmlrpcwire.py

-'''XML-RPC <-> Python'''
-
-import xmlrpclib
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return xmlrpclib.dumps(data, **kw)
-
-    def loads(self, data, **kw):
-        return xmlrpclib.loads(data, **kw)

wire/talk/yaml_talk.py

+'''YaML <-> Python'''
+
+try:
+    from yaml import dump_all, load_all
+except ImportError:
+    raise ImportError('requires PyYaml module')
+
+
+class Wire(object):
+
+    def dumps(self, data, **kw):
+        return dump_all(data, **kw)
+
+    def loads(self, data, **kw):
+        return load_all(data, **kw)

wire/talk/yamlwire.py

-'''YaML <-> Python'''
-
-try:
-    from yaml import dump_all, load_all
-except ImportError:
-    raise ImportError('requires PyYaml module')
-
-
-class Wire(object):
-
-    def dumps(self, data, **kw):
-        return dump_all(data, **kw)
-
-    def loads(self, data, **kw):
-        return load_all(data, **kw)
 except ImportError:
     from req._compat import OrderedDict
 
-
 def lru_cache(maxsize=100):
     '''Least-recently-used cache decorator.
 
         setattr(cls, meth.__name__, value)
         return value
 
-#TODO:user agent randomizer
-
 
 class Load(object):