diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2010-06-12 22:51:11 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2010-06-12 22:51:11 -0400 |
commit | 3a3a11d32c72ab79ce94679aae7a549230c7104f (patch) | |
tree | 3ac36a302b85d5497b3d335238044c8f481ac0fd /forum/deps/livesettings/utils.py | |
parent | 8b6a10ead45bcd915f5da223de2b3fd3c30fc7b5 (diff) | |
download | askbot-3a3a11d32c72ab79ce94679aae7a549230c7104f.tar.gz askbot-3a3a11d32c72ab79ce94679aae7a549230c7104f.tar.bz2 askbot-3a3a11d32c72ab79ce94679aae7a549230c7104f.zip |
moved livesettings and django_authopenid into forum/deps
Diffstat (limited to 'forum/deps/livesettings/utils.py')
-rw-r--r-- | forum/deps/livesettings/utils.py | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/forum/deps/livesettings/utils.py b/forum/deps/livesettings/utils.py new file mode 100644 index 00000000..c0e0e293 --- /dev/null +++ b/forum/deps/livesettings/utils.py @@ -0,0 +1,87 @@ +import sys +import types +import os + +def can_loop_over(maybe): + """Test value to see if it is list like""" + try: + iter(maybe) + except: + return 0 + else: + return 1 + +def is_list_or_tuple(maybe): + return isinstance(maybe, (types.TupleType, types.ListType)) + + +def is_scalar(maybe): + """Test to see value is a string, an int, or some other scalar type""" + return is_string_like(maybe) or not can_loop_over(maybe) + +def is_string_like(maybe): + """Test value to see if it acts like a string""" + try: + maybe+"" + except TypeError: + return 0 + else: + return 1 + + +def flatten_list(sequence, scalarp=is_scalar, result=None): + """flatten out a list by putting sublist entries in the main list""" + if result is None: + result = [] + + for item in sequence: + if scalarp(item): + result.append(item) + else: + flatten_list(item, scalarp, result) + +def load_module(module): + """Load a named python module.""" + try: + module = sys.modules[module] + except KeyError: + __import__(module) + module = sys.modules[module] + return module + +def get_flat_list(sequence): + """flatten out a list and return the flat list""" + flat = [] + flatten_list(sequence, result=flat) + return flat + +def url_join(*args): + """Join any arbitrary strings into a forward-slash delimited string. + Do not strip leading / from first element, nor trailing / from last element. + + This function can take lists as arguments, flattening them appropriately. + + example: + url_join('one','two',['three','four'],'five') => 'one/two/three/four/five' + """ + if len(args) == 0: + return "" + + args = get_flat_list(args) + + if len(args) == 1: + return str(args[0]) + + else: + args = [str(arg).replace("\\", "/") for arg in args] + + work = [args[0]] + for arg in args[1:]: + if arg.startswith("/"): + work.append(arg[1:]) + else: + work.append(arg) + + joined = reduce(os.path.join, work) + + return joined.replace("\\", "/") |