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