basic stuff for loading map data

This commit is contained in:
Laura Klünder 2016-08-19 12:41:02 +02:00
parent 2d639d595a
commit c4287e110c
4 changed files with 66 additions and 9 deletions

View file

@ -1,5 +1,5 @@
from .classes import MapDataManager
from .classes import MapManager
default_app_config = 'c3nav.mapdata.apps.MapdataConfig'
objects = MapDataManager()
mapmanager = MapManager()

View file

@ -1,4 +1,19 @@
from django.apps import AppConfig
from django.conf import settings
from django.core.checks import Warning, register
from . import mapmanager
@register()
def has_map_data_check(app_configs, **kwargs):
if not settings.MAP_DIRS:
return [Warning(
'There are no map data directories configured.',
hint='Add mapdirs=/path/to/directory to your c3nav.cfg.',
id='mapdata.W001',
)]
return []
class MapdataConfig(AppConfig):
@ -6,4 +21,5 @@ class MapdataConfig(AppConfig):
verbose_name = 'map data manager'
def ready(self):
pass
for map_dir in settings.MAP_DIRS:
mapmanager.add_map_dir(map_dir)

View file

@ -1,8 +1,49 @@
class MapDataManager:
def __init__(self):
pass
import json
import os
from collections import OrderedDict
class MapDataSource:
class MapInitError(Exception):
pass
class MapManager:
def __init__(self):
pass
self.main_pkg = None
self.pkgs = OrderedDict()
def add_map_dir(self, path):
pkg = MapDataPackage(path)
if pkg.name in self.pkgs:
raise MapInitError('Duplicate map package: '+pkg.name)
if pkg.extends is None:
if self.main_pkg is not None:
raise MapInitError('There can not be more than one root map package: tried to add '+pkg.name+', '
'but '+self.main_pkg.name+' was there first.')
self.main_pkg = pkg
else:
if pkg.extends not in self.pkgs:
raise MapInitError('map package'+pkg.name+' extends '+pkg.exends+', which was not imported '
'beforehand.')
self.pkgs[pkg.name] = pkg
class MapDataPackage:
def __init__(self, path):
self.path = path
main_file = os.path.join(path, 'map.json')
try:
data = json.load(open(main_file))
except FileNotFoundError:
raise MapInitError(main_file+' not found')
except json.decoder.JSONDecodeError as e:
raise MapInitError('Could not decode '+main_file+': '+str(e))
self.name = data.get('name')
if self.name is None:
raise MapInitError('Map package '+path+' has no name in map.json.')
self.extends = data.get('extends')

View file

@ -17,7 +17,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = config.get('c3nav', 'datadir', fallback=os.environ.get('DATA_DIR', 'data'))
LOG_DIR = os.path.join(DATA_DIR, 'logs')
MAP_DIRS = config.get('c3nav', 'mapdir', fallback=None)
MAP_DIRS = tuple(n for n in config.get('c3nav', 'mapdirs', fallback='').split(',') if n)
if not os.path.exists(DATA_DIR):
os.mkdir(DATA_DIR)