# 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
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Studio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Studio.  If not, see <http://www.gnu.org/licenses/>.
#




log = logging.getLogger(__name__)

class DatastoresController(BaseController):
    """REST Controller styled on the Atom Publishing Protocol"""
    # To properly map this controller, ensure your config/routing.py file has
    # a resource setup:
    #     map.resource('DataStores', 'datastores')

    @ActionProtector(has_permission('view_datastores'))
    @jsonify
    def index(self, format='html'):
        """GET /datastores: All items in the collection."""
        # url('datastores')
        results = meta.Session.query(DataStore)

# 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

# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Studio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Studio.  If not, see <http://www.gnu.org/licenses/>.
#




log = logging.getLogger(__name__)

class DatastoresController(BaseController):
    """REST Controller styled on the Atom Publishing Protocol"""
    # To properly map this controller, ensure your config/routing.py file has
    # a resource setup:
    #     map.resource('DataStores', 'datastores')

    @ActionProtector(has_permission('view_datastores'))
    @jsonify
    def index(self, format='html'):
        """GET /datastores: All items in the collection."""
        # url('datastores')
        results = meta.Session.query(DataStore)

        serialized_datastores = [{'id': ds.id, 'text': ds.name, 'href': h.url_for(controller="datastores", action="show", id=ds.id), 'type': ds.type} for ds in results]