[9006] | 1 | import logging
|
---|
| 2 | import os
|
---|
| 3 |
|
---|
| 4 | from gheat import gheatsettings as settings
|
---|
| 5 |
|
---|
| 6 | from django.core.exceptions import ImproperlyConfigured
|
---|
| 7 | from django.db import connection
|
---|
| 8 |
|
---|
| 9 | # Logging config
|
---|
| 10 | # ==============
|
---|
| 11 |
|
---|
| 12 | if settings.DEBUG:
|
---|
| 13 | level = logging.INFO
|
---|
| 14 | else:
|
---|
| 15 | level = logging.WARNING
|
---|
| 16 | logging.basicConfig(level=level) # Ack! This should be in Aspen. :^(
|
---|
| 17 | log = logging.getLogger('gheat')
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | # Configuration
|
---|
| 21 | # =============
|
---|
| 22 | # Set some things that backends will need.
|
---|
| 23 | ALWAYS_BUILD = settings.GHEAT_ALWAYS_BUILD
|
---|
| 24 | BUILD_EMPTIES = settings.GHEAT_BUILD_EMPTIES
|
---|
| 25 |
|
---|
| 26 | DIRMODE = settings.GHEAT_DIRMODE
|
---|
| 27 | try:
|
---|
| 28 | DIRMODE = int(eval(DIRMODE))
|
---|
| 29 | except (NameError, SyntaxError, ValueError):
|
---|
| 30 | raise ImproperlyConfigured("dirmode (%s) must be an integer." % dirmode)
|
---|
| 31 |
|
---|
| 32 | SIZE = 256 # size of (square) tile; NB: changing this will break gmerc calls!
|
---|
| 33 | MAX_ZOOM = 31 # this depends on Google API; 0 is furthest out as of recent ver.
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | # Try to find an image library.
|
---|
| 37 | # =============================
|
---|
| 38 |
|
---|
| 39 | BACKEND = None
|
---|
| 40 | BACKEND_PIL = False
|
---|
| 41 | BACKEND_PYGAME = False
|
---|
| 42 |
|
---|
| 43 | _want = settings.GHEAT_BACKEND.lower()
|
---|
| 44 | if _want not in ('pil', 'pygame', ''):
|
---|
| 45 | raise ImproperlyConfigured( "The %s backend is not supported, only PIL and "
|
---|
| 46 | + "Pygame (assuming those libraries are installed)."
|
---|
| 47 | )
|
---|
| 48 |
|
---|
| 49 | if _want:
|
---|
| 50 | if _want == 'pygame':
|
---|
| 51 | from gheat import pygame_ as backend
|
---|
| 52 | elif _want == 'pil':
|
---|
| 53 | from gheat import pil_ as backend
|
---|
| 54 | BACKEND = _want
|
---|
| 55 | else:
|
---|
| 56 | try:
|
---|
| 57 | from gheat import pygame_ as backend
|
---|
| 58 | BACKEND = 'pygame'
|
---|
| 59 | except ImportError:
|
---|
| 60 | try:
|
---|
| 61 | from gheat import pil_ as backend
|
---|
| 62 | BACKEND = 'pil'
|
---|
| 63 | except ImportError:
|
---|
| 64 | raise
|
---|
| 65 | pass
|
---|
| 66 |
|
---|
| 67 | if BACKEND is None:
|
---|
| 68 | raise ImportError("Neither Pygame nor PIL could be imported.")
|
---|
| 69 |
|
---|
| 70 | BACKEND_PYGAME = BACKEND == 'pygame'
|
---|
| 71 | BACKEND_PIL = BACKEND == 'pil'
|
---|
| 72 |
|
---|
| 73 | log.info("Using the %s library" % BACKEND)
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | # Set up color schemes and dots.
|
---|
| 77 | # ==============================
|
---|
| 78 |
|
---|
| 79 | color_schemes = dict() # this is used below
|
---|
| 80 |
|
---|
| 81 | _color_schemes_dir = os.path.join(settings.GHEAT_CONF_DIR, 'color-schemes')
|
---|
| 82 | for fname in os.listdir(_color_schemes_dir):
|
---|
| 83 | if not fname.endswith('.png'):
|
---|
| 84 | continue
|
---|
| 85 | name = os.path.splitext(fname)[0]
|
---|
| 86 | fspath = os.path.join(_color_schemes_dir, fname)
|
---|
| 87 | color_schemes[name] = backend.ColorScheme(name, fspath)
|
---|
| 88 |
|
---|
| 89 | def load_dots(backend):
|
---|
| 90 | """Given a backend module, return a mapping of zoom level to Dot object.
|
---|
| 91 | """
|
---|
| 92 | return dict([(zoom, backend.Dot(zoom)) for zoom in range(MAX_ZOOM)])
|
---|
| 93 | dots = load_dots(backend) # factored for easier use from scripts
|
---|
| 94 |
|
---|
| 95 | # Some util methods
|
---|
| 96 | # =================
|
---|
| 97 | def translate(root, url):
|
---|
| 98 | """Translate a URL to the filesystem.
|
---|
| 99 |
|
---|
| 100 | We specifically avoid removing symlinks in the path so that the filepath
|
---|
| 101 | remains under the website root. Also, we don't want trailing slashes for
|
---|
| 102 | directories.
|
---|
| 103 |
|
---|
| 104 | """
|
---|
| 105 | parts = [root] + url.lstrip('/').split('/')
|
---|
| 106 | return os.sep.join(parts).rstrip(os.sep)
|
---|
| 107 |
|
---|
| 108 | ROOT = settings.GHEAT_MEDIA_ROOT
|
---|