summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorDerek Anderson <public@kered.org>2006-10-26 19:09:51 +0000
committerDerek Anderson <public@kered.org>2006-10-26 19:09:51 +0000
commit42851d90dadbf62f5d342ce5c4f496ba1eeba987 (patch)
treea5d0e5c178afb2d7dbb7bf5ab37db9ced42f4b52 /django/utils
parent450889c9a6f7da3c2fce77a0ccf4c4cea9e29710 (diff)
committing to schema-evolution
merge from HEAD git-svn-id: http://code.djangoproject.com/svn/django/branches/schema-evolution@3937 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/autoreload.py8
-rw-r--r--django/utils/datastructures.py31
-rw-r--r--django/utils/functional.py4
-rw-r--r--django/utils/itercompat.py31
-rw-r--r--django/utils/simplejson/scanner.py3
-rw-r--r--django/utils/termcolors.py2
-rw-r--r--django/utils/text.py6
-rw-r--r--django/utils/translation/trans_null.py11
8 files changed, 78 insertions, 18 deletions
diff --git a/django/utils/autoreload.py b/django/utils/autoreload.py
index 6363af7835..e05b7fafe1 100644
--- a/django/utils/autoreload.py
+++ b/django/utils/autoreload.py
@@ -35,6 +35,14 @@ try:
except ImportError:
import dummy_thread as thread
+# This import does nothing, but it's necessary to avoid some race conditions
+# in the threading module. See http://code.djangoproject.com/ticket/2330 .
+try:
+ import threading
+except ImportError:
+ pass
+
+
RUN_RELOADER = True
def reloader_thread():
diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
index 632e804f26..cecb4da170 100644
--- a/django/utils/datastructures.py
+++ b/django/utils/datastructures.py
@@ -14,6 +14,9 @@ class MergeDict(object):
pass
raise KeyError
+ def __contains__(self, key):
+ return self.has_key(key)
+
def get(self, key, default):
try:
return self[key]
@@ -187,17 +190,23 @@ class MultiValueDict(dict):
"Returns a copy of this object."
return self.__deepcopy__()
- def update(self, other_dict):
- "update() extends rather than replaces existing key lists."
- if isinstance(other_dict, MultiValueDict):
- for key, value_list in other_dict.lists():
- self.setlistdefault(key, []).extend(value_list)
- else:
- try:
- for key, value in other_dict.items():
- self.setlistdefault(key, []).append(value)
- except TypeError:
- raise ValueError, "MultiValueDict.update() takes either a MultiValueDict or dictionary"
+ def update(self, *args, **kwargs):
+ "update() extends rather than replaces existing key lists. Also accepts keyword args."
+ if len(args) > 1:
+ raise TypeError, "update expected at most 1 arguments, got %d", len(args)
+ if args:
+ other_dict = args[0]
+ if isinstance(other_dict, MultiValueDict):
+ for key, value_list in other_dict.lists():
+ self.setlistdefault(key, []).extend(value_list)
+ else:
+ try:
+ for key, value in other_dict.items():
+ self.setlistdefault(key, []).append(value)
+ except TypeError:
+ raise ValueError, "MultiValueDict.update() takes either a MultiValueDict or dictionary"
+ for key, value in kwargs.iteritems():
+ self.setlistdefault(key, []).append(value)
class DotExpandedDict(dict):
"""
diff --git a/django/utils/functional.py b/django/utils/functional.py
index d1514d5728..e3c0a3c76b 100644
--- a/django/utils/functional.py
+++ b/django/utils/functional.py
@@ -1,6 +1,6 @@
-def curry(*args, **kwargs):
+def curry(_curried_func, *args, **kwargs):
def _curried(*moreargs, **morekwargs):
- return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))
+ return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
return _curried
class Promise:
diff --git a/django/utils/itercompat.py b/django/utils/itercompat.py
new file mode 100644
index 0000000000..370988bedb
--- /dev/null
+++ b/django/utils/itercompat.py
@@ -0,0 +1,31 @@
+"""
+Providing iterator functions that are not in all version of Python we support.
+Where possible, we try to use the system-native version and only fall back to
+these implementations if necessary.
+"""
+
+import itertools
+
+def compat_tee(iterable):
+ """Return two independent iterators from a single iterable.
+
+ Based on http://www.python.org/doc/2.3.5/lib/itertools-example.html
+ """
+ # Note: Using a dictionary and a list as the default arguments here is
+ # deliberate and safe in this instance.
+ def gen(next, data={}, cnt=[0]):
+ dpop = data.pop
+ for i in itertools.count():
+ if i == cnt[0]:
+ item = data[i] = next()
+ cnt[0] += 1
+ else:
+ item = dpop(i)
+ yield item
+ next = iter(iterable).next
+ return gen(next), gen(next)
+
+if hasattr(itertools, 'tee'):
+ tee = itertools.tee
+else:
+ tee = compat_tee
diff --git a/django/utils/simplejson/scanner.py b/django/utils/simplejson/scanner.py
index c2e9b6eb89..b9244cfed1 100644
--- a/django/utils/simplejson/scanner.py
+++ b/django/utils/simplejson/scanner.py
@@ -3,12 +3,11 @@ Iterator based sre token scanner
"""
import sre_parse, sre_compile, sre_constants
from sre_constants import BRANCH, SUBPATTERN
-from sre import VERBOSE, MULTILINE, DOTALL
import re
__all__ = ['Scanner', 'pattern']
-FLAGS = (VERBOSE | MULTILINE | DOTALL)
+FLAGS = (re.VERBOSE | re.MULTILINE | re.DOTALL)
class Scanner(object):
def __init__(self, lexicon, flags=FLAGS):
self.actions = [None]
diff --git a/django/utils/termcolors.py b/django/utils/termcolors.py
index 3ce1d5bb6b..17a600f899 100644
--- a/django/utils/termcolors.py
+++ b/django/utils/termcolors.py
@@ -2,8 +2,6 @@
termcolors.py
"""
-import types
-
color_names = ('black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white')
foreground = dict([(color_names[x], '3%s' % x) for x in range(8)])
background = dict([(color_names[x], '4%s' % x) for x in range(8)])
diff --git a/django/utils/text.py b/django/utils/text.py
index 7df9bc03b7..9e7bb3b6c4 100644
--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -94,7 +94,8 @@ def compress_string(s):
return zbuf.getvalue()
ustring_re = re.compile(u"([\u0080-\uffff])")
-def javascript_quote(s):
+
+def javascript_quote(s, quote_double_quotes=False):
def fix(match):
return r"\u%04x" % ord(match.group(1))
@@ -104,9 +105,12 @@ def javascript_quote(s):
elif type(s) != unicode:
raise TypeError, s
s = s.replace('\\', '\\\\')
+ s = s.replace('\r', '\\r')
s = s.replace('\n', '\\n')
s = s.replace('\t', '\\t')
s = s.replace("'", "\\'")
+ if quote_double_quotes:
+ s = s.replace('"', '&quot;')
return str(ustring_re.sub(fix, s))
smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)')
diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py
index ed25cae05b..75ad573357 100644
--- a/django/utils/translation/trans_null.py
+++ b/django/utils/translation/trans_null.py
@@ -17,3 +17,14 @@ get_language = lambda: settings.LANGUAGE_CODE
get_language_bidi = lambda: settings.LANGUAGE_CODE in settings.LANGUAGES_BIDI
get_date_formats = lambda: (settings.DATE_FORMAT, settings.DATETIME_FORMAT, settings.TIME_FORMAT)
get_partial_date_formats = lambda: (settings.YEAR_MONTH_FORMAT, settings.MONTH_DAY_FORMAT)
+check_for_language = lambda x: True
+
+def to_locale(language):
+ p = language.find('-')
+ if p >= 0:
+ return language[:p].lower()+'_'+language[p+1:].upper()
+ else:
+ return language.lower()
+
+def get_language_from_request(request):
+ return settings.LANGUAGE_CODE