from optparse import OptionParser
import os
from os import path
-from urllib import urlencode
+from urllib import urlencode, quote_plus
import cherrypy
from jinja2 import Environment, FileSystemLoader
self.repository_name = repository_name
self.rdf_root = 'http://dirtag.com/'
+ def _url(self, url):
+ return unicode(quote_plus(url.encode('utf-8')), 'utf-8')
+
# RESTful HTML interface
@cherrypy.expose
def _dir_html(self, root_dir):
"""
- >>> x = WebInterface(None)
- >>> print x._dir_html('test/tag')
+ >>> d = Dirtag(raw_dir=u'test/raw', tag_dir=u'test/tag')
+ >>> x = WebInterface(d)
+ >>> print x._dir_html(d.tag_dir)
<ul>
- <li><a href="">1</a></li>
+ <li><a href="dir?selected=1&root=tag">1</a></li>
<BLANKLINE>
- <li>x
+ <li><a href="dir?selected=x&root=tag">x</a>
<ul>
- <li><a href="">x/y</a></li>
+ <li><a href="dir?selected=x%2Fy&root=tag">x/y</a></li>
<BLANKLINE>
</ul>
</li>
</ul>
+ >>> for line in x._dir_html(d.raw_dir).split('\\n'):
+ ... print repr(line)
+ u'<ul>'
+ u' <li><a href="dir?selected=b&root=raw">b</a></li>'
+ u''
+ u' <li><a href="dir?selected=%CE%B1&root=raw">\u03b1</a></li>'
+ u''
+ u'</ul>'
"""
lines = ['<ul>']
lines.extend([x for x in dir_tree(root_dir).hook_out(
if node.root == self.dirtag.tag_dir:
root = 'tag'
return ('<a href="%s">%s</a>'
- % ('dir?%s' % urlencode({
- 'root':root,
- 'selected':'/'.join(node),
- }),
+ % ('dir?%s' % unicode(urlencode({
+ 'root':root,
+ 'selected':'/'.join(node).encode('utf-8'),
+ }), 'utf-8'),
'/'.join(node)))
@cherrypy.expose
def dir(self, root, selected):
+ selected = unicode(selected, 'utf-8')
s = self._selected_dir(root, selected)
template = self.env.get_template('dir.html')
files=[(f[-1],
'/'.join(f),
self._dir_tags(f),
- 'file?%s' % urlencode({'selected':'/'.join(
- self.dirtag.raw_node(f))}),
+ unicode('file?%s' % urlencode({'selected':'/'.join(
+ self.dirtag.raw_node(f)).encode('utf-8')}
+ ), 'utf-8'),
self._preview(f))
for f in s.tree.traverse(prefix=[s.root]+s[:-1],
depth=1,
@cherrypy.expose
def file(self, selected):
+ selected = unicode(selected, 'utf-8')
# Disable form value caching in Firefox. See
# http://www.mozilla.org/projects/netlib/http/http-caching-faq.html
cherrypy.response.headers['Cache-Control'] = 'no-store'
def dirtag_xul(self):
template = self.env.get_template('dirtag.xul')
cherrypy.response.headers['Content-Type'] = \
- 'application/vnd.mozilla.xul+xml'
- return template.render(repository_name=self.repository_name)
+ 'application/vnd.mozilla.xul+xml; charset=utf-8'
+ return template.render(repository_name=self.repository_name).encode('utf-8')
@cherrypy.expose
def raw_rdf(self):
def tree_rdf(self, root_dir, name, with_files=True):
cherrypy.response.headers['Content-Type'] = \
- 'application/rdf+xml'
+ 'application/rdf+xml; charset=utf-8'
tree = dir_tree(root_dir)
lines = [
- '<?xml version="1.0"?>',
+ '<?xml version="1.0" encoding="UTF-8"?>',
'',
'<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"',
' xmlns:FILES="%srdf#">' % self.rdf_root,
for node in nodes:
lines.extend([
' <RDF:Description RDF:about="%s%s">'
- % (self.rdf_root, '/'.join([name]+node)),
+ % (self.rdf_root, self._url('/'.join([name]+node))),
' <FILES:name>%s</FILES:name>' % node[-1],
])
if node.type == 'file':
lines.append(' </RDF:Description>')
lines.append(' <RDF:Seq RDF:about="%s%s/files">'
- % (self.rdf_root, name))
+ % (self.rdf_root, self._url(name)))
lines.extend([x for x in tree.hook_out(
data=self, depth=1,
on_open_dir=lambda node,data: (
'%s<RDF:li>\n%s<RDF:Seq RDF:about="%s%s">'
% (' '*(4*len(node)), ' '*(4*len(node)+2),
- data.rdf_root, '/'.join([name]+node))),
+ data.rdf_root,
+ self._url('/'.join([name]+node)))),
on_close_dir=lambda node,data: (
'%s</RDF:Seq>\n%s</RDF:li>'
% (' '*(4*len(node)+2), ' '*(4*len(node)))),
on_file=lambda node,data: (
'%s<RDF:li RDF:resource="%s%s"/>'
% (' '*(4*len(node)), data.rdf_root,
- '/'.join(['raw']+data.dirtag.raw_node(node)))),
+ self._url(
+ '/'.join(['raw']+data.dirtag.raw_node(node))
+ ))),
)])
lines.extend([
' </RDF:Seq>',
'</RDF:RDF>',
'',
])
- return '\n'.join(lines)
+ return '\n'.join(lines).encode('utf-8')
# XUL user input methods
options,args = parser.parse_args()
if options.test:
- import doctest, sys
+ import sys
+ reload(sys)
+ sys.setdefaultencoding('utf-8')
+
+ import doctest
doctest.testmod()
sys.exit(0)
- d = Dirtag(raw_dir=options.raw_dir, tag_dir=options.tag_dir)
+ d = Dirtag(raw_dir=unicode(options.raw_dir),
+ tag_dir=unicode(options.tag_dir))
w = WebInterface(
d, template_dir=options.template_dir, repository_name=options.name)
cherrypy.config.update({