# Path: studio/lib/base.py
# class BaseController(WSGIController):
# 
#     def __call__(self, environ, start_response):
#         """Invoke the Controller"""
#         # WSGIController.__call__ dispatches to the Controller method
#         # the request is routed to. This routing information is
#         # available in environ['pylons.routes_dict']
#         try:
#             if 'lang' in session:
#                 try:
#                     set_lang(session['lang'])
#                 except LanguageError:
#                     # remove lang from session if an error occured
#                     del session['lang']
#                     session.save()
# 
#             return WSGIController.__call__(self, environ, start_response)
#         finally:
#             meta.Session.remove()
# 
# Path: studio/lib/helpers.py
# def gen_mapname():
# def _gen_string(length=8, chars=string.digits + string.letters):
# 
# Path: studio/lib/datasource_discovery.py
# def discover_datasources(datastore_str):
#     """ Discover the datasources contained in the datastore
#     corresponding to the OGR string ogr_str, return an
#     array of {name,type, hash}"""
#     
#     datastore = DataStore(datastore_str)
#     datasources=[]
#     for ds in datastore.get_datasources():
#         datasources.append({'text':ds.name, 'type':ds.type, 'id':ds.getHash(), 'leaf': True})
#     return datasources


import logging
import simplejson
from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from pylons.decorators import jsonify
from repoze.what.predicates import has_permission
from repoze.what.plugins.pylonshq import ActionProtector
from studio.lib.base import BaseController
from studio.lib import helpers as h
from studio.model import *
from studio.lib.datasource_discovery import discover_datasources
        result = meta.Session.query(DataStore).get(id)
        result.name = content['name']
        result.type = content['type']
        result.ogrstring = content['ogrstring']
        meta.Session.commit()
        response.status = 201

    @ActionProtector(has_permission('create_update_datastores'))
    def delete(self, id):
        """DELETE /datastores/id: Delete an existing item."""
        # url('DataStores', id=ID)
        result = meta.Session.query(DataStore).get(id)
        meta.Session.delete(result)
        meta.Session.commit()

        response.status = 204
        # remove content-type from response headers so that webtest won't get confused
        # http://groups.google.com/group/pylons-discuss/browse_thread/thread/1267650386ae521b
        del response.headers['content-type']
        
    @ActionProtector(has_permission('view_datastores'))
    @jsonify
    def show(self, id):
        """GET /datastores/id: Show a specific item."""
        # url('DataStores', id=ID)
        datastore = meta.Session.query(DataStore).get(id)

        # do not raise RuntimeError from discover_datasources
        # if in "test" mode
        try:

# Path: studio/lib/base.py
# class BaseController(WSGIController):
# 
#     def __call__(self, environ, start_response):
#         """Invoke the Controller"""
#         # WSGIController.__call__ dispatches to the Controller method
#         # the request is routed to. This routing information is
#         # available in environ['pylons.routes_dict']
#         try:
#             if 'lang' in session:
#                 try:
#                     set_lang(session['lang'])
#                 except LanguageError:
#                     # remove lang from session if an error occured
#                     del session['lang']
#                     session.save()
# 
#             return WSGIController.__call__(self, environ, start_response)
#         finally:
#             meta.Session.remove()
# 
# Path: studio/lib/helpers.py
# def gen_mapname():
# def _gen_string(length=8, chars=string.digits + string.letters):
# 
# Path: studio/lib/datasource_discovery.py
# def discover_datasources(datastore_str):
#     """ Discover the datasources contained in the datastore
#     corresponding to the OGR string ogr_str, return an
#     array of {name,type, hash}"""
#     
#     datastore = DataStore(datastore_str)
#     datasources=[]
#     for ds in datastore.get_datasources():
#         datasources.append({'text':ds.name, 'type':ds.type, 'id':ds.getHash(), 'leaf': True})
#     return datasources


# Path: studio/controllers/datastores.py
import logging
import simplejson
from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from pylons.decorators import jsonify
from repoze.what.predicates import has_permission
from repoze.what.plugins.pylonshq import ActionProtector
from studio.lib.base import BaseController
from studio.lib import helpers as h
from studio.model import *
from studio.lib.datasource_discovery import discover_datasources

        result = meta.Session.query(DataStore).get(id)
        result.name = content['name']
        result.type = content['type']
        result.ogrstring = content['ogrstring']
        meta.Session.commit()
        response.status = 201

    @ActionProtector(has_permission('create_update_datastores'))
    def delete(self, id):
        """DELETE /datastores/id: Delete an existing item."""
        # url('DataStores', id=ID)
        result = meta.Session.query(DataStore).get(id)
        meta.Session.delete(result)
        meta.Session.commit()

        response.status = 204
        # remove content-type from response headers so that webtest won't get confused
        # http://groups.google.com/group/pylons-discuss/browse_thread/thread/1267650386ae521b
        del response.headers['content-type']
        
    @ActionProtector(has_permission('view_datastores'))
    @jsonify
    def show(self, id):
        """GET /datastores/id: Show a specific item."""
        # url('DataStores', id=ID)
        datastore = meta.Session.query(DataStore).get(id)

        # do not raise RuntimeError from discover_datasources
        # if in "test" mode
        try:

            datasources = discover_datasources(datastore.ogrstring)