Permalink
Browse files

more docs, pep8 and command line choice for server bind IP and Port

  • Loading branch information...
1 parent ba8874a commit 3a3ad566a2a21378b0ec3f254e118dec0b424921 @ThomasChiroux committed Oct 13, 2012
View
@@ -131,5 +131,5 @@ def run(self):
test_suite = 'nose.collector',
extras_require = {
'doc': ["sphinx", ],
- 'devel_tools': ["ipython", "pylint", "pep8", ],
+ 'devel_tools': ["ipython", "pylint", "pep8", "flake8",],
},)
View
@@ -33,4 +33,4 @@
__version__ = pkg_resources.resource_string("attowiki",
"RELEASE-VERSION").strip()
except IOError:
- __version__ = "0.0.0"
+ __version__ = "0.0.0"
View
@@ -24,19 +24,16 @@
# alphabetical order by last name
'Thomas Chiroux', ]
-
-import os
+# dependencies imports
import bottle
-from git import Repo,InvalidGitRepositoryError
+from git import Repo, InvalidGitRepositoryError
from docutils.parsers.rst import directives
from docutils import nodes, languages
-from docutils.parsers.rst.directives.admonitions import BaseAdmonition
-import docutils
-
-import serve_pages
-from rstdirective_todo import Todo
-from tools import attowiki_distro_path
+# project imports
+from attowiki import serve_pages
+from attowiki.rstdirective_todo import Todo
+from attowiki.tools import attowiki_distro_path
def main():
@@ -49,7 +46,7 @@ def main():
# small trick here: get_language will reveal languages.en
labels = languages.get_language('en').labels
# add the label
- languages.en.labels["todo"]="Todo"
+ languages.en.labels["todo"] = "Todo"
# add node
nodes._add_node_class_names(['todo', 'todolist'])
# register the new directive todo
@@ -62,19 +59,39 @@ def main():
Repo.init()
# add view path from module localisation
-
views_path = attowiki_distro_path() + '/views/'
bottle.TEMPLATE_PATH.insert(0, views_path)
app = bottle.Bottle()
- # Mission
- app.route('/', method='GET')(serve_pages.page)
- app.route('/', method='POST')(serve_pages.page)
- app.route('/edit/')(serve_pages.edit)
- app.route('/edit/<name>')(serve_pages.edit)
- app.route('/<name>.__iframe__', method='GET')(serve_pages.iframe)
- app.route('/<name>', method='GET')(serve_pages.page)
- app.route('/<name>', method='POST')(serve_pages.page)
- bottle.debug(True)
- bottle.run(app, host='localhost', port=8080)
+ # All the Urls of the project
+ # index or __index__
+ app.route('/', method='GET')(serve_pages.view_page)
+ # new page
+ app.route('/', method='POST')(serve_pages.view_page)
+ app.route('/edit/')(serve_pages.view_edit)
+ # edit an existing page
+ app.route('/edit/<name>')(serve_pages.view_edit)
+ # render an existing page using docutils
+ app.route('/<name>.__iframe__', method='GET')(serve_pages.view_iframe)
+ # view an existing page
+ app.route('/<name>', method='GET')(serve_pages.view_page)
+ # write new content to an existing page
+ app.route('/<name>', method='POST')(serve_pages.view_page)
+
+ # for devt purpose: set bottle in debug mode
+ bottle.debug(True) # this line may be commented in production mode
+
+ # run locally by default
+ from optparse import OptionParser
+ cmd_parser = OptionParser(usage="usage: %prog package.module:app")
+ cmd_options, cmd_args = cmd_parser.parse_args()
+ if len(cmd_args) >= 2:
+ host, port = (cmd_args[0] or 'localhost'), (cmd_args[1] or 8080)
+ elif len(cmd_args) == 1:
+ host, port = (cmd_args[0] or 'localhost'), (8080)
+ else:
+ host, port = ('localhost'), (8080)
+ if ':' in host:
+ host, port = host.rsplit(':', 1)
+ bottle.run(app, host=host, port=port)
@@ -17,26 +17,25 @@
# along with this program.
# If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>
#
+"""adds restructured text directives to docutils
+"""
__authors__ = [
# alphabetical order by last name
'Thomas Chiroux', ]
-from docutils.parsers.rst import Directive
-from docutils.parsers.rst import states, directives
-from docutils.parsers.rst.roles import set_classes
from docutils.parsers.rst.directives.admonitions import BaseAdmonition
from docutils import nodes
class todo(nodes.Admonition, nodes.Element):
- pass
-
-
-class todolist(nodes.General, nodes.Element):
+ """todo node for docutils"""
pass
class Todo(BaseAdmonition):
- node_class = todo
+ """todo directive for docutils
+ uses BaseAdmonition from docutils (like .. note:: of .. warning:: etc..)
+ """
+ node_class = todo
View
@@ -17,79 +17,156 @@
# along with this program.
# If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>
#
+"""bottle views for attowiki
+"""
__authors__ = [
# alphabetical order by last name
'Thomas Chiroux', ]
import glob
+import datetime
# dependencies imports
from bottle import request, response, template, abort
from docutils.core import publish_string
from docutils.writers.html4css1 import Writer as HisWriter
-from tools import attowiki_distro_path
from git import Repo, InvalidGitRepositoryError
+# project imports
+from attowiki.tools import attowiki_distro_path
-def index():
- """looks for index.rst file and serve it.
- If not found, list all the available files
+
+def view_meta_index():
+ """List all the available .rst files in the directory
+
+ index is called by the 'meta' url : /__index__
"""
- rst_files = [file[2:-4] for file in glob.glob("./*.rst")]
+ rst_files = [filename[2:-4] for filename in glob.glob("./*.rst")]
return template('index', filelist=rst_files, name="__index__")
def check_repo():
+ """checks is local git repo is present or not
+
+ Keywords Arguments:
+ <none>
+
+ Returns:
+ boolean -- True if git repo is present, False if not
+ """
try:
- repo = Repo()
+ Repo()
except InvalidGitRepositoryError:
return False
return True
def commit(filename):
+ """Commit (git) a specified file
+
+ This method does the same than a ::
+
+ $ git commit -a "message"
+
+ Keyword Arguments:
+ :filename: (str) -- name of the file to commit
+
+ Returns:
+ <nothing>
+ """
try:
repo = Repo()
index = repo.index
index.commit("Updated file: {0}".format(filename))
- except:
+ except Exception:
pass
def add_file_to_repo(filename):
+ """Add a file to the git repo
+
+ This method does the same than a ::
+
+ $ git add filename
+
+ Keyword Arguments:
+ :filename: (str) -- name of the file to commit
+
+ Returns:
+ <nothing>
+ """
try:
repo = Repo()
index = repo.index
index.add([filename])
- except:
+ except Exception:
pass
-def edit(name=None):
+def view_edit(name=None):
"""edit or creates a new page
+
+ .. note:: this is a bottle view
+
+ if no page name is given, creates a new page.
+
+ Keyword Arguments:
+ :name: (str) -- name of the page (OPTIONAL)
+
+ Returns:
+ bottle response object
"""
response.set_header('Cache-control', 'no-cache')
response.set_header('Pragma', 'no-cache')
if name is None:
# new page
- return template('edit', name=name, display_name=name,
+ return template('edit',
+ name=name,
+ display_name=name,
is_repo=check_repo(),
+ today=datetime.datetime.now().strftime("%Y%m%d"),
content="")
else:
files = glob.glob("{0}.rst".format(name))
if len(files) > 0:
- file = open(files[0], 'r')
- return template('edit', name=name, display_name=name,
+ file_handle = open(files[0], 'r')
+ return template('edit',
+ name=name,
+ display_name=name,
is_repo=check_repo(),
- content=file.read())
+ today=datetime.datetime.now().strftime("%Y%m%d"),
+ content=file_handle.read())
else:
return abort(404)
-def page(name=None):
+def view_page(name=None):
"""serve a page name
+
+ .. note:: this is a bottle view
+
+ * if the view is called with the POST method, write the new page
+ content to the file, commit the modification and then display the
+ html rendering of the restructured text file
+
+ * if the view is called with the GET method, directly display the html
+ rendering of the restructured text file
+
+ This view dot not render the .rst file directly: it display a small
+ header at the top of the page and an iframe below which is directed
+ to a meta page : name.__iframe__ (see :func:`view_iframe` for more infos)
+
+ Keyword Arguments:
+ :name: (str) -- name of the rest file (without the .rst extension)
+ OPTIONAL
+
+ if no filename is given, first try to find a "index.rst" file in the
+ directory and serve it. If not found, serve the meta page __index__
+
+ Returns:
+ bottle response object
"""
if request.method == 'POST':
if name is None:
@@ -99,9 +176,9 @@ def page(name=None):
if name is not None:
filename = "{0}.rst".format(name)
- file = open(filename, 'w')
- file.write(request.forms.content.encode('utf-8'))
- file.close()
+ file_handle = open(filename, 'w')
+ file_handle.write(request.forms.content.encode('utf-8'))
+ file_handle.close()
add_file_to_repo(filename)
commit(filename)
@@ -119,25 +196,31 @@ def page(name=None):
return template('page', name=name, display_name=name, is_repo=check_repo())
-def iframe(name):
+def view_iframe(name):
"""serve the iframe : the html converted rst file
+
+ .. note:: this is a bottle view
+
+ Take a filename in argument (without .rst) and uses docutils to
+ render the rst file in html.
+
+ Keyword Arguments:
+ :name: (str) -- name of the file (MANDATORY)
"""
- args = {
- 'stylesheet_path' : attowiki_distro_path()
- + '/views/attowiki_docutils.css'
- }
+ args = {'stylesheet_path':
+ attowiki_distro_path() + '/views/attowiki_docutils.css'}
response.set_header('Cache-control', 'no-cache')
response.set_header('Pragma', 'no-cache')
if name == '__index__':
# we should generate and index page
- return index()
+ return view_meta_index()
else:
files = glob.glob("{0}.rst".format(name))
if len(files) > 0:
- file = open(files[0], 'r')
- return publish_string(file.read(),
+ file_handle = open(files[0], 'r')
+ return publish_string(file_handle.read(),
writer=HisWriter(),
settings=None,
settings_overrides=args)
View
@@ -37,4 +37,4 @@ def attowiki_distro_path():
attowiki_path = attowiki_path[:attowiki_path.rfind('/')]
else:
attowiki_path = attowiki_path[:attowiki_path[:-1].rfind('/')]
- return attowiki_path
+ return attowiki_path
@@ -20,7 +20,7 @@
%if name is None:
<form name="page_edit" method="post" action="/">
- filename: <input name="filename" size="20"></input>
+ filename: <input name="filename" size="20" value="{{today}}_"></input>
%else:
<form name="page_edit" method="post" action="/{{display_name}}">
%end

0 comments on commit 3a3ad56

Please sign in to comment.