Skip to content

Commit 590ea99

Browse files
committed
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.
1 parent bd7a372 commit 590ea99

File tree

4 files changed

+104
-90
lines changed

4 files changed

+104
-90
lines changed

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
],
4848
entry_points = {
4949
'console_scripts': [
50-
'zerobin = zerobin.routes:main',
51-
'remove_paste = zerobin.remove_paste:main',
50+
'zerobin = zerobin.cmd:main',
5251
]
5352
}
5453

zerobin/cmd.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python
2+
# coding: utf-8
3+
4+
from __future__ import unicode_literals, absolute_import, print_function
5+
6+
"""
7+
Main script including runserver and delete-paste.
8+
"""
9+
10+
import sys
11+
12+
from sigtools.modifiers import annotate, autokwoargs
13+
import re
14+
15+
try:
16+
import thread
17+
except ImportError:
18+
import _thread as thread
19+
20+
from zerobin.utils import (settings, SettingsValidationError,
21+
drop_privileges)
22+
from zerobin.routes import get_app
23+
from zerobin.paste import Paste
24+
25+
from bottle import run
26+
27+
import clize
28+
29+
@annotate(debug=bool, compressed_static=bool)
30+
def runserver(host='', port='', debug=None, user='', group='',
31+
settings_file='', compressed_static=None,
32+
version=False, paste_id_length=None, server="cherrypy"):
33+
34+
if version:
35+
print('0bin V%s' % settings.VERSION)
36+
sys.exit(0)
37+
38+
settings.HOST = host or settings.HOST
39+
settings.PORT = port or settings.PORT
40+
settings.USER = user or settings.USER
41+
settings.GROUP = group or settings.GROUP
42+
settings.PASTE_ID_LENGTH = paste_id_length or settings.PASTE_ID_LENGTH
43+
44+
try:
45+
_, app = get_app(debug, settings_file, compressed_static, settings=settings)
46+
except SettingsValidationError as err:
47+
print('Configuration error: %s' % err.message, file=sys.stderr)
48+
sys.exit(1)
49+
50+
thread.start_new_thread(drop_privileges, (settings.USER, settings.GROUP))
51+
52+
if settings.DEBUG:
53+
run(app, host=settings.HOST, port=settings.PORT, reloader=True,
54+
server="cherrypy")
55+
else:
56+
run(app, host=settings.HOST, port=settings.PORT, server="cherrypy")
57+
58+
59+
# The regex parse the url and separate the paste's id from the decription key
60+
# After the '/paste/' part, there is several caracters, identified as
61+
# the uuid of the paste. Followed by a '#', the decryption key of the paste.
62+
paste_url = re.compile('/paste/(?P<paste_id>.*)#(?P<key>.*)')
63+
64+
def unpack_paste(paste):
65+
"""Take either the ID or the URL of a paste, and return its ID"""
66+
67+
try_url = paste_url.search(paste)
68+
69+
if try_url:
70+
return try_url.group('paste_id')
71+
return paste
72+
73+
@annotate(quiet='q')
74+
@autokwoargs
75+
def delete_paste(quiet=False, *pastes):
76+
"""
77+
Remove pastes, given its ID or its URL
78+
79+
quiet: Don't print anything
80+
81+
pastes: List of pastes, given by ID or URL
82+
"""
83+
84+
for paste_uuid in map(unpack_paste, pastes):
85+
try:
86+
Paste.load(paste_uuid).delete()
87+
88+
if not quiet:
89+
print('Paste {} is removed'.format(paste_uuid))
90+
91+
except ValueError:
92+
if not quiet:
93+
print('Paste {} doesn\'t exist'.format(paste_uuid))
94+
95+
def main():
96+
subcommands = [runserver, delete_paste]
97+
subcommand_names = [clize.util.name_py2cli(name)
98+
for name in clize.util.dict_from_names(subcommands).keys()]
99+
if len(sys.argv) < 2 or sys.argv[1] not in subcommand_names:
100+
sys.argv.insert(1, subcommand_names[0])
101+
clize.run(runserver, delete_paste)
102+

zerobin/remove_paste.py

-50
This file was deleted.

zerobin/routes.py

+1-38
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from __future__ import unicode_literals, absolute_import, print_function
55

66
"""
7-
Main script including controller, rooting, dependency management, and
8-
server run.
7+
Script including controller, rooting, and dependency management.
98
"""
109

1110
import os
@@ -31,8 +30,6 @@
3130
import bottle
3231
from bottle import (Bottle, run, static_file, view, request)
3332

34-
import clize
35-
3633
from zerobin.paste import Paste
3734

3835

@@ -178,37 +175,3 @@ def get_app(debug=None, settings_file='',
178175
bottle.debug(True)
179176

180177
return settings, app
181-
182-
183-
@clize.clize(coerce={'debug': bool, 'compressed_static': bool})
184-
def runserver(host='', port='', debug=None, user='', group='',
185-
settings_file='', compressed_static=None,
186-
version=False, paste_id_length=None, server="cherrypy"):
187-
188-
if version:
189-
print('0bin V%s' % settings.VERSION)
190-
sys.exit(0)
191-
192-
settings.HOST = host or settings.HOST
193-
settings.PORT = port or settings.PORT
194-
settings.USER = user or settings.USER
195-
settings.GROUP = group or settings.GROUP
196-
settings.PASTE_ID_LENGTH = paste_id_length or settings.PASTE_ID_LENGTH
197-
198-
try:
199-
_, app = get_app(debug, settings_file, compressed_static, settings=settings)
200-
except SettingsValidationError as err:
201-
print('Configuration error: %s' % err.message, file=sys.stderr)
202-
sys.exit(1)
203-
204-
thread.start_new_thread(drop_privileges, (settings.USER, settings.GROUP))
205-
206-
if settings.DEBUG:
207-
run(app, host=settings.HOST, port=settings.PORT, reloader=True,
208-
server="cherrypy")
209-
else:
210-
run(app, host=settings.HOST, port=settings.PORT, server="cherrypy")
211-
212-
213-
def main():
214-
clize.run(runserver)

0 commit comments

Comments
 (0)