<s> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> import sys <EOL> import time <EOL> import os <EOL> import traceback <EOL> import ConfigParser <EOL> from web_request . handlers import wsgi , mod_python , cgi <EOL> from lxml import etree <EOL> import cgi as cgimod <EOL> from FeatureServer . WebFeatureService . Response . TransactionResponse import TransactionResponse <EOL> from FeatureServer . WebFeatureService . Response . TransactionSummary import TransactionSummary <EOL> from FeatureServer . WebFeatureService . Response . ActionResult import ActionResult <EOL> from FeatureServer . Workspace . FileHandler import FileHandler <EOL> from FeatureServer . Exceptions . ExceptionReport import ExceptionReport <EOL> from FeatureServer . Exceptions . WebFeatureService . InvalidValueException import InvalidValueException <EOL> from FeatureServer . Exceptions . ConnectionException import ConnectionException <EOL> from FeatureServer . Exceptions . LayerNotFoundException import LayerNotFoundException <EOL> import FeatureServer . Processing <EOL> from web_request . response import Response <EOL> if '<STR_LIT>' in os . environ : <EOL> cfgfiles = os . environ [ '<STR_LIT>' ] . split ( "<STR_LIT:U+002C>" ) <EOL> else : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> workingdir = os . path . abspath ( os . path . join ( os . getcwd ( ) , os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) ) ) <EOL> cfgfiles = ( os . path . join ( workingdir , "<STR_LIT>" ) , os . path . join ( workingdir , "<STR_LIT:..>" , "<STR_LIT>" ) ) <EOL> else : <EOL> cfgfiles = ( "<STR_LIT>" , os . path . join ( "<STR_LIT:..>" , "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> class Server ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , datasources , metadata = { } , processes = { } ) : <EOL> self . datasources = datasources <EOL> self . metadata = metadata <EOL> self . processes = processes <EOL> def _loadFromSection ( cls , config , section , module_type , ** objargs ) : <EOL> type = config . get ( section , "<STR_LIT:type>" ) <EOL> module = __import__ ( "<STR_LIT>" % ( module_type , type ) , globals ( ) , locals ( ) , type ) <EOL> objclass = getattr ( module , type ) <EOL> for opt in config . options ( section ) : <EOL> objargs [ opt ] = config . get ( section , opt ) <EOL> if module_type is '<STR_LIT>' : <EOL> return objclass ( section , ** objargs ) <EOL> else : <EOL> return objclass ( ** objargs ) <EOL> loadFromSection = classmethod ( _loadFromSection ) <EOL> def _load ( cls , * files ) : <EOL> """<STR_LIT>""" <EOL> config = ConfigParser . ConfigParser ( ) <EOL> config . read ( files ) <EOL> metadata = { } <EOL> if config . has_section ( "<STR_LIT>" ) : <EOL> for key in config . options ( "<STR_LIT>" ) : <EOL> metadata [ key ] = config . get ( "<STR_LIT>" , key ) <EOL> processes = { } <EOL> datasources = { } <EOL> for section in config . sections ( ) : <EOL> if section == "<STR_LIT>" : continue <EOL> if section . startswith ( "<STR_LIT>" ) : <EOL> try : <EOL> processes [ section [ <NUM_LIT:8> : ] ] = FeatureServer . Processing . loadFromSection ( config , section ) <EOL> except Exception , E : <EOL> pass <EOL> else : <EOL> datasources [ section ] = cls . loadFromSection ( config , section , '<STR_LIT>' ) <EOL> return cls ( datasources , metadata , processes ) <EOL> load = classmethod ( _load ) <EOL> def dispatchRequest ( self , base_path = "<STR_LIT>" , path_info = "<STR_LIT:/>" , params = { } , request_method = "<STR_LIT:GET>" , post_data = None , accepts = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> response_code = "<STR_LIT>" <EOL> host = base_path <EOL> request = None <EOL> content_types = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:application/json>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:html>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> exceptionReport = ExceptionReport ( ) <EOL> path = path_info . split ( "<STR_LIT:/>" ) <EOL> found = False <EOL> format = "<STR_LIT>" <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> format = params [ '<STR_LIT>' ] <EOL> if format . lower ( ) in content_types : <EOL> format = content_types [ format . lower ( ) ] <EOL> found = True <EOL> if not found and len ( path ) > <NUM_LIT:1> : <EOL> path_pieces = path [ - <NUM_LIT:1> ] . split ( "<STR_LIT:.>" ) <EOL> if len ( path_pieces ) > <NUM_LIT:1> : <EOL> format = path_pieces [ - <NUM_LIT:1> ] <EOL> if format . lower ( ) in content_types : <EOL> format = content_types [ format . lower ( ) ] <EOL> found = True <EOL> if not found and not params . has_key ( "<STR_LIT>" ) and post_data : <EOL> try : <EOL> dom = etree . XML ( post_data ) <EOL> params [ '<STR_LIT>' ] = dom . get ( '<STR_LIT>' ) <EOL> except etree . ParseError : pass <EOL> if not found and not params . has_key ( "<STR_LIT:version>" ) and post_data : <EOL> try : <EOL> dom = etree . XML ( post_data ) <EOL> params [ '<STR_LIT:version>' ] = dom . get ( '<STR_LIT:version>' ) <EOL> except etree . ParseError : pass <EOL> if not found and not params . has_key ( "<STR_LIT>" ) and post_data : <EOL> try : <EOL> dom = etree . XML ( post_data ) <EOL> for key , value in cgimod . parse_qsl ( post_data , keep_blank_values = True ) : <EOL> if key . lower ( ) == '<STR_LIT>' : <EOL> params [ '<STR_LIT>' ] = value <EOL> except etree . ParseError : pass <EOL> if not found and params . has_key ( "<STR_LIT>" ) : <EOL> format = params [ '<STR_LIT>' ] <EOL> if format . lower ( ) in content_types : <EOL> format = content_types [ format . lower ( ) ] <EOL> found = True <EOL> if not found and accepts : <EOL> if accepts . lower ( ) in content_types : <EOL> format = content_types [ accepts . lower ( ) ] <EOL> found = True <EOL> if not found and not format : <EOL> if self . metadata . has_key ( "<STR_LIT>" ) : <EOL> format = self . metadata [ '<STR_LIT>' ] <EOL> else : <EOL> format = "<STR_LIT>" <EOL> service_module = __import__ ( "<STR_LIT>" % format , globals ( ) , locals ( ) , format ) <EOL> service = getattr ( service_module , format ) <EOL> request = service ( self ) <EOL> response = [ ] <EOL> try : <EOL> request . parse ( params , path_info , host , post_data , request_method ) <EOL> if len ( request . actions ) and request . actions [ <NUM_LIT:0> ] . method == "<STR_LIT>" : <EOL> return request . encode_metadata ( request . actions [ <NUM_LIT:0> ] ) <EOL> if len ( request . actions ) > <NUM_LIT:0> and hasattr ( request . actions [ <NUM_LIT:0> ] , '<STR_LIT>' ) and request . actions [ <NUM_LIT:0> ] . request is not None : <EOL> version = '<STR_LIT>' <EOL> if hasattr ( request . actions [ <NUM_LIT:0> ] , '<STR_LIT:version>' ) and len ( request . actions [ <NUM_LIT:0> ] . version ) > <NUM_LIT:0> : <EOL> version = request . actions [ <NUM_LIT:0> ] . version <EOL> if request . actions [ <NUM_LIT:0> ] . request . lower ( ) == "<STR_LIT>" : <EOL> return getattr ( request , request . actions [ <NUM_LIT:0> ] . request . lower ( ) ) ( version ) <EOL> elif request . actions [ <NUM_LIT:0> ] . request . lower ( ) == "<STR_LIT>" : <EOL> return getattr ( request , request . actions [ <NUM_LIT:0> ] . request . lower ( ) ) ( version ) <EOL> datasource = self . datasources [ request . datasources [ <NUM_LIT:0> ] ] <EOL> if request_method != "<STR_LIT:GET>" and hasattr ( datasource , '<STR_LIT>' ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> try : <EOL> datasource . begin ( ) <EOL> if len ( request . actions ) > <NUM_LIT:0> and hasattr ( request . actions [ <NUM_LIT:0> ] , '<STR_LIT>' ) and request . actions [ <NUM_LIT:0> ] . request is not None : <EOL> if request . actions [ <NUM_LIT:0> ] . request . lower ( ) == "<STR_LIT>" : <EOL> '''<STR_LIT:U+0020>''' <EOL> try : <EOL> transactionResponse = TransactionResponse ( ) <EOL> transactionResponse . setSummary ( TransactionSummary ( ) ) <EOL> for action in request . actions : <EOL> method = getattr ( datasource , action . method ) <EOL> try : <EOL> result = method ( action ) <EOL> if isinstance ( result , ActionResult ) : <EOL> transactionResponse . addResult ( result ) <EOL> elif result is not None : <EOL> response += result <EOL> except InvalidValueException as e : <EOL> exceptionReport . add ( e ) <EOL> datasource . commit ( ) <EOL> except : <EOL> datasource . rollback ( ) <EOL> raise <EOL> if hasattr ( datasource , '<STR_LIT>' ) : <EOL> for process in datasource . processes . split ( "<STR_LIT:U+002C>" ) : <EOL> if not self . processes . has_key ( process ) : <EOL> raise Exception ( "<STR_LIT>" % ( process , "<STR_LIT:U+002C>" . join ( self . processes . keys ( ) ) ) ) <EOL> response = self . processes [ process ] . dispatch ( features = response , params = params ) <EOL> if transactionResponse . summary . totalDeleted > <NUM_LIT:0> or transactionResponse . summary . totalInserted > <NUM_LIT:0> or transactionResponse . summary . totalUpdated > <NUM_LIT:0> or transactionResponse . summary . totalReplaced > <NUM_LIT:0> : <EOL> response = transactionResponse <EOL> except ConnectionException as e : <EOL> exceptionReport . add ( e ) <EOL> except LayerNotFoundException as e : <EOL> exceptionReport . add ( e ) <EOL> if len ( exceptionReport ) > <NUM_LIT:0> : <EOL> if self . metadata . has_key ( "<STR_LIT>" ) : <EOL> service_module = __import__ ( "<STR_LIT>" % self . metadata [ '<STR_LIT>' ] , globals ( ) , locals ( ) , self . metadata [ '<STR_LIT>' ] ) <EOL> service = getattr ( service_module , self . metadata [ '<STR_LIT>' ] ) <EOL> default_exception = service ( self ) <EOL> if hasattr ( default_exception , "<STR_LIT>" ) : <EOL> mime , data , headers , encoding = default_exception . encode_exception_report ( exceptionReport ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> if hasattr ( request , "<STR_LIT>" ) : <EOL> mime , data , headers , encoding = request . encode_exception_report ( exceptionReport ) <EOL> else : <EOL> service_module = __import__ ( "<STR_LIT>" % self . metadata [ '<STR_LIT>' ] , globals ( ) , locals ( ) , self . metadata [ '<STR_LIT>' ] ) <EOL> service = getattr ( service_module , self . metadata [ '<STR_LIT>' ] ) <EOL> default_service = service ( self ) <EOL> if hasattr ( default_service , "<STR_LIT>" ) : <EOL> mime , data , headers , encoding = default_service . encode_exception_report ( exceptionReport ) <EOL> else : <EOL> from FeatureServer . Service . WFS import WFS <EOL> wfs_service = WFS ( self ) <EOL> mime , data , headers , encoding = wfs_service . encode_exception_report ( exceptionReport ) <EOL> else : <EOL> mime , data , headers , encoding = request . encode ( response ) <EOL> return Response ( data = data , content_type = mime , headers = headers , status_code = response_code , encoding = encoding ) <EOL> def dispatchWorkspaceRequest ( self , base_path = "<STR_LIT>" , path_info = "<STR_LIT:/>" , params = { } , request_method = "<STR_LIT:GET>" , post_data = None , accepts = "<STR_LIT>" ) : <EOL> handler = FileHandler ( '<STR_LIT>' ) <EOL> handler . removeExpired ( ) <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> identifier = '<STR_LIT>' <EOL> if params . has_key ( '<STR_LIT:id>' ) : <EOL> identifier = params [ '<STR_LIT:id>' ] <EOL> short = handler . create ( params [ '<STR_LIT>' ] , params [ '<STR_LIT>' ] , identifier ) <EOL> output = "<STR_LIT>" <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> output += params [ "<STR_LIT>" ] + '<STR_LIT:(>' <EOL> output += '<STR_LIT>' + short + '<STR_LIT>' <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> output += '<STR_LIT>' <EOL> return Response ( data = output . decode ( "<STR_LIT:utf-8>" ) , content_type = "<STR_LIT>" , status_code = "<STR_LIT>" ) <EOL> elif params . has_key ( '<STR_LIT:key>' ) : <EOL> handler . updateLastAccess ( params [ '<STR_LIT:key>' ] ) <EOL> data = handler . getByKey ( params [ '<STR_LIT:key>' ] ) <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> for layer in self . datasources : <EOL> if layer == data [ <NUM_LIT:2> ] : <EOL> self . datasources = { layer : self . datasources [ layer ] } <EOL> self . datasources [ layer ] . abstract += "<STR_LIT>" + str ( data [ <NUM_LIT:0> ] ) <EOL> break <EOL> if params . has_key ( '<STR_LIT>' ) : <EOL> if params [ '<STR_LIT>' ] . lower ( ) == '<STR_LIT>' : <EOL> if params . has_key ( '<STR_LIT>' ) < > True : <EOL> if post_data == None : <EOL> params [ '<STR_LIT>' ] = data [ <NUM_LIT:3> ] <EOL> return self . dispatchRequest ( base_path , path_info , params , request_method , post_data , accepts ) <EOL> elif params . has_key ( '<STR_LIT>' ) : <EOL> output = "<STR_LIT>" <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> output += params [ "<STR_LIT>" ] + '<STR_LIT:(>' <EOL> output += '<STR_LIT>' <EOL> data = handler . getByKey ( params [ '<STR_LIT>' ] ) <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> date = time . strftime ( "<STR_LIT>" , time . localtime ( float ( data [ <NUM_LIT:4> ] ) ) ) <EOL> output += '<STR_LIT>' + data [ <NUM_LIT:0> ] + '<STR_LIT>' + date + '<STR_LIT>' <EOL> output += "<STR_LIT>" <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> output += '<STR_LIT>' <EOL> return Response ( data = output . decode ( "<STR_LIT:utf-8>" ) , content_type = "<STR_LIT>" , status_code = "<STR_LIT>" ) <EOL> elif params . has_key ( '<STR_LIT>' ) : <EOL> output = "<STR_LIT>" <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> output += params [ "<STR_LIT>" ] + '<STR_LIT:(>' <EOL> output += '<STR_LIT>' <EOL> workspaces = handler . getByIdentifier ( params [ '<STR_LIT>' ] ) <EOL> for data in workspaces : <EOL> date = time . strftime ( "<STR_LIT>" , time . localtime ( float ( data [ <NUM_LIT:4> ] ) ) ) <EOL> output += '<STR_LIT>' + data [ <NUM_LIT:0> ] + '<STR_LIT>' + date + '<STR_LIT>' <EOL> if len ( data ) > <NUM_LIT:0> : <EOL> output = output [ : - <NUM_LIT:1> ] <EOL> output += "<STR_LIT>" <EOL> if params . has_key ( "<STR_LIT>" ) : <EOL> output += '<STR_LIT>' <EOL> return Response ( data = output . decode ( "<STR_LIT:utf-8>" ) , content_type = "<STR_LIT>" , status_code = "<STR_LIT>" ) <EOL> return None <EOL> theServer = None <EOL> lastRead = <NUM_LIT:0> <EOL> def wsgi_app ( environ , start_response ) : <EOL> global theServer , lastRead <EOL> last = <NUM_LIT:0> <EOL> for cfg in cfgfiles : <EOL> try : <EOL> cfgTime = os . stat ( cfg ) [ <NUM_LIT:8> ] <EOL> if cfgTime > last : <EOL> last = cfgTime <EOL> except : <EOL> pass <EOL> if not theServer or last > lastRead : <EOL> cfgs = cfgfiles <EOL> theServer = Server . load ( * cfgs ) <EOL> lastRead = time . time ( ) <EOL> return wsgi ( theServer . dispatchRequest , environ , start_response ) <EOL> def wsgi_app_workspace ( environ , start_response ) : <EOL> global theServer , lastRead <EOL> last = <NUM_LIT:0> <EOL> for cfg in cfgfiles : <EOL> try : <EOL> cfgTime = os . stat ( cfg ) [ <NUM_LIT:8> ] <EOL> if cfgTime > last : <EOL> last = cfgTime <EOL> except : <EOL> pass <EOL> if not theServer or last > lastRead : <EOL> cfgs = cfgfiles <EOL> theServer = Server . load ( * cfgs ) <EOL> lastRead = time . time ( ) <EOL> return wsgi ( theServer . dispatchWorkspaceRequest , environ , start_response ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> service = Server . load ( * cfgfiles ) <EOL> cgi ( service ) </s>
