Changeset 2927
- Timestamp:
- 05/16/06 16:28:06 (2 years ago)
- Files:
-
- django/trunk/django/conf/__init__.py (modified) (3 diffs)
- django/trunk/django/template/defaultfilters.py (modified) (2 diffs)
- django/trunk/django/utils/translation.py (modified) (2 diffs)
- django/trunk/docs/i18n.txt (modified) (1 diff)
- django/trunk/docs/settings.txt (modified) (1 diff)
- django/trunk/docs/templates_python.txt (modified) (2 diffs)
- django/trunk/tests/othertests/templates.py (modified) (1 diff)
- django/trunk/tests/runtests.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/conf/__init__.py
r2809 r2927 13 13 ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE" 14 14 15 class LazySettings: 16 """ 17 A lazy proxy for either global Django settings or a custom settings object. 18 The user can manually configure settings prior to using them. Otherwise, 19 Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE. 20 """ 21 def __init__(self): 22 # _target must be either None or something that supports attribute 23 # access (getattr, hasattr, etc). 24 self._target = None 25 26 def __getattr__(self, name): 27 if self._target is None: 28 self._import_settings() 29 if name == '__members__': 30 # Used to implement dir(obj), for example. 31 return self._target.get_all_members() 32 return getattr(self._target, name) 33 34 def __setattr__(self, name, value): 35 if name == '_target': 36 self.__dict__['_target'] = value 37 else: 38 setattr(self._target, name, value) 39 40 def _import_settings(self): 41 """ 42 Load the settings module pointed to by the environment variable. This 43 is used the first time we need any settings at all, if the user has not 44 previously configured the settings manually. 45 """ 46 try: 47 settings_module = os.environ[ENVIRONMENT_VARIABLE] 48 if not settings_module: # If it's set but is an empty string. 49 raise KeyError 50 except KeyError: 51 raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE 52 53 self._target = Settings(settings_module) 54 55 def configure(self, default_settings=global_settings, **options): 56 """ 57 Called to manually configure the settings. The 'default_settings' 58 parameter sets where to retrieve any unspecified values from (its 59 argument must support attribute access (__getattr__)). 60 """ 61 if self._target != None: 62 raise EnvironmentError, 'Settings already configured.' 63 holder = UserSettingsHolder(default_settings) 64 for name, value in options.items(): 65 setattr(holder, name, value) 66 self._target = holder 67 15 68 class Settings: 16 17 69 def __init__(self, settings_module): 18 19 70 # update this dict from global settings (but only for ALL_CAPS settings) 20 71 for setting in dir(global_settings): … … 28 79 mod = __import__(self.SETTINGS_MODULE, '', '', ['']) 29 80 except ImportError, e: 30 raise EnvironmentError, "Could not import settings '%s' ( is it on sys.path?): %s" % (self.SETTINGS_MODULE, e)81 raise EnvironmentError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e) 31 82 32 83 # Settings that should be converted into tuples if they're mistakenly entered … … 57 108 os.environ['TZ'] = self.TIME_ZONE 58 109 59 # try to load DJANGO_SETTINGS_MODULE 60 try: 61 settings_module = os.environ[ENVIRONMENT_VARIABLE] 62 if not settings_module: # If it's set but is an empty string. 63 raise KeyError 64 except KeyError: 65 raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE 110 def get_all_members(self): 111 return dir(self) 66 112 67 # instantiate the configuration object 68 settings = Settings(settings_module) 113 class UserSettingsHolder: 114 """ 115 Holder for user configured settings. 116 """ 117 # SETTINGS_MODULE does not really make sense in the manually configured 118 # (standalone) case. 119 SETTINGS_MODULE = None 120 121 def __init__(self, default_settings): 122 """ 123 Requests for configuration variables not in this class are satisfied 124 from the module specified in default_settings (if possible). 125 """ 126 self.default_settings = default_settings 127 128 def __getattr__(self, name): 129 return getattr(self.default_settings, name) 130 131 def get_all_members(self): 132 return dir(self) + dir(self.default_settings) 133 134 settings = LazySettings() 69 135 70 136 # install the translation machinery so that it is available 71 137 from django.utils import translation 72 138 translation.install() 73 django/trunk/django/template/defaultfilters.py
r2905 r2927 328 328 ################### 329 329 330 def date(value, arg= settings.DATE_FORMAT):330 def date(value, arg=None): 331 331 "Formats a date according to the given format" 332 332 from django.utils.dateformat import format 333 if arg is None: 334 arg = settings.DATE_FORMAT 333 335 return format(value, arg) 334 336 335 def time(value, arg= settings.TIME_FORMAT):337 def time(value, arg=None): 336 338 "Formats a time according to the given format" 337 339 from django.utils.dateformat import time_format 340 if arg is None: 341 arg = settings.TIME_FORMAT 338 342 return time_format(value, arg) 339 343 … … 436 440 except Exception, e: 437 441 return "Error in formatting:%s" % e 438 442 439 443 # Syntax: register.filter(name of filter, callback) 440 444 register.filter(add) django/trunk/django/utils/translation.py
r2911 r2927 118 118 globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale') 119 119 120 parts = settings.SETTINGS_MODULE.split('.') 121 project = __import__(parts[0], {}, {}, []) 122 projectpath = os.path.join(os.path.dirname(project.__file__), 'locale') 120 if settings.SETTINGS_MODULE is not None: 121 parts = settings.SETTINGS_MODULE.split('.') 122 project = __import__(parts[0], {}, {}, []) 123 projectpath = os.path.join(os.path.dirname(project.__file__), 'locale') 124 else: 125 projectpath = None 123 126 124 127 def _fetch(lang, fallback=None): … … 156 159 res = _merge(localepath) 157 160 158 if os.path.isdir(projectpath):161 if projectpath and os.path.isdir(projectpath): 159 162 res = _merge(projectpath) 160 163 django/trunk/docs/i18n.txt
r2809 r2927 540 540 a big project out of several apps and put all translations into one big project 541 541 message file. The choice is yours. 542 543 .. note:: 544 545 If you're using manually configured settings, as described in the 546 `settings documentation`_, the ``locale`` directory in the project 547 directory will not be examined, since Django loses the ability to work out 548 the location of the project directory. (Django normally uses the location 549 of the settings file to determine this, and a settings file doesn't exist 550 if you're manually configuring your settings.) 551 552 .. _settings documentation: http://www.djangoproject.com/documentation/settings/#using-settings-without-the-django-settings-module-environment-variable 542 553 543 554 All message file repositories are structured the same way. They are: django/trunk/docs/settings.txt
r2809 r2927 725 725 purely for performance. 726 726 * Don't reinvent an already-existing setting. 727 728 Using settings without setting DJANGO_SETTINGS_MODULE 729 ===================================================== 730 731 In some cases, you might want to bypass the ``DJANGO_SETTINGS_MODULE`` 732 environment variable. For example, if you're using the template system by 733 itself, you likely don't want to have to set up an environment variable 734 pointing to a settings module. 735 736 In these cases, you can configure Django's settings manually. Do this by 737 calling ``django.conf.settings.configure()``. 738 739 Example:: 740 741 from django.conf import settings 742 743 settings.configure(DEBUG=True, TEMPLATE_DEBUG=True, 744 TEMPLATE_DIRS=('/home/web-apps/myapp', '/home/web-apps/base')) 745 746 Pass ``configure()`` as many keyword arguments as you'd like, with each keyword 747 argument representing a setting and its value. Each argument name should be all 748 uppercase, with the same name as the settings described above. If a particular 749 setting is not passed to ``configure()`` and is needed at some later point, 750 Django will use the default setting value. 751 752 Custom default settings 753 ----------------------- 754 755 If you'd like default values to come from somewhere other than 756 ``django.conf.global_settings``, you can pass in a module or class that 757 provides the default settings as the ``default_settings`` argument (or as the 758 first positional argument) in the call to ``configure()``. 759 760 In this example, default settings are taken from ``myapp_defaults``, and the 761 ``DEBUG`` setting is set to ``True``, regardless of its value in 762 ``myapp_defaults``:: 763 764 from django.conf import settings 765 from myapp import myapp_defaults 766 767 settings.configure(default_settings=myapp_defaults, DEBUG=True) 768 769 The following example, which uses ``myapp_defaults`` as a positional argument, 770 is equivalent:: 771 772 settings.configure(myapp_defaults, DEBUG = True) 773 774 Either configure() or DJANGO_SETTINGS_MODULE is required 775 -------------------------------------------------------- 776 777 If you're not setting the ``DJANGO_SETTINGS_MODULE`` environment variable, you 778 *must* call ``configure()`` at some point before using any code that reads 779 settings. 780 781 If you don't set ``DJANGO_SETTINGS_MODULE`` and don't call ``configure()``, 782 Django will raise an ``EnvironmentError`` exception the first time a setting 783 is accessed. 784 785 If you set ``DJANGO_SETTINGS_MODULE``, access settings values somehow, *then* 786 call ``configure()``, Django will raise an ``EnvironmentError`` saying settings 787 have already been configured. 788 789 Also, it's an error to call ``configure()`` more than once, or to call 790 ``configure()`` after any setting has been accessed. 791 792 It boils down to this: Use exactly one of either ``configure()`` or 793 ``DJANGO_SETTINGS_MODULE``. Not both, and not neither. django/trunk/docs/templates_python.txt
r2809 r2927 7 7 reference on the language syntax, see 8 8 `The Django template language: For template authors`_. 9 10 If you're looking to use the Django template system as part of another 11 application -- i.e., without the rest of the framework -- make sure to read 12 the `configuration`_ section later in this document. 9 13 10 14 .. _`The Django template language: For template authors`: http://www.djangoproject.com/documentation/templates/ … … 877 881 ``{% for %}``, ``{% ifequal %}`` and ``{% ifchanged %}``. They live in 878 882 ``django/template/defaulttags.py``. 883 884 .. _configuration: 885 886 Configuring the template system in standalone mode 887 ================================================== 888 889 .. note:: 890 891 This section is only of interest to people trying to use the template 892 system as an output component in another application. If you are using the 893 template system as part of a Django application, nothing here applies to 894 you. 895 896 Normally, Django will load all the configuration information it needs from its 897 own default configuration file, combined with the settings in the module given 898 in the ``DJANGO_SETTINGS_MODULE`` environment variable. But if you're using the 899 template system independently of the rest of Django, the environment variable 900 approach isn't very convenient, because you probably want to configure the 901 template system in line with the rest of your application rather than dealing 902 with settings files and pointing to them via environment variables. 903 904 To solve this problem, you need to use the manual configuration option 905 described in the `settings file`_ documentation. Simply import the appropriate 906 pieces of the templating system and then, *before* you call any of the 907 templating functions, call ``django.conf.settings.configure()`` with any 908 settings you wish to specify. You might want to consider setting at least 909 ``TEMPLATE_DIRS`` (if you are going to use template loaders), 910 ``DEFAULT_CHARSET`` (although the default of ``utf-8`` is probably fine) and 911 ``TEMPLATE_DEBUG``. All available settings are described in the 912 `settings documentation`_, and any setting starting with *TEMPLATE_* 913 is of obvious interest. 914 915 .. _settings file: http://www.djangoproject.com/documentation/settings/#using-settings-without-the-django-settings-module-environment-variable 916 .. _settings documentation: http://www.djangoproject.com/documentation/settings/ django/trunk/tests/othertests/templates.py
r2809 r2927 508 508 509 509 if __name__ == "__main__": 510 settings.configure() 510 511 run_tests(1, True) django/trunk/tests/runtests.py
r2809 r2927 73 73 def run_tests(self): 74 74 from django.conf import settings 75 76 # An empty access of the settings to force the default options to be 77 # installed prior to assigning to them. 78 settings.INSTALLED_APPS 75 79 76 80 # Manually set INSTALLED_APPS to point to the test models.
