summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark McClain <mark.mcclain@shootq.com>2011-03-06 20:25:17 -0500
committerMark McClain <mark.mcclain@shootq.com>2011-03-06 20:25:17 -0500
commit046c3c275ed3305a017c437ab37c410472339f53 (patch)
treefc5d4dcbf4c9da22192afd4a8881b302867b0634
parentb3d49515913512e7615a2befe8af77914cc1c58b (diff)
downloadpecan-046c3c275ed3305a017c437ab37c410472339f53.tar.gz
more robust splitext for python2.5
-rw-r--r--pecan/core.py28
-rw-r--r--pecan/util.py25
-rw-r--r--tests/test_util.py11
3 files changed, 41 insertions, 23 deletions
diff --git a/pecan/core.py b/pecan/core.py
index 4dae9b9..555fba4 100644
--- a/pecan/core.py
+++ b/pecan/core.py
@@ -1,6 +1,6 @@
from templating import RendererFactory
from routing import lookup_controller, NonCanonicalPath
-from util import _cfg
+from util import _cfg, splitext
from webob import Request, Response, exc
from threading import local
@@ -199,20 +199,6 @@ class Pecan(object):
self.template_path = template_path
self.force_canonical = force_canonical
- def get_content_type(self, format):
- '''
- Returns a content-type for a file extension.
-
- :param format: The file extension, such as .html, .json, or .txt.
- '''
-
- return {
- '.html' : 'text/html',
- '.xhtml' : 'text/html',
- '.json' : 'application/json',
- '.txt' : 'text/plain'
- }.get(format)
-
def route(self, node, path):
'''
Looks up a controller from a node based upon the specified path.
@@ -371,15 +357,11 @@ class Pecan(object):
path = request.routing_path
if state.content_type is None and '.' in path.split('/')[-1]:
- state.content_type = guess_type(path)[0]
+ path, extension = splitext(path)
- # store the extension for retrieval by controllers
- ext_index = path.rfind('.')
- if ext_index > 1:
- request.extension = path[ext_index:]
- path = path[:ext_index]
- else:
- request.extension = ''
+ request.extension = extension
+ # preface with a letter to ensure compat for 2.5
+ state.content_type = guess_type('x' + extension)[0]
controller, remainder = self.route(self.root, path)
cfg = _cfg(controller)
diff --git a/pecan/util.py b/pecan/util.py
index b4aeb12..3a311d5 100644
--- a/pecan/util.py
+++ b/pecan/util.py
@@ -1,6 +1,31 @@
+import sys
+import os
+
def iscontroller(obj):
return getattr(obj, 'exposed', False)
def _cfg(f):
if not hasattr(f, '_pecan'): f._pecan = {}
return f._pecan
+
+def compat_splitext(path):
+ """
+ This method emulates the behavior os.path.splitext introduced in python 2.6
+ """
+ basename = os.path.basename(path)
+
+ index = basename.rfind('.')
+ if index > 0:
+ root = basename[:index]
+ if root.count('.') != index:
+ return (os.path.join(os.path.dirname(path), root), basename[index:])
+
+ return (path, '')
+
+# use the builtin splitext unless we're python 2.5
+if sys.version_info >= (2,6):
+ from os.path import splitext
+else: #pragma no cover
+ splitext = compat_splitext
+
+
diff --git a/tests/test_util.py b/tests/test_util.py
new file mode 100644
index 0000000..109e0be
--- /dev/null
+++ b/tests/test_util.py
@@ -0,0 +1,11 @@
+from pecan.util import compat_splitext
+
+def test_compat_splitext():
+ assert ('foo', '.bar') == compat_splitext('foo.bar')
+ assert ('/foo/bar', '.txt') == compat_splitext('/foo/bar.txt')
+ assert ('/foo/bar', '') == compat_splitext('/foo/bar')
+ assert ('.bashrc', '') == compat_splitext('.bashrc')
+ assert ('..bashrc', '') == compat_splitext('..bashrc')
+ assert ('/.bashrc', '') == compat_splitext('/.bashrc')
+ assert ('/foo.bar/.bashrc', '') == compat_splitext('/foo.bar/.bashrc')
+ assert ('/foo.js', '.js') == compat_splitext('/foo.js.js')