Skip to content

Commit

Permalink
working on test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
quantmind committed Apr 15, 2011
1 parent c3150fc commit 94f0298
Show file tree
Hide file tree
Showing 31 changed files with 403 additions and 169 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ docs/build/*
dist/*
.project
.pydevproject
cfg.py
.settings/*
lib/src/parser.c
pulsar/http/cparser/parser.*
File renamed without changes.
6 changes: 4 additions & 2 deletions examples/calculator.py → examples/calculator/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
A very simple JSON-RPC Calculator
'''
import pulsar
from pulsar.http import rpc

class Calculator(pulsar.JSONRPC):
class Calculator(rpc.JSONRPC):

def rpc_ping(self, request):
return 'pong'
Expand All @@ -27,4 +28,5 @@ def run():


if __name__ == '__main__':
run()
run()

11 changes: 11 additions & 0 deletions examples/calculator/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pulsar import test
from pulsar.http import rpc

from .calculator import run


class TestCalculatorExample(test.TestCase):

def setUp(self):
self.p = rpc.JsonProxy('http://localhost:8060')

Empty file added examples/helloworld/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions examples/helloworld.py → examples/helloworld/helloworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def hello(environ, start_response):
return iter([data])


def run():
def run(**kwargs):
wsgi = pulsar.require('wsgi')
wsgi.createServer(callable = hello).run()
wsgi.createServer(callable = hello, **kwargs).run()


if __name__ == '__main__':
Expand Down
15 changes: 15 additions & 0 deletions examples/helloworld/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pulsar import test
from pulsar.http import HttpClient

from .helloworld import run

class TestCalculatorExample(test.TestCase):
uri = 'http://localhost:8060'

def setUp(self):
self.c = HttpClient()

def testThreadWorker(self):
run(worker_class = 'http_t')
resp = self.c.request(self.uri)
self.assertEqual(resp,'Hello World!')
16 changes: 4 additions & 12 deletions pulsar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
'''Concurrent server and message queues'''

VERSION = (0, 1, 'dev')


def get_version():
return '.'.join(map(str,VERSION))

SERVER_NAME = "pulsar"
SERVER_SOFTWARE = "{0}/{1}".format(SERVER_NAME,get_version())


def getLogger(name = None):
import logging
name = '{0}.{1}'.format(SERVER_NAME,name) if name else SERVER_NAME
return logging.getLogger(name)

__version__ = get_version()
__license__ = "BSD"
__author__ = "Luca Sbardella"
Expand All @@ -34,6 +23,10 @@ def getLogger(name = None):
'Topic :: Utilities'
]

from .utils.log import *

SERVER_SOFTWARE = "python-{0}/{1}".format(SERVER_NAME,get_version())

from .utils.exceptions import *
from .utils import test
from .utils import system
Expand All @@ -45,6 +38,5 @@ def getLogger(name = None):
from .workers.base import *
from .workers.arbiter import *
from .apps.base import Application, require
from .utils.rpc import *


6 changes: 5 additions & 1 deletion pulsar/apps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ def __setstate__(self, state):

def load_config(self, **params):
'''Load the application configuration'''
self.cfg = pulsar.Config(self.usage, **params)
self.cfg = pulsar.Config(self.usage)

# add params
for k, v in params.items():
self.cfg.set(k.lower(), v)

# parse console args
parser = self.cfg.parser()
opts, args = parser.parse_args()
Expand Down
2 changes: 2 additions & 0 deletions pulsar/http/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pulsar.utils.importer import import_module

from .client import HttpClient

def get_httplib(cfg = None):
name = None if not cfg else cfg.settings['httplib'].value
if name == 'gunicorn':
Expand Down
31 changes: 17 additions & 14 deletions pulsar/http/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import urllib

from .std import

from .std import HttpClient1, getproxies_environment
HttpClients={1:HttpClient1}
try:
from ._httplib2 import HttpClient2
HttpClients[2] = HttpClient2
except ImportError:
pass


def getproxy(schema = 'http'):
p = urllib.getproxies_environment()
return p.get(schema,None)
form_headers = {'Content-type': 'application/x-www-form-urlencoded'}


def HttpClient(cache = None, proxy_info = None, timeout = None, type = 1, async = False):
'''Create a http client handler:
'''Create a http client handler using different implementation.
It can build a synchronous or an asyncronous handler build on top
of the :class:`pulsar.IOLoop`.
* *cache* is the http cache file.
* *proxy_info* proxy server
* *timeout*
* *type* the type of client.
'''
:parameter cache: Cache file. Default ``None``.
:parameter proxy_info: Dictionary of proxies. Default ``None``.
:parameter timeout: Connection timeout. Default ``None``.
:parameter type: Handler implementation. Default ``1``.
:parameter async: Synchronous or Asynchronous. Default ``False``.
'''
if type not in HttpClients:
raise ValueError('HttpClient{0} not available'.format(type))
client = HttpClients[type]
proxy = proxy_info
if proxy is None:
proxy = getproxy()
proxy = getproxies_environment()

return client(proxy_info = proxy, cache = cache, timeout = timeout)

Expand Down
36 changes: 36 additions & 0 deletions pulsar/http/client/_httplib2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'''\
The Httplib2 clinet
This is a thin layer on top of httplib2 python library.
http://code.google.com/p/httplib2/
'''
import httplib2

from .std import HttpClientBase


class Response(object):
__slots__ = ('response','content')
def __init__(self, response, content):
self.response = response
self.content = content


class HttpClient2(HttpClientBase):

def __init__(self, proxy_info = None,
timeout = None, cache = None, headers = None):
self._opener = httplib2.Http(cache = cache, timeout = timeout,
proxy_info = proxy_info)

def request(self, uri, method='GET', body=None, headers = None, **kwargs):
r = self._opener.open(uri,
method=method,
body=body,
headers=self.headers(headers))
return Response(r)

def add_credentials(self, username, password, domain = ''):
self._opener.add_credentials(username, password, domain)

56 changes: 38 additions & 18 deletions pulsar/http/client/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,68 @@
This is a thin layer on top of urllib2 in python2 / urllib in Python 3
It exposes the httplib1 class from the standard library.
'''
from pulsar.utils.py2py3 import ispy3k
if ispy3k:
import pulsar
if pulsar.ispy3k:
# Python 3
from urllib.request import Request, build_opener, install_opener
from urllib.request import HTTPCookieProcessor, HTTPPasswordMgrWithDefaultRealm
from urllib.request import HTTPBasicAuthHandler
from urllib.request import HTTPBasicAuthHandler, ProxyHandler
from urllib.request import getproxies_environment
from urllib.parse import urlencode
else:
# Python 2.*
from urllib2 import Request, build_opener, install_opener, HTTPCookieProcessor
from urllib2 import HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler
from urllib import urlencode
from urllib2 import ProxyHandler
from urllib import urlencode, getproxies_environment



class Response(object):

def __init__(self, response):
self._response = response

@property
def content(self):
if not hasattr(self,'_content'):
self._content = self.response.read()
return self._content


class HttpClient1(object):

def __init__(self, proxy_info = None, timeout = None, cache = None):
self._opener = opener
class HttpClientBase(object):
DEFAULT_HEADERS = {'user-agent': pulsar.SERVER_SOFTWARE}

def headers(self, headers):
d = self.DEFAULT_HEADERS.copy()
if not headers:
return d
else:
d.update(headers)
return d


class HttpClient1(HttpClientBase):

def __init__(self, proxy_info = None,
timeout = None, cache = None, headers = None):
proxy = ProxyHandler(proxy_info)
self._opener = build_opener(proxy)
self.timeout = timeout

def request(self, url, body=None, **kwargs):
response = self._opener.open(url,data=body,timeout=timeout)
content = c.read()
return (Response(response),content)

@classmethod
def auth(cls, username, password, uri, realm=None, timeout=None):
'''Create an httplib1 instance witn a basic authentication handler.
The authentication'''
response = self._opener.open(url,data=body,timeout=self.timeout)
return Response(response)

def add_password(self, username, password, uri, realm=None):
'''Add Basic HTTP Authentication to the opener'''
if realm is None:
password_mgr = HTTPPasswordMgrWithDefaultRealm()
else:
password_mgr = HTTPPasswordMgr()
password_mgr.add_password(realm, uri, user, passwd)
opener = HTTPBasicAuthHandler(password_mgr)
return cls(opener,timeout)
self._opener.add_handler(HTTPBasicAuthHandler(password_mgr))




Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 3 additions & 6 deletions pulsar/utils/rpc/handlers.py → pulsar/http/rpc/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
from datetime import datetime

from pulsar import PickableMixin
from .exceptions import NoSuchFunction

__all__ = ['RpcHandler']
Expand Down Expand Up @@ -73,7 +74,7 @@ def __new__(cls, name, bases, attrs):
BaseHandler = MetaRpcHandler('BaseRpcHandler',(object,),{'virtual':True})


class RpcHandler(BaseHandler):
class RpcHandler(BaseHandler,PickableMixin):
'''Server Handler.
Sub-handlers for prefixed methods (e.g., system.listMethods)
can be added with putSubHandler. By default, prefixes are
Expand All @@ -96,11 +97,7 @@ def __init__(self,
self.route = route if route is not None else self.route
self.subHandlers = {}
self.started = datetime.now()
logger = kwargs.pop('logger',None)
if logger:
self.log = logger
else:
self.log = logging.getLogger(self.__class__.__name__)
self.log = self.getLogger(**kwargs)
if subhandlers:
for route,handler in subhandlers.items():
if inspect.isclass(handler):
Expand Down
4 changes: 2 additions & 2 deletions pulsar/utils/rpc/jsonrpc.py → pulsar/http/rpc/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import json

from pulsar.utils.crypt import gen_unique_id
from pulsar.http import HttpClient
from pulsar.utils.jsontools import DefaultJSONEncoder, DefaultJSONHook

from .handlers import RpcHandler
#from unuk.http import httplib


__all__ = ['JSONRPC',
Expand Down Expand Up @@ -161,7 +161,7 @@ def __init__(self, url, name = None, version = None,
self.__id = id
self.__data = data if data is not None else {}
if not http:
self._http = httplib(proxy_info = proxies)
self._http = HttpClient(proxy_info = proxies)
else:
self._http = http

Expand Down
1 change: 0 additions & 1 deletion pulsar/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@


Loading

0 comments on commit 94f0298

Please sign in to comment.