Adjustment so that web.py works with unicode paths
authorW. Trevor King <wking@drexel.edu>
Tue, 20 Jul 2010 19:55:54 +0000 (15:55 -0400)
committerW. Trevor King <wking@drexel.edu>
Tue, 20 Jul 2010 19:55:54 +0000 (15:55 -0400)
dirtag/__init__.py
dirtag/web.py
test/raw/b/b1.svg

index f55b88f8e9f586691a6d44eb71810a968c95b780..175665cea41f385bcab9576dc1a459de5d047bb7 100644 (file)
@@ -357,7 +357,6 @@ class Dirtag (object):
 if __name__ == '__main__':
     # Uncomment the following lines if you're having problems with
     # UTF-8 in your docstrings.
-    #import sys
     #reload(sys)
     #sys.setdefaultencoding('utf-8')
 
index abbbcfde4f1248a055a1042a005f4041be803bac..0bee179ca6a1e8fb47cf1d92a58008f021276aa5 100755 (executable)
@@ -4,7 +4,7 @@ from mimetypes import guess_type
 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
@@ -23,6 +23,9 @@ class WebInterface:
         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
@@ -36,18 +39,27 @@ class WebInterface:
 
     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(
@@ -82,14 +94,15 @@ class WebInterface:
         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')
 
@@ -98,8 +111,9 @@ class WebInterface:
             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,
@@ -125,6 +139,7 @@ class WebInterface:
 
     @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'
@@ -189,8 +204,8 @@ class WebInterface:
     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):
@@ -202,10 +217,10 @@ class WebInterface:
 
     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,
@@ -217,7 +232,7 @@ class WebInterface:
         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':
@@ -226,20 +241,23 @@ class WebInterface:
             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>',
@@ -247,7 +265,7 @@ class WebInterface:
                 '</RDF:RDF>',
                 '',
                 ])
-        return '\n'.join(lines)
+        return '\n'.join(lines).encode('utf-8')
 
     # XUL user input methods
 
@@ -296,11 +314,16 @@ if __name__ == '__main__':
     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({
index bafb194ca3a3bfc041af5edd3fbb79c8a58dda94..99c4a3abea55b53e1ff9d60003bc4db00b9ab458 100644 (file)
@@ -1,8 +1,7 @@
-<?xml version="1.0" standalone="no"?>
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
 
-
 <svg xmlns="http://www.w3.org/2000/svg" version="1.1"
      width="100%" height="100%" viewBox="0 0 300 300">
   <title>b1.svg</title>