Skip to content

Commit

Permalink
Installable from PyPi with all commands.
Browse files Browse the repository at this point in the history
Code was refactored. Cony is a module now with submodule repos,
containing all 3-party commands. Also, exacutable command was renamed
from ./cony.py into the ./cony-server.
  • Loading branch information
svetlyak40wt committed Mar 20, 2011
1 parent ab959f1 commit 2e4051e
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 124 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist/
*.pyc
local_settings.py
.vim/ide.session
build
31 changes: 22 additions & 9 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ What the difference between Cony and Bunny1?
micro web framework without dependencies.
* Cony could be easily extended. You have no need to inherit any classes like
you do with Bunny1. With Cony, you place your commands in a separate
file and fire `./cony.py`.
file and fire `./cony-server`.

Introduction
------------
Expand Down Expand Up @@ -51,16 +51,19 @@ Installation

Ways to install Cony:

* Just clone the repository and to run `./cony.py`.
* `easy_install cony; cony.py`
* `pip install cony; cony.py`
* Just clone the repository and to run `./cony-server`.
* `easy_install cony; cony-server`
* `pip install cony; cony-server`

Now you have server up and running. It binds to the localhost:8080 by
default. Open the <http://localhost:8080> in your browser to see the help.

You can configure Cony by editing the top of "cony.py" and looking for the
"SERVER_*" entries. These can be adjusted to change the port, change what
interface is bound to (use '' to bind to all interfaces).
You can configure Cony by overwriting `SERVER_*` variables in your
`local_settings.py` file. Your could define:

SERVER_MODE = 'STANDALONE' # or 'WSGI', or 'CGI'
SERVER_PORT = 8080
SERVER_HOST = 'localhost' # or '' to allow on all interfaces

You can also configure CGI or WSGI modes if you want to integrate this into
an existing web server such as Apache.
Expand All @@ -73,7 +76,7 @@ Create "local_settings.py" which contains:

In your Apache configuration, add:

ScriptAlias /cony/ /path/to/cony.py
ScriptAlias /cony/ /path/to/cony-server

Re-start Apache and you should now be able to use
"http://servername/cony/?s=%s" as the Cony URL in your browser
Expand All @@ -91,7 +94,7 @@ mod_wsgi").
In your Apache configuration, add:

WSGIPythonPath /path/to/cony/directory
WSGIScriptAlias /cony/ /path/to/cony/directory/cony.py
WSGIScriptAlias /cony/ /path/to/cony/directory/cony-server

Re-start Apache and you should now be able to use
"http://servername/cony/?s=%s" as the Cony URL in your browser
Expand Down Expand Up @@ -203,13 +206,23 @@ such as `rich_help('--help')`, then the link adds that argument to the
command link. This is meant so that you can have the main help page link
to help pages for the individual commands.

Contributing
------------

There are many commands in `cony.repo` submodule. To plug them all,
do `from cony.repo import *` or import only those you need for.

If you wrote a command, useful to other users, [fork this project][GitHub] on a
GitHub and add you command somewhere in the `cony.repo` submodule. Then send me
a pull request.

Contributors
------------

* Alexander Artemenko (author)
* Sean Reifschneider

[GitHub]: http://github.com/svetlyak40wt/cony
[smart-bm]: http://en.wikipedia.org/wiki/Smart_bookmark
[bunny1]: https://github.com/facebook/bunny1
[Keywurl]: http://alexstaubo.github.com/keywurl/
Expand Down
4 changes: 4 additions & 0 deletions cony-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env python

import cony
cony.main()
110 changes: 16 additions & 94 deletions cony.py → cony/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

import sys
import bottle
import urllib2

from bottle import SimpleTemplate, template
from bottle import route, run, request, redirect
from bottle import route, run, request
from itertools import groupby


################
# CONFIGURATION
#
Expand All @@ -26,95 +26,12 @@
##################
# Default commands
##################

def rich_help(help_argument = ''):
"""Decorator for command functions to mark them as providing help.
It causes the default cmd_help to link to them. The optional
`help_argument`, if set, is the argument passed to the linked command
for help.
Usage:
@rich_help('help')
def cmd_some_command(term):
\"\"\"Short help\"\"\"
...
or
@rich_help
def cmd_some_command(term):
\"\"\"Short help\"\"\"
...
"""
if callable(help_argument): # it means decorator was applied without args
func = help_argument
func.rich_help = ''
return func
else:
def decorator(handler):
handler.rich_help = help_argument
return handler
return decorator


def cmd_g(term):
"""Google search."""
redirect('http://www.google.com/search?q=%s' % term)

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_pypi(term):
"""Python package index search.
If there is exact match, then redirects right to the package's page.
"""
import urllib
try:
direct_url = 'http://pypi.python.org/pypi/%s/' % term
result = urllib.urlopen(direct_url)
except Exception, e:
pass
else:
if result.code == 200:
redirect(direct_url)

redirect('http://pypi.python.org/pypi?:action=search&term=%s&submit=search' % term)


@rich_help('--help')
def cmd_p(term):
'''Python documentation search.'''

if term == '--help' or term == '?' or term == '-?':
_template = """
<p>Search the Python documentation pages for the specified string.
If the term is:</p>
<ul>
<li><b>No arguments</b> — Take you to the main Python
documentation library page.</li>
<li><b>Matches module name</b> — Go directly to that
module's documentation page.</li>
<li><b>Otherwise</b> — Passes the term on to the PyDoc search page.</li>
</ul>
%rebase layout title = 'PyDoc Help — Cony'
"""
return dict(template=_template)
elif not term:
redirect('http://docs.python.org/library/index.html')
else:
try:
url = 'http://docs.python.org/dev/library/%s.html' % term
urllib2.urlopen(url)
redirect(url)
except urllib2.HTTPError:
redirect('http://docs.python.org/search.html?q=%s'
'&check_keywords=yes&area=default' % term)


def cmd_help(term):
"""Shows all available commands."""
items = []
Expand Down Expand Up @@ -176,6 +93,7 @@ def cmd_help(term):
"""
return dict(template=_template, items = items, title = u'Help — Cony')


########################
# Templates related part
########################
Expand Down Expand Up @@ -244,8 +162,9 @@ def __init__(self, source=None, name=None, **kwargs):
from local_settings import *
if 'TEMPLATES' in locals():
_TEMPLATES.update(TEMPLATES)
except ImportError:
pass
except ImportError, e:
if 'local_settings' not in e.message:
raise


@route('/')
Expand Down Expand Up @@ -280,12 +199,14 @@ def do_command():
return result


bottle.debug(DEBUG)
def wsgi():
bottle.debug(DEBUG)
return bottle.app()

if SERVER_MODE == 'WSGI':
application = bottle.app()

elif __name__ == '__main__':
def main():
bottle.debug(DEBUG)

if SERVER_MODE == 'STANDALONE':
run(
reloader=DEBUG,
Expand All @@ -298,3 +219,4 @@ def do_command():
print 'Wrong SERVER_MODE=%r defined for running from command-line' % (SERVER_MODE,)
sys.exit(1)
sys.exit(0)

File renamed without changes.
File renamed without changes.
77 changes: 77 additions & 0 deletions cony/repo/search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
import urllib2
from bottle import redirect
from cony.utils import rich_help


def cmd_google(term):
"""Google search."""
redirect('http://www.google.com/search?q=%s' % term)


def cmd_fl(term):
"""Search among Flickr photos under Creative Commons license."""
redirect('http://www.flickr.com/search/?q=%s&l=cc&ss=0&ct=0&mt=all&w=all&adv=1' % term)


def cmd_pep(term):
"""Search a Python Enhancement Proposal by it's number. For example: 'pep 8'."""
redirect('http://www.python.org/dev/peps/pep-%0.4d/' % int(term))


def cmd_dj(term):
"""Django documentation search."""
redirect(
'http://docs.djangoproject.com/en/dev/search/?cx=009763561546736975936:e88ek0eurf4&'
'cof=FORID:11&q=%s&siteurl=docs.djangoproject.com/en/dev/topics/db/models/' % term
)


def cmd_pypi(term):
"""Python package index search.
If there is exact match, then redirects right to the package's page.
"""
import urllib
try:
direct_url = 'http://pypi.python.org/pypi/%s/' % term
result = urllib.urlopen(direct_url)
except Exception, e:
pass
else:
if result.code == 200:
redirect(direct_url)

redirect('http://pypi.python.org/pypi?:action=search&term=%s&submit=search' % term)


@rich_help('--help')
def cmd_python(term):
'''Python documentation search.'''

if term == '--help' or term == '?' or term == '-?':
_template = """
<p>Search the Python documentation pages for the specified string.
If the term is:</p>
<ul>
<li><b>No arguments</b> — Take you to the main Python
documentation library page.</li>
<li><b>Matches module name</b> — Go directly to that
module's documentation page.</li>
<li><b>Otherwise</b> — Passes the term on to the PyDoc search page.</li>
</ul>
%rebase layout title = 'PyDoc Help — Cony'
"""
return dict(template=_template)
elif not term:
redirect('http://docs.python.org/library/index.html')
else:
try:
url = 'http://docs.python.org/dev/library/%s.html' % term
urllib2.urlopen(url)
redirect(url)
except urllib2.HTTPError:
redirect('http://docs.python.org/search.html?q=%s'
'&check_keywords=yes&area=default' % term)

32 changes: 32 additions & 0 deletions cony/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def rich_help(help_argument = ''):
"""Decorator for command functions to mark them as providing help.
It causes the default cmd_help to link to them. The optional
`help_argument`, if set, is the argument passed to the linked command
for help.
Usage:
@rich_help('help')
def cmd_some_command(term):
\"\"\"Short help\"\"\"
...
or
@rich_help
def cmd_some_command(term):
\"\"\"Short help\"\"\"
...
"""
if callable(help_argument): # it means decorator was applied without args
func = help_argument
func.rich_help = ''
return func
else:
def decorator(handler):
handler.rich_help = help_argument
return handler
return decorator


19 changes: 0 additions & 19 deletions examples/search.py

This file was deleted.

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="cony",
version="0.2.0",
version="0.2.1",
description=(
'"Cony" is a tool to write smart bookmarks in '
'python and to share them across all your browsers and with a '
Expand All @@ -26,6 +26,6 @@
'Topic :: Internet :: WWW/HTTP :: WSGI :: Server',
],
packages=find_packages(),
scripts=['cony.py'],
scripts=['cony-server'],
install_requires = ['bottle'],
)

0 comments on commit 2e4051e

Please sign in to comment.