source: src/django_gheat/gheat/__init__.py@ 9563

Last change on this file since 9563 was 9145, checked in by rick, 14 years ago

Always building is slow and make development a pain, when building scripts (due
to the init of all kind of empty files).

File size: 3.2 KB
Line 
1import logging
2import os
3
4from gheat import gheatsettings as settings
5
6from django.core.exceptions import ImproperlyConfigured
7from django.db import connection
8
9# Logging config
10# ==============
11
12if settings.DEBUG:
13 level = logging.WARNING
14else:
15 level = logging.WARNING
16if os.getenv('DEBUG', None):
17 level = logging.DEBUG
18
19logging.basicConfig(level=level) # Ack! This should be in Aspen. :^(
20log = logging.getLogger('gheat')
21
22
23# Configuration
24# =============
25# Set some things that backends will need.
26ALWAYS_BUILD = settings.GHEAT_ALWAYS_BUILD = False
27BUILD_EMPTIES = settings.GHEAT_BUILD_EMPTIES = False
28
29DIRMODE = settings.GHEAT_DIRMODE
30try:
31 DIRMODE = int(eval(DIRMODE))
32except (NameError, SyntaxError, ValueError):
33 raise ImproperlyConfigured("dirmode (%s) must be an integer." % dirmode)
34
35SIZE = 256 # size of (square) tile; NB: changing this will break gmerc calls!
36MAX_ZOOM = 22 # this depends on Google API; 0 is furthest out as of recent ver.
37
38
39# Try to find an image library.
40# =============================
41
42BACKEND = None
43BACKEND_PIL = False
44BACKEND_PYGAME = False
45
46_want = settings.GHEAT_BACKEND.lower()
47if _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
52if _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
58else:
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'
66 except ImportError:
67 raise
68 pass
69
70 if BACKEND is None:
71 raise ImportError("Neither Pygame nor PIL could be imported.")
72
73BACKEND_PYGAME = BACKEND == 'pygame'
74BACKEND_PIL = BACKEND == 'pil'
75
76log.info("Using the %s library" % BACKEND)
77
78
79# Set up color schemes and dots.
80# ==============================
81
82color_schemes = dict() # this is used below
83
84_color_schemes_dir = os.path.join(settings.GHEAT_CONF_DIR, 'color-schemes')
85for 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
92def 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)])
96dots = load_dots(backend) # factored for easier use from scripts
97
98# Some util methods
99# =================
100def 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
111ROOT = settings.GHEAT_MEDIA_ROOT
Note: See TracBrowser for help on using the repository browser.