forked from ccheever/bunny1
-
Notifications
You must be signed in to change notification settings - Fork 7
/
__init__.py
executable file
·225 lines (186 loc) · 5.92 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import bottle
from bottle import SimpleTemplate, template
from bottle import route, run, request
from itertools import groupby
################
# CONFIGURATION
#
# These values can be overridden in a "local_settings.py" file so that
# your local changes don't require merging into new versions of cony.
################
DEBUG = True
# Stand-alone server running as a daemon on port 8080
SERVER_MODE = 'STANDALONE' # or 'WSGI', or 'CGI'
SERVER_PORT = 8080
SERVER_HOST = 'localhost' # or '' to allow on all interfaces
HELP_TERMS = ('--help', '?', '-?')
##################
# Default commands
##################
from cony.repo.search import cmd_google as cmd_g
from cony.repo.search import cmd_python as cmd_p
from cony.repo.search import cmd_pypi
cmd_fallback = cmd_g
def cmd_help(term):
"""Shows all available commands."""
items = []
# functions should be sorted by value
# because we will group them lately
functions = sorted(globals().items(), key=lambda x: x[1])
commands = (
(cmd, name)
for name, cmd in functions
if name != 'cmd_fallback' and name.startswith('cmd_') and callable(cmd)
)
commands = groupby(commands, lambda x: x[0])
# "list" of (func, (cmd_example, cmd_exmpl, cmd_ex, ...)) tuples
# names are sorted by length
commands = (
(cmd, sorted(map(lambda x: x[1][4:], values), key=lambda x: len(x), reverse=True))
for cmd, values in commands
)
# and finally, sort by name
commands = sorted(commands, key=lambda x: x[1])
for cmd, names in commands:
title = ', '.join(names)
if cmd is cmd_fallback:
title += ' (default)'
data = dict(
name = names[0],
title = title,
doc = cmd.__doc__,
rich_help = None,
)
rich_help = getattr(cmd, 'rich_help', None)
if rich_help is not None:
data['rich_help'] = names[0] + ' ' + rich_help
items.append(data)
_template = """
<dl class="help">
%for item in items:
<dt>
%if item['rich_help'] is None:
{{ item['title'] }}
%else:
<a href="?s={{ item['rich_help'] }}">{{ item['title'] }}</a></dt>
%end
</dt>
<dd>{{ item['doc'] }}</dt>
%end
</dl>
%rebase layout title='Help — Cony'
"""
return dict(template=_template, items = items, title = u'Help — Cony')
########################
# Templates related part
########################
_TEMPLATES = dict( # {{{
layout = """
<!DOCTYPE html>
<html>
<head>
<title>{{ title or u'Cony — Smart bookmarks' }}</title>
<style>
.container {
margin: 2em 200px 2em 200px; background: #EEE;
padding: 1em 1em 0.5em 1em;
}
.container header {
border-bottom: 1px solid #BBB;
margin-bottom: 2em;
}
.container dl.help dd {
margin-bottom: 1em;
}
.container dl.help dt {
font-weight: bold;
}
.container footer {
border-top: 1px solid #BBB;
text-align: center;
}
.container footer p {
font-size: 0.75em;
margin-top: 0.5em;
margin-bottom: 0.2em;
}
</style>
</head>
<body>
<div class="container">
<header><h1>{{ title }}</h1></header>
%include
<footer>
<p class="copyright">Opensource. By <a href="mailto:svetlyak.40wt@gmail.com">Alexander Artemenko</a>. <a href="http://github.com/svetlyak40wt/cony/">Fork it</a> at the GitHub.</p>
<p class="thanks">Idea was stolen from Facebook's <a href="https://github.com/facebook/bunny1/">bunny1</a>, many thanks to them.</p>
</footer>
</div>
</body>
</html>
""",
) # }}}
class VerySimpleTemplate(SimpleTemplate):
""" A wrapper around Bottle templates, allows to
define templates right in the same file keeping
ability to use inheritance.
"""
def __init__(self, source=None, name=None, **kwargs):
if source is None and name is not None:
source = _TEMPLATES[name]
super(VerySimpleTemplate, self).__init__(
source=source, name=name, **kwargs
)
try:
from local_settings import *
if 'TEMPLATES' in locals():
_TEMPLATES.update(TEMPLATES)
except ImportError, e:
if 'local_settings' not in e.message:
raise
@route('/')
def do_command():
"""Runs a command"""
search_string = request.GET.get('s', 'help')
search_string = search_string.decode('utf-8')
tokens = search_string.split(' ', 1)
command_name = tokens[0]
term = len(tokens) == 2 and tokens[1] or ''
command = globals().get('cmd_' + command_name, None)
if command is None:
command = cmd_fallback
term = search_string
result = command(term)
if isinstance(result, dict):
# Command could return a dict
# in that case, we have to render it first
# here we have to take original function's name
# to work with aliases
original_cmd_name = command.__name__[4:]
name = result.pop('template', original_cmd_name)
kwargs = dict(
title = None,
)
kwargs.update(result)
return template(name, template_adapter=VerySimpleTemplate, **kwargs)
else:
return result
def wsgi():
bottle.debug(DEBUG)
return bottle.app()
def main():
bottle.debug(DEBUG)
if SERVER_MODE == 'STANDALONE':
run(
reloader=DEBUG,
host=SERVER_HOST,
port=SERVER_PORT,
)
elif SERVER_MODE == 'CGI':
run(server=bottle.CGIServer)
else:
print 'Wrong SERVER_MODE=%r defined for running from command-line' % (SERVER_MODE,)
sys.exit(1)
sys.exit(0)