# -*- coding: utf-8 -*-
"""Setup the {{package}} application"""
import logging

from {{package}}.config.environment import load_environment

log = logging.getLogger(__name__)

from paste.deploy import loadapp
from pylons import config
from {{package}}.model import *
from {{package}}.model.rdfmodel import *
import elixir
from StringIO import StringIO


sample_nt = StringIO("""
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix ov:    <http://owl.openvest.org/2005/10/Portfolio#>.
@prefix vcard: <http://www.w3.org/2006/vcard/ns#>.
@prefix iso3166: <http://www.daml.org/2001/09/countries/iso-3166-ont#>.

ov:C_US_SUNW a ov:Company;
     ov:GICS_2 ov:GICS_2_45;
     ov:companyName "Sun Microsystems";
     ov:country iso3166:US;
     ov:nasdaqSymbol "SUNW";
     ov:secCik "0000709519";
     ov:symbol "JAVA". 
     
ov:C_US_IBM a ov:Company;
     ov:GICS_2 ov:GICS_2_45;
     ov:companyName "International Business Machines Corp.";
     ov:country iso3166:US;
     ov:nyseSymbol "IBM";
     ov:secCik "0000051143";
     ov:symbol "IBM";
     ov:yindustry "Diversified Computer Systems";
     ov:ysector "Technology";
     vcard:tel "914-499-1900";
     vcard:url "http://www.ibm.com"^^<http://www.w3.org/2001/XMLSchema#anyURI>; 
     ov:stockDescription "International Business Machines Corporation (IBM) operates as an information technology (IT) company worldwide. It has .....".
""")

def setup_app(command, conf, vars):
    """Place any commands to setup {{package}} here"""
    load_environment(conf.global_conf, conf.local_conf)
    from {{package}} import model
    model.metadata.create_all(checkfirst=True)
    model.Session.commit()
    if config['rdfalchemy.dburi'].lower().startswith('zodb://'):
        import transaction
    graph = config['rdfalchemy.ra_engine']
    graph.parse(sample_nt, format="n3")
    assert len(graph) == 19
    if config['rdfalchemy.dburi'].lower().startswith('zodb://'):
        transaction.commit()
    else:
        graph.commit()
    graph.close()
