diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2012-05-24 18:06:25 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2012-05-24 18:06:25 (GMT) |
commit | 2930ead383d0c63837e57e23f6d124c1c9d6b5a8 (patch) | |
tree | dd24ea26bd2da84e63f45985858cd74f29076419 | |
parent | a6872403b3d61edf9f1b961518cc282ec68ab9fa (diff) |
Polish design and avoid having high level logic variables on library level
-rw-r--r-- | TODO | 3 | ||||
-rw-r--r-- | restful_document/__init__.py | 1 | ||||
-rw-r--r-- | restful_document/env.py | 24 | ||||
-rw-r--r-- | restful_document/router.py | 24 | ||||
-rw-r--r-- | restful_document/subscribe_socket.py | 15 | ||||
-rw-r--r-- | tests/__init__.py | 4 |
6 files changed, 22 insertions, 49 deletions
@@ -1,2 +1,5 @@ - implement authentication + +1.0 +=== - replace urllib2 with requests with greenlets enabled diff --git a/restful_document/__init__.py b/restful_document/__init__.py index d3c54fc..ab4306b 100644 --- a/restful_document/__init__.py +++ b/restful_document/__init__.py @@ -14,7 +14,6 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from restful_document.env import HTTPStatus, BadRequest, \ - host, port, trust_users, keyfile, certfile, subscribe_port, \ only_sync_notification from restful_document.router import Router diff --git a/restful_document/env.py b/restful_document/env.py index 3542f72..1d23beb 100644 --- a/restful_document/env.py +++ b/restful_document/env.py @@ -20,30 +20,6 @@ from active_toolkit import optparse subscriber = None -host = optparse.Option( - _('hostname to listen incomming connections'), - default='0.0.0.0') - -port = optparse.Option( - _('port number to listen incomming connections'), - default=8000, type_cast=int) - -subscribe_port = optparse.Option( - _('port number to listen incomming subscribtion requests'), - default=8001, type_cast=int) - -trust_users = optparse.Option( - _('switch off user credentials check; disabling this option will ' \ - 'require OpenSSH-5.6 or later.'), - default=False, type_cast=optparse.Option.bool_cast, - action='store_true') - -keyfile = optparse.Option( - _('path to SSL certificate keyfile to serve requests via HTTPS')) - -certfile = optparse.Option( - _('path to SSL certificate file to serve requests via HTTPS')) - only_sync_notification = optparse.Option( _('subscribers can be notified only with "sync" events; ' \ 'that is useful to minimize interactions between ' \ diff --git a/restful_document/router.py b/restful_document/router.py index 1bb1c80..f1ddafe 100644 --- a/restful_document/router.py +++ b/restful_document/router.py @@ -54,22 +54,22 @@ class Router(object): response['Location'] = error.location result = '' except Exception, error: - util.exception(_('Error while processing %r request'), - environ['PATH_INFO'] or '/') - if isinstance(error, ad.Unauthorized): response.status = '401 Unauthorized' response['WWW-Authenticate'] = 'Sugar' - elif isinstance(error, ad.Forbidden): - response.status = '403 Forbidden' - elif isinstance(error, ad.NotFound): - response.status = '404 Not Found' - elif isinstance(error, env.HTTPStatus): - response.status = error.status - response.update(error.headers or {}) - result = error.result else: - response.status = '500 Internal Server Error' + util.exception(_('Error while processing %r request'), + environ['PATH_INFO'] or '/') + if isinstance(error, ad.Forbidden): + response.status = '403 Forbidden' + elif isinstance(error, ad.NotFound): + response.status = '404 Not Found' + elif isinstance(error, env.HTTPStatus): + response.status = error.status + response.update(error.headers or {}) + result = error.result + else: + response.status = '500 Internal Server Error' if result is None: result = {'error': str(error), diff --git a/restful_document/subscribe_socket.py b/restful_document/subscribe_socket.py index 626e1fb..6d5c128 100644 --- a/restful_document/subscribe_socket.py +++ b/restful_document/subscribe_socket.py @@ -27,7 +27,9 @@ _logger = logging.getLogger('restful_document.subscribe_socket') class SubscribeSocket(object): - def __init__(self, volume): + def __init__(self, volume, host, port): + self._host = host + self._port = port self._server = None self._tickets = set() self._subscribers = set() @@ -37,20 +39,15 @@ class SubscribeSocket(object): def subscribe(self): ticket = os.urandom(16).encode('hex') self._tickets.add(ticket) - - return {'host': env.host.value, - 'port': env.subscribe_port.value, - 'ticket': ticket, - } + return {'host': self._host, 'port': self._port, 'ticket': ticket} def serve_forever(self): - _logger.info(_('Listening for subscriptions on %s port'), - env.subscribe_port.value) + _logger.info(_('Listening for subscriptions on %s port'), self._port) conn = coroutine.socket(socket.AF_INET, socket.SOCK_STREAM) # pylint: disable-msg=E1101 conn.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - conn.bind((env.host.value, env.subscribe_port.value)) + conn.bind((self._host, self._port)) conn.listen(5) self._server = coroutine.Server(conn, self._serve_client) diff --git a/tests/__init__.py b/tests/__init__.py index d4e4576..c5ca24b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -66,8 +66,6 @@ class Test(unittest.TestCase): ad.index_flush_timeout.value = 0 ad.index_flush_threshold.value = 1 - _env.subscribe_port.value = 8101 - self.httpd_seqno = 0 def tearDown(self): @@ -126,7 +124,7 @@ class Test(unittest.TestCase): volume = ad.SingleVolume(tmpdir + '/db', classes) cp = ad.VolumeCommands(volume) httpd = coroutine.WSGIServer(('localhost', port), Router(cp)) - subscriber = SubscribeSocket(volume) + subscriber = SubscribeSocket(volume, 'localhost', port + 1) try: gevent.joinall([ gevent.spawn(httpd.serve_forever), |