Skip to content

Commit

Permalink
Console tests, modernized to argparse and now supports thumbor --version
Browse files Browse the repository at this point in the history
  • Loading branch information
heynemann committed Oct 29, 2015
1 parent 6cf0629 commit 5f94645
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 41 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def run_setup(extension_modules=[]):
"statsd>=3.0.1",
"libthumbor",
"futures",
"argparse",
],

extras_require={
Expand Down
45 changes: 45 additions & 0 deletions tests/test_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# thumbor imaging service
# https://github.com/thumbor/thumbor/wiki

# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 globo.com timehome@corp.globo.com

from unittest import TestCase

from preggy import expect

from thumbor.console import get_server_parameters


class ConsoleTestCase(TestCase):
def test_can_get_default_server_parameters(self):
params = get_server_parameters()
expect(params.port).to_equal(8888)
expect(params.ip).to_equal('0.0.0.0')
expect(params.config_path).to_be_null()
expect(params.keyfile).to_be_null()
expect(params.log_level).to_equal('warning')
expect(params.app_class).to_equal('thumbor.app.ThumborServiceApp')
expect(params.fd).to_be_null()

def test_can_get_custom_server_parameters(self):
params = get_server_parameters([
'--port=9999',
'--ip=127.0.0.1',
'--conf=/tmp/conf.conf',
'--keyfile=./tests/fixtures/thumbor.key',
'--log-level=debug',
'--app=custom.app',
'--fd=/tmp/fd',
])
expect(params.port).to_equal(9999)
expect(params.ip).to_equal('127.0.0.1')
expect(params.config_path).to_equal('/tmp/conf.conf')
expect(params.keyfile).to_equal('./tests/fixtures/thumbor.key')
expect(params.log_level).to_equal('debug')
expect(params.app_class).to_equal('custom.app')
expect(params.fd).to_equal('/tmp/fd')
1 change: 1 addition & 0 deletions thumbor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
'''This is the main module in thumbor'''

__version__ = "5.2.1"
__release_date__ = "29-Oct-2015"
83 changes: 43 additions & 40 deletions thumbor/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,63 @@
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 globo.com timehome@corp.globo.com

import optparse
import argparse

from thumbor.context import ServerParameters
from thumbor import __version__
from thumbor import __version__, __release_date__


def get_server_parameters(arguments=None):
parser = optparse.OptionParser(
usage="thumbor or type thumbor -h (--help) for help", description=__doc__, version=__version__
)
parser.add_option(
"-p", "--port", type="int", dest="port", default=8888,
help="The port to run this thumbor instance at [default: %default]."
if arguments is None:
arguments = []

parser = argparse.ArgumentParser(description='thumbor server')

parser.add_argument('--version', action='version', version="Thumbor v%s (%s)" % (__version__, __release_date__))

parser.add_argument(
'-p', '--port', default=8888, type=int,
help="The port to run this thumbor instance at [default: %(default)s]."
)
parser.add_option(
"-i", "--ip", dest="ip", default="0.0.0.0",
help="The host address to run this thumbor instance at [default: %default]."

parser.add_argument(
'-i', '--ip', default='0.0.0.0',
help="The host address to run this thumbor instance at [default: %(default)s]."
)
parser.add_option(
"-f", "--fd", dest="file_descriptor",

parser.add_argument(
'-f', '--fd',
help="The file descriptor number or path to listen for connections on (--port and --ip will be ignored if this is set)"
"[default: %default]."
"[default: %(default)s]."
)
parser.add_option(
"-c", "--conf", dest="conf", default="",
help="The path of the configuration file to use for this thumbor instance [default: %default]."

parser.add_argument(
'-c', '--conf', default=None,
help="The path of the configuration file to use for this thumbor instance [default: %(default)s]."
)
parser.add_option(
"-k", "--keyfile", dest="keyfile", default="",
help="The path of the security key file to use for this thumbor instance [default: %default]."

parser.add_argument(
"-k", "--keyfile", default=None,
help="The path of the configuration file to use for this thumbor instance [default: %(default)s]."
)
parser.add_option(
"-l", "--log-level", dest="log_level", default="warning",

parser.add_argument(
"-l", "--log-level", default="warning",
help="The log level to be used. Possible values are: debug, info, warning, error, critical or notset. "
"[default: %default]."
)
parser.add_option(
"-a", "--app", dest="app", default='thumbor.app.ThumborServiceApp',
help="A custom app to use for this thumbor server in case you subclassed ThumborServiceApp [default: %default]."
"[default: %(default)s]."
)

(options, args) = parser.parse_args(arguments)
parser.add_argument(
"-a", "--app", default='thumbor.app.ThumborServiceApp',
help="A custom app to use for this thumbor server in case you subclassed ThumborServiceApp [default: %(default)s]."
)

port = options.port
ip = options.ip
fd = options.file_descriptor
conf = options.conf or None
keyfile = options.keyfile or None
log_level = options.log_level
options = parser.parse_args(arguments)

return ServerParameters(port=port,
ip=ip,
config_path=conf,
keyfile=keyfile,
log_level=log_level,
return ServerParameters(port=options.port,
ip=options.ip,
config_path=options.conf,
keyfile=options.keyfile,
log_level=options.log_level,
app_class=options.app,
fd=fd)
fd=options.fd)
4 changes: 3 additions & 1 deletion thumbor/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def run_server(application, context):

def main(arguments=None):
'''Runs thumbor server with the specified arguments.'''
if arguments is None:
arguments = sys.argv

server_parameters = get_server_parameters(arguments)
config = get_config(server_parameters.config_path)
Expand All @@ -138,4 +140,4 @@ def main(arguments=None):
context.thread_pool.cleanup()

if __name__ == "__main__":
main(sys.argv[1:])
main(sys.argv)

0 comments on commit 5f94645

Please sign in to comment.