Source

jp-places / src / place_database.py

Full commit
# -*- coding: utf-8 -*-
# 
#  place_database.py
#  foks
#  
#  Created by Lars Yencken on 2007-07-11.
#  Copyright 2007-2008 Lars Yencken. All rights reserved.
# 

"A script to build place-name database tables for FOKS."

#----------------------------------------------------------------------------#

from os.path import join, exists

from django.conf import settings
from cjktools.common import sopen
import consoleLog

from foks.lexicon import models, unified_places

#----------------------------------------------------------------------------#

_place_gazetteer = join(settings.DATA_DIR, u'place_gazetteer.gz')

dependencies = [_place_gazetteer, unified_places.__file__, models.__file__]

log = consoleLog.default

#----------------------------------------------------------------------------#

class PlaceDatabase(object):
    """
    This class generates and stores the dictionary and hierarchy of place
    to the database tables lexicon_placename and lexicon_placenamealias. All this is
    done via its build() method.
    """


    def __init__(self):
        self.built = False
        self.root_node = None
        return

    def build(self):
        """
        Builds the lexicon_placename and lexicon_placenamealias database tables.
        """
        if self.built:
            return

        log.start('Building place tables', nSteps=2)
        log.log('Constructing place hierarchy')
        if exists(_place_gazetteer):
            self.root_node = unified_places.UnifiedHierarchy.from_file(
                    _place_gazetteer)
        else:
            self.root_node = unified_places.UnifiedHierarchy.get_cached()

        log.log('Storing places to the database')
        models.PlaceName.from_gazetteer(self.root_node)
        log.finish()

        self.built = True
        return

#----------------------------------------------------------------------------#

def build():
    obj = PlaceDatabase()
    obj.build()

#----------------------------------------------------------------------------#

if __name__ == u'__main__':
    build()