Changeset 874
- Timestamp:
- 10/14/05 18:32:35 (3 years ago)
- Files:
-
- django/branches/i18n/django/conf/global_settings.py (modified) (1 diff)
- django/branches/i18n/django/core/template/loader.py (modified) (1 diff)
- django/branches/i18n/django/core/template/loaders/eggs.py (added)
- django/branches/i18n/django/core/template/loaders/filesystem.py (modified) (1 diff)
- django/branches/i18n/ez_setup.py (modified) (8 diffs)
- django/branches/i18n/tests/othertests/templates.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/i18n/django/conf/global_settings.py
r869 r874 71 71 # Extension on all templates. 72 72 TEMPLATE_FILE_EXTENSION = '.html' 73 74 # List of callables that know how to import templates from various sources. 75 # See the comments in django/core/template/loader.py for interface 76 # documentation. 77 TEMPLATE_LOADERS = ( 78 'django.core.template.loaders.filesystem.load_template_source', 79 # 'django.core.template.loaders.eggs.load_template_source', 80 ) 73 81 74 82 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a django/branches/i18n/django/core/template/loader.py
r869 r874 1 "Wrapper for loading templates from storage of some sort (e.g. files or db)" 2 1 # Wrapper for loading templates from storage of some sort (e.g. filesystem, database). 2 # 3 # This uses the TEMPLATE_LOADERS setting, which is a list of loaders to use. 4 # Each loader is expected to have this interface: 5 # 6 # callable(name, dirs=[]) 7 # 8 # name is the template name. 9 # dirs is an optional list of directories to search instead of TEMPLATE_DIRS. 10 # 11 # Each loader should have an "is_usable" attribute set. This is a boolean that 12 # specifies whether the loader can be used in this Python installation. Each 13 # loader is responsible for setting this when it's initialized. 14 # 15 # For example, the eggs loader (which is capable of loading templates from 16 # Python eggs) sets is_usable to False if the "pkg_resources" module isn't 17 # installed, because pkg_resources is necessary to read eggs. 18 19 from django.core.exceptions import ImproperlyConfigured 3 20 from django.core.template import Template, Context, Node, TemplateDoesNotExist, TemplateSyntaxError, resolve_variable_with_filters, register_tag 4 from django.core.template.loaders.filesystem import load_template_source 21 from django.conf.settings import TEMPLATE_LOADERS 22 23 template_source_loaders = [] 24 for path in TEMPLATE_LOADERS: 25 i = path.rfind('.') 26 module, attr = path[:i], path[i+1:] 27 try: 28 mod = __import__(module, globals(), locals(), [attr]) 29 except ImportError, e: 30 raise ImproperlyConfigured, 'Error importing template source loader %s: "%s"' % (module, e) 31 try: 32 func = getattr(mod, attr) 33 except AttributeError: 34 raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template source loader' % (module, attr) 35 if not func.is_usable: 36 import warnings 37 warnings.warn("Your TEMPLATE_LOADERS setting includes %r, but your Python installation doesn't support that type of template loading. Consider removing that line from TEMPLATE_LOADERS." % path) 38 else: 39 template_source_loaders.append(func) 40 41 def load_template_source(name, dirs=None): 42 for loader in template_source_loaders: 43 try: 44 return loader(name, dirs) 45 except TemplateDoesNotExist: 46 pass 47 raise TemplateDoesNotExist, name 5 48 6 49 class ExtendsError(Exception): django/branches/i18n/django/core/template/loaders/filesystem.py
r869 r874 20 20 error_msg = "Your TEMPLATE_DIRS settings is empty. Change it to point to at least one template directory." 21 21 raise TemplateDoesNotExist, error_msg 22 load_template_source.is_usable = True django/branches/i18n/ez_setup.py
r66 r874 14 14 This file can also be run as a script to install or upgrade setuptools. 15 15 """ 16 17 DEFAULT_VERSION = "0.5a12" 18 DEFAULT_URL = "http://www.python.org/packages/source/s/setuptools/" 16 import sys 17 DEFAULT_VERSION = "0.6a5" 18 DEFAULT_URL = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3] 19 20 md5_data = { 21 'setuptools-0.5a13-py2.3.egg': '85edcf0ef39bab66e130d3f38f578c86', 22 'setuptools-0.5a13-py2.4.egg': 'ede4be600e3890e06d4ee5e0148e092a', 23 'setuptools-0.6a1-py2.3.egg': 'ee819a13b924d9696b0d6ca6d1c5833d', 24 'setuptools-0.6a1-py2.4.egg': '8256b5f1cd9e348ea6877b5ddd56257d', 25 'setuptools-0.6a2-py2.3.egg': 'b98da449da411267c37a738f0ab625ba', 26 'setuptools-0.6a2-py2.4.egg': 'be5b88bc30aed63fdefd2683be135c3b', 27 'setuptools-0.6a3-py2.3.egg': 'ee0e325de78f23aab79d33106dc2a8c8', 28 'setuptools-0.6a3-py2.4.egg': 'd95453d525a456d6c23e7a5eea89a063', 29 'setuptools-0.6a4-py2.3.egg': 'e958cbed4623bbf47dd1f268b99d7784', 30 'setuptools-0.6a4-py2.4.egg': '7f33c3ac2ef1296f0ab4fac1de4767d8', 31 'setuptools-0.6a5-py2.3.egg': '748408389c49bcd2d84f6ae0b01695b1', 32 'setuptools-0.6a5-py2.4.egg': '999bacde623f4284bfb3ea77941d2627', 33 } 19 34 20 35 import sys, os 21 36 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 37 def _validate_md5(egg_name, data): 38 if egg_name in md5_data: 39 from md5 import md5 40 digest = md5(data).hexdigest() 41 if digest != md5_data[egg_name]: 42 print >>sys.stderr, ( 43 "md5 validation of %s failed! (Possible download problem?)" 44 % egg_name 45 ) 46 sys.exit(2) 47 return data 40 48 41 49 42 50 def use_setuptools( 43 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir 51 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, 52 download_delay=15 44 53 ): 45 54 """Automatically find/download setuptools and make it available on sys.path … … 48 57 as an egg for download under the `download_base` URL (which should end with 49 58 a '/'). `to_dir` is the directory where setuptools will be downloaded, if 50 it is not already available. 51 52 If an older version of setuptools is installed, this will print a message53 t o ``sys.stderr`` and raise SystemExit in an attempt to abort the calling54 script.59 it is not already available. If `download_delay` is specified, it should 60 be the number of seconds that will be paused before initiating a download, 61 should one be required. If an older version of setuptools is installed, 62 this routine will print a message to ``sys.stderr`` and raise SystemExit in 63 an attempt to abort the calling script. 55 64 """ 56 65 try: … … 62 71 ) 63 72 sys.exit(2) 64 65 73 except ImportError: 66 egg = download_setuptools(version, download_base, to_dir )74 egg = download_setuptools(version, download_base, to_dir, download_delay) 67 75 sys.path.insert(0, egg) 68 76 import setuptools; setuptools.bootstrap_install_from = egg … … 82 90 83 91 def download_setuptools( 84 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir 92 version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, 93 delay = 15 85 94 ): 86 95 """Download setuptools from a specified location and return its filename … … 89 98 as an egg for download under the `download_base` URL (which should end 90 99 with a '/'). `to_dir` is the directory where the egg will be downloaded. 100 `delay` is the number of seconds to pause before an actual download attempt. 91 101 """ 92 102 import urllib2, shutil 93 103 egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3]) 94 url = download_base + egg_name + '.zip' # XXX104 url = download_base + egg_name 95 105 saveto = os.path.join(to_dir, egg_name) 96 106 src = dst = None 97 98 107 if not os.path.exists(saveto): # Avoid repeated downloads 99 108 try: 100 109 from distutils import log 110 if delay: 111 log.warn(""" 112 --------------------------------------------------------------------------- 113 This script requires setuptools version %s to run (even to display 114 help). I will attempt to download it for you (from 115 %s), but 116 you may need to enable firewall access for this script first. 117 I will start the download in %d seconds. 118 ---------------------------------------------------------------------------""", 119 version, download_base, delay 120 ); from time import sleep; sleep(delay) 101 121 log.warn("Downloading %s", url) 102 122 src = urllib2.urlopen(url) 103 123 # Read/write all in one block, so we don't create a corrupt file 104 124 # if the download is interrupted. 105 data = src.read() 106 dst = open(saveto,"wb") 107 dst.write(data) 125 data = _validate_md5(egg_name, src.read()) 126 dst = open(saveto,"wb"); dst.write(data) 108 127 finally: 109 128 if src: src.close() 110 129 if dst: dst.close() 111 112 130 return os.path.realpath(saveto) 113 114 115 116 117 118 119 120 121 122 123 131 124 132 def main(argv, version=DEFAULT_VERSION): … … 131 139 tmpdir = tempfile.mkdtemp(prefix="easy_install-") 132 140 try: 133 egg = download_setuptools(version, to_dir=tmpdir )141 egg = download_setuptools(version, to_dir=tmpdir, delay=0) 134 142 sys.path.insert(0,egg) 135 143 from setuptools.command.easy_install import main … … 151 159 except ImportError: 152 160 from easy_install import main 153 main(list(argv)+[download_setuptools( )])161 main(list(argv)+[download_setuptools(delay=0)]) 154 162 sys.exit(0) # try to force an exit 155 163 else: … … 160 168 print "Setuptools version",version,"or greater has been installed." 161 169 print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' 170 171 172 173 def update_md5(filenames): 174 """Update our built-in md5 registry""" 175 176 import re 177 from md5 import md5 178 179 for name in filenames: 180 base = os.path.basename(name) 181 f = open(name,'rb') 182 md5_data[base] = md5(f.read()).hexdigest() 183 f.close() 184 185 data = [" %r: %r,\n" % it for it in md5_data.items()] 186 data.sort() 187 repl = "".join(data) 188 189 import inspect 190 srcfile = inspect.getsourcefile(sys.modules[__name__]) 191 f = open(srcfile, 'rb'); src = f.read(); f.close() 192 193 match = re.search("\nmd5_data = {\n([^}]+)}", src) 194 if not match: 195 print >>sys.stderr, "Internal error!" 196 sys.exit(2) 197 198 src = src[:match.start(1)] + repl + src[match.end(1):] 199 f = open(srcfile,'w') 200 f.write(src) 201 f.close() 202 203 162 204 if __name__=='__main__': 163 main(sys.argv[1:]) 164 205 if len(sys.argv)>2 and sys.argv[1]=='--md5update': 206 update_md5(sys.argv[2:]) 207 else: 208 main(sys.argv[1:]) 209 210 211 212 213 django/branches/i18n/tests/othertests/templates.py
r869 r874 261 261 } 262 262 263 # This replaces the standard template loader.264 263 def test_template_loader(template_name, template_dirs=None): 264 "A custom template loader that loads the unit-test templates." 265 265 try: 266 266 return TEMPLATE_TESTS[template_name][0] … … 269 269 270 270 def run_tests(verbosity=0, standalone=False): 271 loader.load_template_source, old_template_loader = test_template_loader, loader.load_template_source 271 # Register our custom template loader. 272 old_template_loaders = loader.template_source_loaders 273 loader.template_source_loaders = [test_template_loader] 274 272 275 failed_tests = [] 273 276 tests = TEMPLATE_TESTS.items() … … 296 299 print "Template test: %s -- FAILED. Expected %r, got %r" % (name, vals[2], output) 297 300 failed_tests.append(name) 298 loader. load_template_source = old_template_loader301 loader.template_source_loaders = old_template_loaders 299 302 300 303 if failed_tests and not standalone:
