def get_grids(self): response = {'status': 0, 'errors': [], 'grids': []} try: map_id = self.request.GET.get('map') if map_id == '': response['errors'].append('A map id is required.') except KeyError as e: response['errors'].append('A map id is required.') if len(response['errors']) == 0: map = Map.by_id(map_id) workspace = Workspace.by_id(map.workspace_id) if (workspace.name == self.request.userid): current_directory = os.path.dirname( os.path.dirname(os.path.abspath(__file__))) + '/' config_file = current_directory + 'mapcacheConfig.xml.default' codecs.open(config_file, encoding='utf8', mode='r') try: with codecs.open(config_file, encoding='utf8') as f: config = f.read() f.close() except IOError: response['errors'].append( "An error occured while opening '" + config_file + "' file.") if len(response['errors']) == 0: try: config_bs = BeautifulSoup(config) grids = config_bs.mapcache.findAll('grid') except: response['errors'].append( "An error occured while parsing '" + config_file + "' file.") if len(response['errors']) == 0: for grid in grids: for attr, val in grid.attrs: if attr == 'name': response['grids'].append(val) response['status'] = 1 else: response['errors'].append('Access denied.') return response
def get_grids(self): response = { 'status': 0, 'errors': [], 'grids': [] } try: map_id = self.request.GET.get('map') if map_id == '': response['errors'].append('A map id is required.') except KeyError as e: response['errors'].append('A map id is required.') if len(response['errors']) == 0: map = Map.by_id(map_id) workspace = Workspace.by_id(map.workspace_id) if(workspace.name == self.request.userid): current_directory = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/' config_file = current_directory + 'mapcacheConfig.xml.default' codecs.open(config_file, encoding='utf8', mode='r') try: with codecs.open(config_file, encoding='utf8') as f: config = f.read() f.close() except IOError: response['errors'].append("An error occured while opening '" + config_file + "' file.") if len(response['errors']) == 0: try: config_bs = BeautifulSoup(config) grids = config_bs.mapcache.findAll('grid') except: response['errors'].append("An error occured while parsing '" + config_file + "' file.") if len(response['errors']) == 0: for grid in grids: for attr, val in grid.attrs: if attr == 'name': response['grids'].append(val) response['status'] = 1 else: response['errors'].append('Access denied.') return response
def get_workspace_configs(self): response = { 'status': 0, 'errors': [], 'configs': [] } try: ws_name = self.request.GET.get('ws') except KeyError as e: response['errors'].append('A workspace name is required.') if len(response['errors']) == 0: workspace = Workspace.by_name(ws_name) if(workspace.name == self.request.userid): dbconfig_query = DBSession.query( DatabaseConfig.name, DatabaseConfig.type, DatabaseConfig.host, DatabaseConfig.port, DatabaseConfig.database_name, DatabaseConfig.user, DatabaseConfig.query ).filter( DatabaseConfig.workspace_id == workspace.id ) try: dbconfigs = dbconfig_query.all() except NoResultFound, e: response['errors'].append("The database configuration you requested was not found.") if len(response['errors']) == 0: for dbconfig in dbconfigs: response['configs'].append({ 'name': dbconfig[0], 'dbtype': dbconfig[1], 'dbhost': dbconfig[2], 'dbport': dbconfig[3], 'dbname': dbconfig[4], 'dbuser': dbconfig[5], 'dbquery': dbconfig[6] }) response['status'] = 1 else: response['errors'].append('Access denied.')
def delete_config(self): response = { 'status': 0, 'errors': [] } try: ws_name = self.request.POST.get('ws') if ws_name == '': response['errors'].append('A workspace name is required.') except KeyError as e: response['errors'].append('A workspace name is required.') try: name = self.request.POST.get('name') if name == '': response['errors'].append('Config name is required.') except KeyError as e: response['errors'].append('Config name is required.') if len(response['errors']) == 0: workspace = Workspace.by_name(ws_name) if(workspace.name == self.request.userid): dbconfig_query = DBSession.query( DatabaseConfig ).filter( DatabaseConfig.name == name, DatabaseConfig.workspace_id == workspace.id ) try: dbconfig = dbconfig_query.one() except NoResultFound, e: response['errors'].append("The database configuration you're trying to delete was not found.") if len(response['errors']) == 0: try: dbconfig_query.delete() except exc.SQLAlchemyError as e: response['errors'].append("An error occured while deleting database config.") if len(response['errors']) == 0: response['status'] = 1 else: response['errors'].append('Access denied.')
def getJobs(self): response = { 'status': 0, 'errors': [], 'jobs': [] } try: ws_name= self.request.GET.get('ws') if ws_name == '': response['errors'].append('A workspace name is required.') except KeyError as e: response['errors'].append('A workspace name is required.') if len(response['errors']) == 0: workspace = Workspace.by_name(ws_name) if(workspace.name == self.request.userid): query = DBSession.query( Job.id, Job.title, Job.status, Job.map_id, Map.workspace_id, Map.id, Map.name) \ .filter(Map.workspace_id == workspace.id, Job.map_id == Map.id) jobs = query.all() for job in jobs: response['jobs'].append({ 'id': job[0], 'title': job[1], 'status': job[2], 'map_name': job[6] }) response['status'] = 1 else: response['errors'].append('Access denied.') return response
def delete_config(self): response = {'status': 0, 'errors': []} try: ws_name = self.request.POST.get('ws') if ws_name == '': response['errors'].append('A workspace name is required.') except KeyError as e: response['errors'].append('A workspace name is required.') try: name = self.request.POST.get('name') if name == '': response['errors'].append('Config name is required.') except KeyError as e: response['errors'].append('Config name is required.') if len(response['errors']) == 0: workspace = Workspace.by_name(ws_name) if (workspace.name == self.request.userid): dbconfig_query = DBSession.query(DatabaseConfig).filter( DatabaseConfig.name == name, DatabaseConfig.workspace_id == workspace.id) try: dbconfig = dbconfig_query.one() except NoResultFound, e: response['errors'].append( "The database configuration you're trying to delete was not found." ) if len(response['errors']) == 0: try: dbconfig_query.delete() except exc.SQLAlchemyError as e: response['errors'].append( "An error occured while deleting database config.") if len(response['errors']) == 0: response['status'] = 1 else: response['errors'].append('Access denied.')
def get_workspace_configs(self): response = {'status': 0, 'errors': [], 'configs': []} try: ws_name = self.request.GET.get('ws') except KeyError as e: response['errors'].append('A workspace name is required.') if len(response['errors']) == 0: workspace = Workspace.by_name(ws_name) if (workspace.name == self.request.userid): dbconfig_query = DBSession.query( DatabaseConfig.name, DatabaseConfig.type, DatabaseConfig.host, DatabaseConfig.port, DatabaseConfig.database_name, DatabaseConfig.user, DatabaseConfig.query).filter( DatabaseConfig.workspace_id == workspace.id) try: dbconfigs = dbconfig_query.all() except NoResultFound, e: response['errors'].append( "The database configuration you requested was not found." ) if len(response['errors']) == 0: for dbconfig in dbconfigs: response['configs'].append({ 'name': dbconfig[0], 'dbtype': dbconfig[1], 'dbhost': dbconfig[2], 'dbport': dbconfig[3], 'dbname': dbconfig[4], 'dbuser': dbconfig[5], 'dbquery': dbconfig[6] }) response['status'] = 1 else: response['errors'].append('Access denied.')
def getJobs(self): response = {'status': 0, 'errors': [], 'jobs': []} try: ws_name = self.request.GET.get('ws') if ws_name == '': response['errors'].append('A workspace name is required.') except KeyError as e: response['errors'].append('A workspace name is required.') if len(response['errors']) == 0: workspace = Workspace.by_name(ws_name) if (workspace.name == self.request.userid): query = DBSession.query( Job.id, Job.title, Job.status, Job.map_id, Map.workspace_id, Map.id, Map.name) \ .filter(Map.workspace_id == workspace.id, Job.map_id == Map.id) jobs = query.all() for job in jobs: response['jobs'].append({ 'id': job[0], 'title': job[1], 'status': job[2], 'map_name': job[6] }) response['status'] = 1 else: response['errors'].append('Access denied.') return response
def startJob(self): response = { 'status': 0, 'errors': [], 'job': {} } extent = None dbconfig = None try: mapID = self.request.POST.get('map') if mapID == '': response['errors'].append('An map ID is required.') except KeyError as e: response['errors'].append('A map ID is required.') try: title = self.request.POST.get('title') if title == '': response['errors'].append('A job title is required.') except KeyError as e: response['errors'].append('A job title is required.') try: zoomLevels = self.request.POST.get('zoomlevels') if zoomLevels == '': response['errors'].append('Zoom levels are required.') except KeyError as e: response['errors'].append('Zoom levels are required.') try: metatile = self.request.POST.get('metatile') if metatile == '': response['errors'].append('Metatiles size is required.') except KeyError as e: response['errors'].append('Metatiles size is required.') try: grid = self.request.POST.get('grid') if grid == '': response['errors'].append('A grid is required.') except KeyError as e: response['errors'].append('A grid is required.') try: extent_type = self.request.POST.get('type') if extent_type == '': response['errors'].append('Extent type is required.') except KeyError as e: response['errors'].append('Extent type is required.') if len(response['errors']) == 0: if extent_type == 'string': try: extent = self.request.POST.get('extent') if extent == '': response['errors'].append('An extent is required.') except KeyError as e: response['errors'].append('An extent is required.') else: try: dbhost = self.request.POST.get('dbhost') if dbhost == '': response['errors'].append('Host name is required.') except KeyError as e: response['errors'].append('Host name is required.') try: dbport = self.request.POST.get('dbport') if dbport == '': response['errors'].append('Database port is required.') except KeyError as e: response['errors'].append('Database port is required.') try: dbname = self.request.POST.get('dbname') if dbname == '': response['errors'].append('Database name is required.') except KeyError as e: response['errors'].append('Database name is required.') try: dbuser = self.request.POST.get('dbuser') if dbuser == '': response['errors'].append('Database user is required.') except KeyError as e: response['errors'].append('Database user is required.') try: dbpassword = self.request.POST.get('dbpassword') if dbpassword == '': dbpassword = None except KeyError as e: dbpassword = None try: dbquery = self.request.POST.get('dbquery') if dbquery == '': response['errors'].append('A query is required.') except KeyError as e: response['errors'].append('A query is required.') if len(response['errors']) == 0: dbconfig = { 'type': extent_type, 'host': dbhost, 'port': dbport, 'name': dbname, 'user': dbuser, 'password': dbpassword, 'query': dbquery } if len(response['errors']) == 0: try: map = Map.by_id(mapID) except NoResultFound, e: response['errors'].append('This map is unavailable or does not exist.') if len(response['errors']) == 0: workspace = Workspace.by_id(map.workspace_id) if(workspace.name == self.request.userid): workspaces_directory = self.request.registry.settings.get('workspaces.directory', '') + '/' map_directory = workspaces_directory + self.request.userid + '/' + map.name + '/' mapfile = map_directory + 'map/' + map.name + '.map' kwargs = { 'title': title, 'status': 1, 'map_id': mapID } job = Job(**kwargs) try: DBSession.add(job) #used to get the job id DBSession.flush() except exc.SQLAlchemyError as e: response['errors'].append(e) if len(response['errors']) == 0: job_directory = self.request.registry.settings.get('mapcache.output.directory', '') mapserver_url = self.request.registry.settings.get('mapserver.url', '') + '?' pManager = processManager() pManager.addProcess(job, map_directory, mapfile, zoomLevels, metatile, grid, extent=extent, dbconfig=dbconfig, jobdir=job_directory, mapserver_url=mapserver_url) kwargs['id'] = job.id response['job'] = kwargs response['status'] = 1 else: response['errors'].append('Access denied.')
def save_config(self): response = { 'status': 0, 'errors': [] } try: ws_name = self.request.POST.get('ws') if ws_name == '': response['errors'].append('A workspace name is required.') except KeyError as e: response['errors'].append('A workspace name is required.') try: name = self.request.POST.get('name') if name == '': response['errors'].append('Config name is required.') except KeyError as e: response['errors'].append('Config name is required.') try: dbtype = self.request.POST.get('dbtype') if dbtype == '': response['errors'].append('Database type is required.') except KeyError as e: response['errors'].append('Database type is required.') try: dbhost = self.request.POST.get('dbhost') if dbhost == '': response['errors'].append('Host name is required.') except KeyError as e: response['errors'].append('Host name is required.') try: dbport = self.request.POST.get('dbport') if dbport == '': response['errors'].append('Database port is required.') except KeyError as e: response['errors'].append('Database port is required.') try: dbname = self.request.POST.get('dbname') if dbname == '': response['errors'].append('Database name is required.') except KeyError as e: response['errors'].append('Database name is required.') try: dbuser = self.request.POST.get('dbuser') if dbuser == '': response['errors'].append('Database user is required.') except KeyError as e: response['errors'].append('Database user is required.') try: dbquery = self.request.POST.get('dbquery') if dbquery == '': dbquery = None except KeyError as e: dbquery = None if len(response['errors']) == 0: workspace = Workspace.by_name(ws_name) if(workspace.name == self.request.userid): dbconfig_query = DBSession.query(DatabaseConfig).filter( DatabaseConfig.name == name, DatabaseConfig.workspace_id == workspace.id ) try: dbconfig = dbconfig_query.one() except NoResultFound, e: dbconfig = None kwargs = { 'name': name, 'type': dbtype, 'host': dbhost, 'port': dbport, 'database_name': dbname, 'user': dbuser, 'query': dbquery, 'workspace_id': workspace.id } if dbconfig: try: dbconfig_query.update(kwargs) except exc.SQLAlchemyError as e: response['errors'].append("An error occured while updating database config.") else: dbconfig = DatabaseConfig(**kwargs) try: DBSession.add(dbconfig) except exc.SQLAlchemyError as e: response['errors'].append(e) if len(response['errors']) == 0: response['status'] = 1 else: response['errors'].append('Access denied.')
class APIMapcacheViewer(object): def __init__(self, request): self.request = request self.matchdict = request.matchdict @view_config(route_name='mapcache.getLayers', permission='view', renderer='json', request_method='GET') def getLayers(self): response = {'status': 0, 'errors': [], 'layers': [], 'layernames': []} try: mapID = self.request.GET.get('map') except KeyError as e: response['errors'].append('A map ID is required.') return response if mapID is None: response['errors'].append('A map ID is required.') return response try: map = Map.by_id(mapID) except NoResultFound, e: response['errors'].append( 'This map is unavailable or does not exist.') return response workspace = Workspace.by_id(map.workspace_id) if (workspace.name == self.request.userid): ##First, let's browse the folders to find some finished jobs. ## TODO: Add optional additional paths. paths = [] ## Add the default folder workspacesDirectory = self.request.registry.settings.get( 'workspaces.directory', '') + '/' mapDirectory = workspacesDirectory + self.request.userid + '/' + map.name + '/' mapcacheDirectory = self.request.registry.settings.get( 'mapcache.output.directory', '') if not mapcacheDirectory or mapcacheDirectory == '': mapcacheDirectory = mapDirectory + 'mapcache/' else: mapcacheDirectory = mapcacheDirectory.rstrip('/') + '/' paths.append(mapcacheDirectory) level = 3 #maximum depth for recursive search for path in paths: some_dir = path.rstrip(os.path.sep) if os.path.isdir(some_dir): num_sep = some_dir.count(os.path.sep) for root, dirs, filenames in os.walk(some_dir): num_sep_this = root.count(os.path.sep) if num_sep + level <= num_sep_this: del dirs[:] for filename in fnmatch.filter(filenames, 'mapcacheConfig.xml'): response['layernames'].append( os.path.split(root)[1]) response['layers'].append( os.path.join(root, filename)) pprint.pprint(os.path.join(root, filename)) break else: response['errors'].append('Access denied.') return response
response['errors'].append('A job path is required.') if jobPath is None: response['errors'].append('A job path is required.') try: tilerequest = self.request.GET.get('request') except KeyError as e: response['errors'].append('A tile request is required.') if tilerequest is None: response['errors'].append('A tile request is required.') try: map = Map.by_id(mapID) except NoResultFound, e: response['errors'].append( 'This map is unavailable or does not exist.') return response workspace = Workspace.by_id(map.workspace_id) if len(response['errors']) is 0: if workspace.name == self.request.userid: if (os.path.isfile(jobPath)): # We now get the tileset and the grid from the config file # NOTE: We only support 1 tileset per config file for now, but several grids are supported. try: with codecs.open(jobPath, encoding='utf8') as f: content = f.read() f.close() except IOError: response['errors'].append( "An error occured while opening '" + config_file + "' file.") return response content = BeautifulStoneSoup(content)
def startJob(self): response = {'status': 0, 'errors': [], 'job': {}} extent = None dbconfig = None try: mapID = self.request.POST.get('map') if mapID == '': response['errors'].append('An map ID is required.') except KeyError as e: response['errors'].append('A map ID is required.') try: title = self.request.POST.get('title') if title == '': response['errors'].append('A job title is required.') except KeyError as e: response['errors'].append('A job title is required.') try: zoomLevels = self.request.POST.get('zoomlevels') if zoomLevels == '': response['errors'].append('Zoom levels are required.') except KeyError as e: response['errors'].append('Zoom levels are required.') try: metatile = self.request.POST.get('metatile') if metatile == '': response['errors'].append('Metatiles size is required.') except KeyError as e: response['errors'].append('Metatiles size is required.') try: grid = self.request.POST.get('grid') if grid == '': response['errors'].append('A grid is required.') except KeyError as e: response['errors'].append('A grid is required.') try: extent_type = self.request.POST.get('type') if extent_type == '': response['errors'].append('Extent type is required.') except KeyError as e: response['errors'].append('Extent type is required.') if len(response['errors']) == 0: if extent_type == 'string': try: extent = self.request.POST.get('extent') if extent == '': response['errors'].append('An extent is required.') except KeyError as e: response['errors'].append('An extent is required.') else: try: dbhost = self.request.POST.get('dbhost') if dbhost == '': response['errors'].append('Host name is required.') except KeyError as e: response['errors'].append('Host name is required.') try: dbport = self.request.POST.get('dbport') if dbport == '': response['errors'].append('Database port is required.') except KeyError as e: response['errors'].append('Database port is required.') try: dbname = self.request.POST.get('dbname') if dbname == '': response['errors'].append('Database name is required.') except KeyError as e: response['errors'].append('Database name is required.') try: dbuser = self.request.POST.get('dbuser') if dbuser == '': response['errors'].append('Database user is required.') except KeyError as e: response['errors'].append('Database user is required.') try: dbpassword = self.request.POST.get('dbpassword') if dbpassword == '': dbpassword = None except KeyError as e: dbpassword = None try: dbquery = self.request.POST.get('dbquery') if dbquery == '': response['errors'].append('A query is required.') except KeyError as e: response['errors'].append('A query is required.') if len(response['errors']) == 0: dbconfig = { 'type': extent_type, 'host': dbhost, 'port': dbport, 'name': dbname, 'user': dbuser, 'password': dbpassword, 'query': dbquery } if len(response['errors']) == 0: try: map = Map.by_id(mapID) except NoResultFound, e: response['errors'].append( 'This map is unavailable or does not exist.') if len(response['errors']) == 0: workspace = Workspace.by_id(map.workspace_id) if (workspace.name == self.request.userid): workspaces_directory = self.request.registry.settings.get( 'workspaces.directory', '') + '/' map_directory = workspaces_directory + self.request.userid + '/' + map.name + '/' mapfile = map_directory + 'map/' + map.name + '.map' kwargs = {'title': title, 'status': 1, 'map_id': mapID} job = Job(**kwargs) try: DBSession.add(job) #used to get the job id DBSession.flush() except exc.SQLAlchemyError as e: response['errors'].append(e) if len(response['errors']) == 0: job_directory = self.request.registry.settings.get( 'mapcache.output.directory', '') mapserver_url = self.request.registry.settings.get( 'mapserver.url', '') + '?' pManager = processManager() pManager.addProcess(job, map_directory, mapfile, zoomLevels, metatile, grid, extent=extent, dbconfig=dbconfig, jobdir=job_directory, mapserver_url=mapserver_url) kwargs['id'] = job.id response['job'] = kwargs response['status'] = 1 else: response['errors'].append('Access denied.')
def save_config(self): response = {'status': 0, 'errors': []} try: ws_name = self.request.POST.get('ws') if ws_name == '': response['errors'].append('A workspace name is required.') except KeyError as e: response['errors'].append('A workspace name is required.') try: name = self.request.POST.get('name') if name == '': response['errors'].append('Config name is required.') except KeyError as e: response['errors'].append('Config name is required.') try: dbtype = self.request.POST.get('dbtype') if dbtype == '': response['errors'].append('Database type is required.') except KeyError as e: response['errors'].append('Database type is required.') try: dbhost = self.request.POST.get('dbhost') if dbhost == '': response['errors'].append('Host name is required.') except KeyError as e: response['errors'].append('Host name is required.') try: dbport = self.request.POST.get('dbport') if dbport == '': response['errors'].append('Database port is required.') except KeyError as e: response['errors'].append('Database port is required.') try: dbname = self.request.POST.get('dbname') if dbname == '': response['errors'].append('Database name is required.') except KeyError as e: response['errors'].append('Database name is required.') try: dbuser = self.request.POST.get('dbuser') if dbuser == '': response['errors'].append('Database user is required.') except KeyError as e: response['errors'].append('Database user is required.') try: dbquery = self.request.POST.get('dbquery') if dbquery == '': dbquery = None except KeyError as e: dbquery = None if len(response['errors']) == 0: workspace = Workspace.by_name(ws_name) if (workspace.name == self.request.userid): dbconfig_query = DBSession.query(DatabaseConfig).filter( DatabaseConfig.name == name, DatabaseConfig.workspace_id == workspace.id) try: dbconfig = dbconfig_query.one() except NoResultFound, e: dbconfig = None kwargs = { 'name': name, 'type': dbtype, 'host': dbhost, 'port': dbport, 'database_name': dbname, 'user': dbuser, 'query': dbquery, 'workspace_id': workspace.id } if dbconfig: try: dbconfig_query.update(kwargs) except exc.SQLAlchemyError as e: response['errors'].append( "An error occured while updating database config.") else: dbconfig = DatabaseConfig(**kwargs) try: DBSession.add(dbconfig) except exc.SQLAlchemyError as e: response['errors'].append(e) if len(response['errors']) == 0: response['status'] = 1 else: response['errors'].append('Access denied.')
except KeyError as e: response['errors'].append('A job path is required.') if jobPath is None: response['errors'].append('A job path is required.') try: tilerequest = self.request.GET.get('request') except KeyError as e: response['errors'].append('A tile request is required.') if tilerequest is None: response['errors'].append('A tile request is required.') try: map = Map.by_id(mapID) except NoResultFound, e: response['errors'].append('This map is unavailable or does not exist.') return response workspace = Workspace.by_id(map.workspace_id) if len(response['errors']) is 0: if workspace.name == self.request.userid: if(os.path.isfile(jobPath)): # We now get the tileset and the grid from the config file # NOTE: We only support 1 tileset per config file for now, but several grids are supported. try: with codecs.open(jobPath, encoding='utf8') as f: content = f.read() f.close() except IOError: response['errors'].append("An error occured while opening '" + config_file + "' file.") return response content = BeautifulStoneSoup(content) tileset = content.mapcache.find('tileset') #Finding out the name of the tileset