def __init__(self, rtconfig=None, env=None, version='3.0.0'): """ Initialize CSW """ if not env: self.environ = os.environ else: self.environ = env self.context = config.StaticContext() # Lazy load this when needed # (it will permanently update global cfg namespaces) self.sruobj = None self.opensearchobj = None self.oaipmhobj = None # init kvp self.kvp = {} self.mode = 'csw' self.async = False self.soap = False self.request = None self.exception = False self.status = 'OK' self.profiles = None self.manager = False self.outputschemas = {} self.mimetype = 'application/xml; charset=UTF-8' self.encoding = 'UTF-8' self.pretty_print = 0 self.domainquerytype = 'list' self.orm = 'django' self.language = {'639_code': 'en', 'text': 'english'} self.process_time_start = time() # define CSW implementation object (default CSW3) self.iface = csw3.Csw3(server_csw=self) self.request_version = version if self.request_version == '2.0.2': self.iface = csw2.Csw2(server_csw=self) self.context.set_model('csw') # load user configuration try: LOGGER.info('Loading user configuration') if isinstance(rtconfig, SafeConfigParser): # serialized already self.config = rtconfig else: self.config = SafeConfigParser() if isinstance(rtconfig, dict): # dictionary for section, options in rtconfig.items(): self.config.add_section(section) for k, v in options.items(): self.config.set(section, k, v) else: # configuration file import codecs with codecs.open(rtconfig, encoding='utf-8') as scp: self.config.readfp(scp) except Exception as err: LOGGER.exception('Could not load user configuration: %s', err) self.response = self.iface.exceptionreport( 'NoApplicableCode', 'service', 'Error opening configuration %s' % rtconfig ) return # set server.home safely # TODO: make this more abstract self.config.set( 'server', 'home', os.path.dirname(os.path.join(os.path.dirname(__file__), '..')) ) self.context.pycsw_home = self.config.get('server', 'home') self.context.url = self.config.get('server', 'url') log.setup_logger(self.config) LOGGER.info('running configuration %s', rtconfig) LOGGER.debug('QUERY_STRING: %s', self.environ['QUERY_STRING']) # set OGC schemas location if not self.config.has_option('server', 'ogc_schemas_base'): self.config.set('server', 'ogc_schemas_base', self.context.ogc_schemas_base) # set mimetype if self.config.has_option('server', 'mimetype'): self.mimetype = self.config.get('server', 'mimetype').encode() # set encoding if self.config.has_option('server', 'encoding'): self.encoding = self.config.get('server', 'encoding') # set domainquerytype if self.config.has_option('server', 'domainquerytype'): self.domainquerytype = self.config.get('server', 'domainquerytype') # set XML pretty print if (self.config.has_option('server', 'pretty_print') and self.config.get('server', 'pretty_print') == 'true'): self.pretty_print = 1 # set Spatial Ranking option if (self.config.has_option('server', 'spatial_ranking') and self.config.get('server', 'spatial_ranking') == 'true'): util.ranking_enabled = True # set language default if self.config.has_option('server', 'language'): try: LOGGER.info('Setting language') lang_code = self.config.get('server', 'language').split('-')[0] self.language['639_code'] = lang_code self.language['text'] = self.context.languages[lang_code] except Exception as err: LOGGER.exception('Could not set language: %s', err) pass LOGGER.debug('Configuration: %s.', self.config) LOGGER.debug('Model: %s.', self.context.model) # load user-defined mappings if they exist if self.config.has_option('repository', 'mappings'): # override default repository mappings try: import imp module = self.config.get('repository', 'mappings') if '/' in module: # filepath modulename = '%s' % os.path.splitext(module)[0].replace( os.sep, '.') mappings = imp.load_source(modulename, module) else: # dotted name mappings = __import__(module, fromlist=['']) LOGGER.info('Loading custom repository mappings ' 'from %s', module) self.context.md_core_model = mappings.MD_CORE_MODEL self.context.refresh_dc(mappings.MD_CORE_MODEL) except Exception as err: LOGGER.exception('Could not load custom mappings: %s', err) self.response = self.iface.exceptionreport( 'NoApplicableCode', 'service', 'Could not load repository.mappings') # load outputschemas LOGGER.info('Loading outputschemas') for osch in pycsw.plugins.outputschemas.__all__: output_schema_module = __import__( 'pycsw.plugins.outputschemas.%s' % osch) mod = getattr(output_schema_module.plugins.outputschemas, osch) self.outputschemas[mod.NAMESPACE] = mod LOGGER.debug('Outputschemas loaded: %s.', self.outputschemas) LOGGER.debug('Namespaces: %s', self.context.namespaces)
def __init__(self, config: ConfigParser): """ constructor :param config: ConfigParser pycsw configuration dict :returns: `pycsw.ogc.api.API` instance """ self.config = config log.setup_logger(self.config) if self.config['server']['url'].startswith('${'): LOGGER.debug( f"Server URL is an environment variable: {self.config['server']['url']}" ) url_ = match_env_var(self.config['server']['url']) else: url_ = self.config['server']['url'] LOGGER.debug(f'Server URL: {url_}') self.config['server']['url'] = url_.rstrip('/') self.context = StaticContext() LOGGER.debug('Setting maxrecords') try: self.maxrecords = int(self.config['server']['maxrecords']) except KeyError: self.maxrecords = 10 LOGGER.debug(f'maxrecords: {self.maxrecords}') repo_filter = None if self.config.has_option('repository', 'filter'): repo_filter = self.config.get('repository', 'filter') self.orm = 'sqlalchemy' from pycsw.core import repository try: LOGGER.info('Loading default repository') self.repository = repository.Repository( self.config.get('repository', 'database'), self.context, # self.environ.get('local.app_root', None), None, self.config.get('repository', 'table'), repo_filter) LOGGER.debug(f'Repository loaded {self.repository.dbtype}') except Exception as err: msg = f'Could not load repository {err}' LOGGER.exception(msg) raise self.query_mappings = { 'type': self.repository.dataset.type, 'recordUpdated': self.repository.dataset.insert_date, 'title': self.repository.dataset.title, 'description': self.repository.dataset.abstract, 'keywords': self.repository.dataset.keywords, 'anytext': self.repository.dataset.anytext, 'bbox': self.repository.dataset.wkt_geometry }
def __init__(self, rtconfig=None, env=None, version='3.0.0'): """ Initialize CSW """ if not env: self.environ = os.environ else: self.environ = env self.context = config.StaticContext() # Lazy load this when needed # (it will permanently update global cfg namespaces) self.sruobj = None self.opensearchobj = None self.oaipmhobj = None # init kvp self.kvp = {} self.mode = 'csw' self.asynchronous = False self.soap = False self.request = None self.exception = False self.status = 'OK' self.profiles = None self.manager = False self.outputschemas = {} self.mimetype = 'application/xml; charset=UTF-8' self.encoding = 'UTF-8' self.pretty_print = 0 self.domainquerytype = 'list' self.orm = 'django' self.language = {'639_code': 'en', 'text': 'english'} self.process_time_start = time() # define CSW implementation object (default CSW3) self.iface = csw3.Csw3(server_csw=self) self.request_version = version if self.request_version == '2.0.2': self.iface = csw2.Csw2(server_csw=self) self.context.set_model('csw') # load user configuration try: LOGGER.info('Loading user configuration') if isinstance(rtconfig, SafeConfigParser): # serialized already self.config = rtconfig else: self.config = SafeConfigParser() if isinstance(rtconfig, dict): # dictionary for section, options in rtconfig.items(): self.config.add_section(section) for k, v in options.items(): self.config.set(section, k, v) else: # configuration file import codecs with codecs.open(rtconfig, encoding='utf-8') as scp: self.config.readfp(scp) except Exception as err: msg = 'Could not load configuration' LOGGER.exception('%s %s: %s', msg, rtconfig, err) self.response = self.iface.exceptionreport( 'NoApplicableCode', 'service', msg) return # set server.home safely # TODO: make this more abstract self.config.set( 'server', 'home', os.path.dirname(os.path.join(os.path.dirname(__file__), '..')) ) self.context.pycsw_home = self.config.get('server', 'home') self.context.url = self.config.get('server', 'url') log.setup_logger(self.config) LOGGER.info('running configuration %s', rtconfig) LOGGER.debug('QUERY_STRING: %s', self.environ['QUERY_STRING']) # set OGC schemas location if not self.config.has_option('server', 'ogc_schemas_base'): self.config.set('server', 'ogc_schemas_base', self.context.ogc_schemas_base) # set mimetype if self.config.has_option('server', 'mimetype'): self.mimetype = self.config.get('server', 'mimetype').encode() # set encoding if self.config.has_option('server', 'encoding'): self.encoding = self.config.get('server', 'encoding') # set domainquerytype if self.config.has_option('server', 'domainquerytype'): self.domainquerytype = self.config.get('server', 'domainquerytype') # set XML pretty print if (self.config.has_option('server', 'pretty_print') and self.config.get('server', 'pretty_print') == 'true'): self.pretty_print = 1 # set Spatial Ranking option if (self.config.has_option('server', 'spatial_ranking') and self.config.get('server', 'spatial_ranking') == 'true'): util.ranking_enabled = True # set language default if self.config.has_option('server', 'language'): try: LOGGER.info('Setting language') lang_code = self.config.get('server', 'language').split('-')[0] self.language['639_code'] = lang_code self.language['text'] = self.context.languages[lang_code] except Exception as err: LOGGER.exception('Could not set language: %s', err) pass LOGGER.debug('Configuration: %s.', self.config) LOGGER.debug('Model: %s.', self.context.model) # load user-defined mappings if they exist if self.config.has_option('repository', 'mappings'): # override default repository mappings try: import imp module = self.config.get('repository', 'mappings') if os.sep in module: # filepath modulename = '%s' % os.path.splitext(module)[0].replace( os.sep, '.') mappings = imp.load_source(modulename, module) else: # dotted name mappings = __import__(module, fromlist=['']) LOGGER.info('Loading custom repository mappings ' 'from %s', module) self.context.md_core_model = mappings.MD_CORE_MODEL self.context.refresh_dc(mappings.MD_CORE_MODEL) except Exception as err: LOGGER.exception('Could not load custom mappings: %s', err) self.response = self.iface.exceptionreport( 'NoApplicableCode', 'service', 'Could not load repository.mappings') # load outputschemas LOGGER.info('Loading outputschemas') for osch in pycsw.plugins.outputschemas.__all__: output_schema_module = __import__( 'pycsw.plugins.outputschemas.%s' % osch) mod = getattr(output_schema_module.plugins.outputschemas, osch) self.outputschemas[mod.NAMESPACE] = mod LOGGER.debug('Outputschemas loaded: %s.', self.outputschemas) LOGGER.debug('Namespaces: %s', self.context.namespaces)
def __init__(self, rtconfig=None, env=None, version="3.0.0"): """ Initialize CSW """ if not env: self.environ = os.environ else: self.environ = env self.context = config.StaticContext() # Lazy load this when needed # (it will permanently update global cfg namespaces) self.sruobj = None self.opensearchobj = None self.oaipmhobj = None # init kvp self.kvp = {} self.mode = "csw" self.async = False self.soap = False self.request = None self.exception = False self.status = "OK" self.profiles = None self.manager = False self.outputschemas = {} self.mimetype = "application/xml; charset=UTF-8" self.encoding = "UTF-8" self.pretty_print = 0 self.domainquerytype = "list" self.orm = "django" self.language = {"639_code": "en", "text": "english"} self.process_time_start = time() # define CSW implementation object (default CSW3) self.iface = csw3.Csw3(server_csw=self) self.request_version = version if self.request_version == "2.0.2": self.iface = csw2.Csw2(server_csw=self) self.context.set_model("csw") # load user configuration try: if isinstance(rtconfig, SafeConfigParser): # serialized already self.config = rtconfig else: self.config = SafeConfigParser() if isinstance(rtconfig, dict): # dictionary for section, options in rtconfig.items(): self.config.add_section(section) for k, v in options.items(): self.config.set(section, k, v) else: # configuration file import codecs with codecs.open(rtconfig, encoding="utf-8") as scp: self.config.readfp(scp) except Exception: self.response = self.iface.exceptionreport( "NoApplicableCode", "service", "Error opening configuration %s" % rtconfig ) return # set server.home safely # TODO: make this more abstract self.config.set("server", "home", os.path.dirname(os.path.join(os.path.dirname(__file__), ".."))) self.context.pycsw_home = self.config.get("server", "home") self.context.url = self.config.get("server", "url") log.setup_logger(self.config) LOGGER.debug("running configuration %s" % rtconfig) LOGGER.debug(str(self.environ["QUERY_STRING"])) # set OGC schemas location if not self.config.has_option("server", "ogc_schemas_base"): self.config.set("server", "ogc_schemas_base", self.context.ogc_schemas_base) # set mimetype if self.config.has_option("server", "mimetype"): self.mimetype = self.config.get("server", "mimetype").encode() # set encoding if self.config.has_option("server", "encoding"): self.encoding = self.config.get("server", "encoding") # set domainquerytype if self.config.has_option("server", "domainquerytype"): self.domainquerytype = self.config.get("server", "domainquerytype") # set XML pretty print if self.config.has_option("server", "pretty_print") and self.config.get("server", "pretty_print") == "true": self.pretty_print = 1 # set Spatial Ranking option if ( self.config.has_option("server", "spatial_ranking") and self.config.get("server", "spatial_ranking") == "true" ): util.ranking_enabled = True # set language default if self.config.has_option("server", "language"): try: LOGGER.info("Setting language") lang_code = self.config.get("server", "language").split("-")[0] self.language["639_code"] = lang_code self.language["text"] = self.context.languages[lang_code] except: pass LOGGER.debug("Configuration: %s." % self.config) LOGGER.debug("Model: %s." % self.context.model) # load user-defined mappings if they exist if self.config.has_option("repository", "mappings"): # override default repository mappings try: import imp module = self.config.get("repository", "mappings") modulename = "%s" % os.path.splitext(module)[0].replace(os.sep, ".") LOGGER.debug("Loading custom repository mappings " "from %s." % module) mappings = imp.load_source(modulename, module) self.context.md_core_model = mappings.MD_CORE_MODEL self.context.refresh_dc(mappings.MD_CORE_MODEL) except Exception as err: self.response = self.iface.exceptionreport( "NoApplicableCode", "service", "Could not load repository.mappings %s" % str(err) ) # load outputschemas LOGGER.debug("Loading outputschemas.") for osch in pycsw.plugins.outputschemas.__all__: output_schema_module = __import__("pycsw.plugins.outputschemas.%s" % osch) mod = getattr(output_schema_module.plugins.outputschemas, osch) self.outputschemas[mod.NAMESPACE] = mod LOGGER.debug("Outputschemas loaded: %s." % self.outputschemas) LOGGER.debug("Namespaces: %s" % self.context.namespaces)
def __init__(self, rtconfig=None, env=None, version='3.0.0'): """ Initialize CSW """ if not env: self.environ = os.environ else: self.environ = env self.context = config.StaticContext() # Lazy load this when needed # (it will permanently update global cfg namespaces) self.sruobj = None self.opensearchobj = None self.oaipmhobj = None # init kvp self.kvp = {} self.mode = 'csw' self.asynchronous = False self.soap = False self.request = None self.exception = False self.status = 'OK' self.profiles = None self.manager = False self.outputschemas = {} self.mimetype = 'application/xml; charset=UTF-8' self.encoding = 'UTF-8' self.pretty_print = 0 self.domainquerytype = 'list' self.orm = 'django' self.language = {'639_code': 'en', 'text': 'english'} self.process_time_start = time() self.xslts = [] # define CSW implementation object (default CSW3) self.iface = csw3.Csw3(server_csw=self) self.request_version = version if self.request_version == '2.0.2': self.iface = csw2.Csw2(server_csw=self) self.context.set_model('csw') # load user configuration try: LOGGER.info('Loading user configuration') if isinstance(rtconfig, configparser.ConfigParser): # serialized already self.config = rtconfig else: self.config = configparser.ConfigParser( interpolation=util.EnvInterpolation()) if isinstance(rtconfig, dict): # dictionary for section, options in rtconfig.items(): self.config.add_section(section) for k, v in options.items(): self.config.set(section, k, v) else: # configuration file import codecs with codecs.open(rtconfig, encoding='utf-8') as scp: self.config.read_file(scp) except Exception as err: msg = 'Could not load configuration' LOGGER.exception('%s %s: %s', msg, rtconfig, err) self.response = self.iface.exceptionreport('NoApplicableCode', 'service', msg) return # set server.home safely # TODO: make this more abstract self.config.set( 'server', 'home', os.path.dirname(os.path.join(os.path.dirname(__file__), '..'))) if 'PYCSW_IS_CSW' in env and env['PYCSW_IS_CSW']: self.config.set('server', 'url', self.config['server']['url'].rstrip('/') + '/csw') if 'PYCSW_IS_OPENSEARCH' in env and env['PYCSW_IS_OPENSEARCH']: self.config.set( 'server', 'url', self.config['server']['url'].rstrip('/') + '/opensearch') self.mode = 'opensearch' self.context.pycsw_home = self.config.get('server', 'home') self.context.url = self.config.get('server', 'url') log.setup_logger(self.config) LOGGER.info('running configuration %s', rtconfig) LOGGER.debug('QUERY_STRING: %s', self.environ['QUERY_STRING']) # set OGC schemas location if not self.config.has_option('server', 'ogc_schemas_base'): self.config.set('server', 'ogc_schemas_base', self.context.ogc_schemas_base) # set mimetype if self.config.has_option('server', 'mimetype'): self.mimetype = self.config.get('server', 'mimetype').encode() # set encoding if self.config.has_option('server', 'encoding'): self.encoding = self.config.get('server', 'encoding') # set domainquerytype if self.config.has_option('server', 'domainquerytype'): self.domainquerytype = self.config.get('server', 'domainquerytype') # set XML pretty print if (self.config.has_option('server', 'pretty_print') and self.config.get('server', 'pretty_print') == 'true'): self.pretty_print = 1 # set Spatial Ranking option if (self.config.has_option('server', 'spatial_ranking') and self.config.get('server', 'spatial_ranking') == 'true'): util.ranking_enabled = True # set language default if self.config.has_option('server', 'language'): try: LOGGER.info('Setting language') lang_code = self.config.get('server', 'language').split('-')[0] self.language['639_code'] = lang_code self.language['text'] = self.context.languages[lang_code] except Exception as err: LOGGER.exception('Could not set language: %s', err) pass LOGGER.debug('Configuration: %s.', self.config) LOGGER.debug('Model: %s.', self.context.model) # load user-defined mappings if they exist custom_mappings_path = self.config.get('repository', 'mappings', fallback=None) if custom_mappings_path is not None: md_core_model = util.load_custom_repo_mappings( custom_mappings_path) if md_core_model is not None: self.context.md_core_model = md_core_model self.context.refresh_dc(md_core_model) else: LOGGER.exception('Could not load custom mappings: %s') self.response = self.iface.exceptionreport( 'NoApplicableCode', 'service', 'Could not load repository.mappings') # load user-defined max attempt to retry db connection try: self.max_retries = int(self.config.get("repository", "max_retries")) except configparser.NoOptionError: self.max_retries = 5 # load outputschemas LOGGER.info('Loading outputschemas') for osch in pycsw.plugins.outputschemas.__all__: output_schema_module = __import__( 'pycsw.plugins.outputschemas.%s' % osch) mod = getattr(output_schema_module.plugins.outputschemas, osch) self.outputschemas[mod.NAMESPACE] = mod LOGGER.debug('Outputschemas loaded: %s.', self.outputschemas) LOGGER.debug('Namespaces: %s', self.context.namespaces) LOGGER.info('Loading XSLT transformations') xslt_defs = [ x for x in self.config.sections() if x.startswith('xslt:') ] for x in xslt_defs: LOGGER.debug('Loading XSLT %s' % x) input_os, output_os = x.split(':', 1)[-1].split(',') self.xslts.append({x: self.config.get(x, 'xslt')})
def __init__(self, config: ConfigParser): """ constructor :param config: ConfigParser pycsw configuration dict :returns: `pycsw.ogc.api.API` instance """ self.config = config log.setup_logger(self.config) if self.config['server']['url'].startswith('${'): LOGGER.debug(f"Server URL is an environment variable: {self.config['server']['url']}") url_ = match_env_var(self.config['server']['url']) else: url_ = self.config['server']['url'] LOGGER.debug(f'Server URL: {url_}') self.config['server']['url'] = url_.rstrip('/') self.context = StaticContext() LOGGER.debug('Setting maxrecords') try: self.maxrecords = int(self.config['server']['maxrecords']) except KeyError: self.maxrecords = 10 LOGGER.debug(f'maxrecords: {self.maxrecords}') repo_filter = None if self.config.has_option('repository', 'filter'): repo_filter = self.config.get('repository', 'filter') custom_mappings_path = self.config.get('repository', 'mappings', fallback=None) if custom_mappings_path is not None: md_core_model = load_custom_repo_mappings(custom_mappings_path) if md_core_model is not None: self.context.md_core_model = md_core_model else: LOGGER.exception( 'Could not load custom mappings: %s', custom_mappings_path) self.orm = 'sqlalchemy' from pycsw.core import repository try: LOGGER.info('Loading default repository') self.repository = repository.Repository( self.config.get('repository', 'database'), self.context, # self.environ.get('local.app_root', None), None, self.config.get('repository', 'table'), repo_filter ) LOGGER.debug(f'Repository loaded {self.repository.dbtype}') except Exception as err: msg = f'Could not load repository {err}' LOGGER.exception(msg) raise self.query_mappings = { 'type': self.repository.dataset.type, 'parentidentifier': self.repository.dataset.parentidentifier, 'collections': self.repository.dataset.parentidentifier, 'recordUpdated': self.repository.dataset.insert_date, 'title': self.repository.dataset.title, 'description': self.repository.dataset.abstract, 'keywords': self.repository.dataset.keywords, 'anytext': self.repository.dataset.anytext, 'bbox': self.repository.dataset.wkt_geometry, 'date': self.repository.dataset.date, 'time_begin': self.repository.dataset.time_begin, 'time_end': self.repository.dataset.time_end, 'platform': self.repository.dataset.platform, 'instrument': self.repository.dataset.instrument, 'sensortype': self.repository.dataset.sensortype } if self.repository.dbtype == 'postgresql+postgis+native': self.query_mappings['bbox'] = self.repository.dataset.wkb_geometry