From 590ea9913f093d57853939e3590d6cd5fd547a8c Mon Sep 17 00:00:00 2001 From: Shir0kamii Date: Fri, 18 Sep 2015 17:36:14 +0200 Subject: [PATCH] Merge remove_paste.py and and zerobin.py in cmd.py The two commands have been merged together, using subcommands. `remove_paste` is now accessible with `zerobin delete-paste` `zerobin` is moved to `zerobin runserver` but is still accessible with `zerobin` to keep compatibility with older versions. Unfortunately, clize is not structured to allow default subcommand and the actual implementation is nothing but a hack. --- setup.py | 3 +- zerobin/cmd.py | 102 ++++++++++++++++++++++++++++++++++++++++ zerobin/remove_paste.py | 50 -------------------- zerobin/routes.py | 39 +-------------- 4 files changed, 104 insertions(+), 90 deletions(-) create mode 100644 zerobin/cmd.py delete mode 100644 zerobin/remove_paste.py diff --git a/setup.py b/setup.py index 66f4174..c881f02 100644 --- a/setup.py +++ b/setup.py @@ -47,8 +47,7 @@ setup( ], entry_points = { 'console_scripts': [ - 'zerobin = zerobin.routes:main', - 'remove_paste = zerobin.remove_paste:main', + 'zerobin = zerobin.cmd:main', ] } diff --git a/zerobin/cmd.py b/zerobin/cmd.py new file mode 100644 index 0000000..f109f02 --- /dev/null +++ b/zerobin/cmd.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python +# coding: utf-8 + +from __future__ import unicode_literals, absolute_import, print_function + +""" + Main script including runserver and delete-paste. +""" + +import sys + +from sigtools.modifiers import annotate, autokwoargs +import re + +try: + import thread +except ImportError: + import _thread as thread + +from zerobin.utils import (settings, SettingsValidationError, + drop_privileges) +from zerobin.routes import get_app +from zerobin.paste import Paste + +from bottle import run + +import clize + +@annotate(debug=bool, compressed_static=bool) +def runserver(host='', port='', debug=None, user='', group='', + settings_file='', compressed_static=None, + version=False, paste_id_length=None, server="cherrypy"): + + if version: + print('0bin V%s' % settings.VERSION) + sys.exit(0) + + settings.HOST = host or settings.HOST + settings.PORT = port or settings.PORT + settings.USER = user or settings.USER + settings.GROUP = group or settings.GROUP + settings.PASTE_ID_LENGTH = paste_id_length or settings.PASTE_ID_LENGTH + + try: + _, app = get_app(debug, settings_file, compressed_static, settings=settings) + except SettingsValidationError as err: + print('Configuration error: %s' % err.message, file=sys.stderr) + sys.exit(1) + + thread.start_new_thread(drop_privileges, (settings.USER, settings.GROUP)) + + if settings.DEBUG: + run(app, host=settings.HOST, port=settings.PORT, reloader=True, + server="cherrypy") + else: + run(app, host=settings.HOST, port=settings.PORT, server="cherrypy") + + +# The regex parse the url and separate the paste's id from the decription key +# After the '/paste/' part, there is several caracters, identified as +# the uuid of the paste. Followed by a '#', the decryption key of the paste. +paste_url = re.compile('/paste/(?P.*)#(?P.*)') + +def unpack_paste(paste): + """Take either the ID or the URL of a paste, and return its ID""" + + try_url = paste_url.search(paste) + + if try_url: + return try_url.group('paste_id') + return paste + +@annotate(quiet='q') +@autokwoargs +def delete_paste(quiet=False, *pastes): + """ + Remove pastes, given its ID or its URL + + quiet: Don't print anything + + pastes: List of pastes, given by ID or URL + """ + + for paste_uuid in map(unpack_paste, pastes): + try: + Paste.load(paste_uuid).delete() + + if not quiet: + print('Paste {} is removed'.format(paste_uuid)) + + except ValueError: + if not quiet: + print('Paste {} doesn\'t exist'.format(paste_uuid)) + +def main(): + subcommands = [runserver, delete_paste] + subcommand_names = [clize.util.name_py2cli(name) + for name in clize.util.dict_from_names(subcommands).keys()] + if len(sys.argv) < 2 or sys.argv[1] not in subcommand_names: + sys.argv.insert(1, subcommand_names[0]) + clize.run(runserver, delete_paste) + diff --git a/zerobin/remove_paste.py b/zerobin/remove_paste.py deleted file mode 100644 index 9b6ebc4..0000000 --- a/zerobin/remove_paste.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python -# coding: utf-8 - -from zerobin.paste import Paste -from sigtools.modifiers import annotate, autokwoargs -from clize import run -import re - -# The regex parse the url and separate the paste's id from the decription key -# After the '/paste/' part, there is several caracters, identified as -# the uuid of the paste. Followed by a '#', the decryption key of the paste. -paste_url = re.compile('/paste/(?P.*)#(?P.*)') - -def unpack_paste(paste): - """Take either the ID or the URL of a paste, and return its ID""" - - try_url = paste_url.search(paste) - - if try_url: - return try_url.group('paste_id') - return paste - -@annotate(quiet='q') -@autokwoargs -def remove_paste(quiet=False, *pastes): - """ - Remove pastes, given its ID or its URL - - quiet: Don't print anything - - pastes: List of pastes, given by ID or URL - """ - - for paste_uuid in map(unpack_paste, pastes): - try: - Paste.load(paste_uuid).delete() - - if not quiet: - print('Paste {} is removed'.format(paste_uuid)) - - except ValueError: - if not quiet: - print('Paste {} doesn\'t exist'.format(paste_uuid)) - - -def main(): - run(remove_paste) - -if __name__ == "__main__": - main() diff --git a/zerobin/routes.py b/zerobin/routes.py index 23f4b47..1031766 100644 --- a/zerobin/routes.py +++ b/zerobin/routes.py @@ -4,8 +4,7 @@ from __future__ import unicode_literals, absolute_import, print_function """ - Main script including controller, rooting, dependency management, and - server run. + Script including controller, rooting, and dependency management. """ import os @@ -31,8 +30,6 @@ from zerobin.utils import (settings, SettingsValidationError, import bottle from bottle import (Bottle, run, static_file, view, request) -import clize - from zerobin.paste import Paste @@ -178,37 +175,3 @@ def get_app(debug=None, settings_file='', bottle.debug(True) return settings, app - - -@clize.clize(coerce={'debug': bool, 'compressed_static': bool}) -def runserver(host='', port='', debug=None, user='', group='', - settings_file='', compressed_static=None, - version=False, paste_id_length=None, server="cherrypy"): - - if version: - print('0bin V%s' % settings.VERSION) - sys.exit(0) - - settings.HOST = host or settings.HOST - settings.PORT = port or settings.PORT - settings.USER = user or settings.USER - settings.GROUP = group or settings.GROUP - settings.PASTE_ID_LENGTH = paste_id_length or settings.PASTE_ID_LENGTH - - try: - _, app = get_app(debug, settings_file, compressed_static, settings=settings) - except SettingsValidationError as err: - print('Configuration error: %s' % err.message, file=sys.stderr) - sys.exit(1) - - thread.start_new_thread(drop_privileges, (settings.USER, settings.GROUP)) - - if settings.DEBUG: - run(app, host=settings.HOST, port=settings.PORT, reloader=True, - server="cherrypy") - else: - run(app, host=settings.HOST, port=settings.PORT, server="cherrypy") - - -def main(): - clize.run(runserver)