def __init__(self, request): self.request = request if hasattr(request, 'skos_registry') and request.skos_registry is not None: self.skos_registry = self.request.skos_registry else: raise SkosRegistryNotFoundException() # pragma: no cover
def __init__(self, request): self.request = request self.logged_in = request.authenticated_userid if hasattr(request, 'skos_registry') and request.skos_registry is not None: self.skos_registry = self.request.skos_registry else: raise SkosRegistryNotFoundException()
def __init__(self, request): self.request = request self.skos_manager = self.request.data_managers['skos_manager'] self.conceptscheme_manager = self.request.data_managers['conceptscheme_manager'] if hasattr(request, 'skos_registry') and request.skos_registry is not None: self.skos_registry = self.request.skos_registry else: raise SkosRegistryNotFoundException()
def __init__(self, request): self.request = request self.db = request.db self.scheme_id = self.request.matchdict['scheme_id'] if hasattr(request, 'skos_registry') and request.skos_registry is not None: self.skos_registry = self.request.skos_registry else: raise SkosRegistryNotFoundException() # pragma: no cover self.provider = self.skos_registry.get_provider(self.scheme_id) if not self.provider: raise ConceptSchemeNotFoundException(self.scheme_id) # pragma: no cover
def __init__(self, context, request): self.request = request self.db = request.db self.context = context self.logged_in = request.authenticated_userid self.scheme_id = self.request.matchdict['scheme_id'] if hasattr(request, 'skos_registry') and request.skos_registry is not None: self.skos_registry = self.request.skos_registry else: raise SkosRegistryNotFoundException() self.provider = self.skos_registry.get_provider(self.scheme_id) if not self.provider: raise ConceptSchemeNotFoundException(self.scheme_id)
def __init__(self, request): self.request = request self.scheme_id = self.request.matchdict['scheme_id'] if hasattr(request, 'skos_registry') and request.skos_registry is not None: self.skos_registry = self.request.skos_registry else: raise SkosRegistryNotFoundException() # pragma: no cover self.provider = self.skos_registry.get_provider(self.scheme_id) if not self.provider: raise ConceptSchemeNotFoundException( self.scheme_id) # pragma: no cover if 'c_id' in self.request.matchdict.keys(): self.c_id = self.request.matchdict['c_id'] if not self.c_id.isdigit() or not self.provider.get_by_id( int(self.c_id)): raise ConceptNotFoundException(self.c_id)
def main(): description = """\ Generate a config file for a LDF server. """ usage = "usage: %prog config_uri" parser = optparse.OptionParser(usage=usage, description=textwrap.dedent(description)) parser.add_option( '-l', '--location', dest='config_location', type='string', help='Specify where to put the config file. If not specified, this \ is set to the atramhasis.ldf.config_location from your ini file.') options, args = parser.parse_args(sys.argv[1:]) if not len(args) >= 1: print('You must provide at least one argument.') return 2 config_uri = args[0] env = bootstrap(config_uri) setup_logging(config_uri) config_location = options.config_location if config_location is None: config_location = env['registry'].settings.get( 'atramhasis.ldf.config_location', os.path.abspath(os.path.dirname(config_uri))) dump_location = env['registry'].settings.get( 'atramhasis.dump_location', os.path.abspath(os.path.dirname(config_uri))) ldf_baseurl = env['registry'].settings.get('atramhasis.ldf.baseurl', None) ldf_protocol = env['registry'].settings.get('atramhasis.ldf.protocol', None) request = env['request'] if hasattr(request, 'skos_registry') and request.skos_registry is not None: skos_registry = request.skos_registry else: raise SkosRegistryNotFoundException() # pragma: no cover start_time = time.time() ldfconfig = { 'title': 'Atramhasis LDF server', 'datasources': {}, 'prefixes': { 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#', 'owl': 'http://www.w3.org/2002/07/owl#', 'xsd': 'http://www.w3.org/2001/XMLSchema#', 'hydra': 'http://www.w3.org/ns/hydra/core#', 'void': 'http://rdfs.org/ns/void#', 'skos': 'http://www.w3.org/2004/02/skos/core#', 'skos-thes': 'http://purl.org/iso25964/skos-thes#' } } if ldf_baseurl: ldfconfig['baseURL'] = ldf_baseurl if ldf_protocol: ldfconfig['protocol'] = ldf_protocol pids = [] for p in skos_registry.get_providers(): if any([ not_shown in p.get_metadata()['subject'] for not_shown in ['external'] ]): continue pid = p.get_metadata()['id'] pids.append(pid) filename = os.path.join(dump_location, '%s-full' % pid) filename_ttl = filename + '.ttl' filename_hdt = filename + '.hdt' if os.path.isfile(filename_hdt): dumptype = 'HdtDatasource' dumpfile = filename_hdt else: dumptype = 'TurtleDatasource' dumpfile = filename_ttl sourceconfig = { 'title': p.concept_scheme.label().label if p.concept_scheme.label() else pid, 'type': dumptype, 'settings': { 'file': dumpfile } } for n in p.concept_scheme.notes: if n.type in ['definition', 'scopeNote']: sourceconfig['description'] = n.note break ldfconfig['datasources'][pid] = sourceconfig if len(pids): sourceconfig = { 'title': 'All conceptschemes', 'type': 'CompositeDatasource', 'description': 'All conceptschemes contained in this Atramhasis instance together.', 'settings': { 'references': pids } } ldfconfig['datasources']['composite'] = sourceconfig config_filename = os.path.join(config_location, 'ldf_server_config.json') with open(config_filename, 'w') as fp: json.dump(ldfconfig, fp, indent=4) print('Config written to %s.' % config_filename) print("--- %s seconds ---" % (time.time() - start_time)) env['closer']()
def test_skos_reg_error(self): error = SkosRegistryNotFoundException() self.assertIsNotNone(error) self.assertEqual( "'No SKOS registry found, please check your application setup'", str(error))
def main(): description = """\ Dump all conceptschemes to files. Will serialise as Turtle and RDF/XML format. """ usage = "usage: %prog config_uri" parser = optparse.OptionParser(usage=usage, description=textwrap.dedent(description)) parser.add_option( '-l', '--location', dest='dump_location', type='string', help='Specify where to dump the conceptschemes. If not specified, this \ is set to the atramhasis.dump_location from your ini file.') parser.add_option( '-r', '--rdf2hdt', dest='rdf2hdt', type='string', default=False, help= 'Specify where the rdf2hdt command can be found. If not specified, this \ is set to atramhasis.rdf2hdt from your ini file.') options, args = parser.parse_args(sys.argv[1:]) if not len(args) >= 1: print('You must provide at least one argument.') return 2 config_uri = args[0] env = bootstrap(config_uri) setup_logging(config_uri) dump_location = options.dump_location if dump_location is None: dump_location = env['registry'].settings.get( 'atramhasis.dump_location', os.path.abspath(os.path.dirname(config_uri))) rdf2hdt = options.rdf2hdt if not rdf2hdt: rdf2hdt = env['registry'].settings.get('atramhasis.rdf2hdt', False) request = env['request'] if hasattr(request, 'skos_registry') and request.skos_registry is not None: skos_registry = request.skos_registry else: raise SkosRegistryNotFoundException() # pragma: no cover counts = [] files = [] for p in skos_registry.get_providers(): if any([ not_shown in p.get_metadata()['subject'] for not_shown in ['external'] ]): continue start_time = time.time() pid = p.get_metadata()['id'] filename = os.path.join(dump_location, '%s-full' % pid) filename_ttl = '%s.ttl' % filename filename_rdf = '%s.rdf' % filename files.append(filename_ttl) print('Generating dump for %s' % pid) graph = utils.rdf_dumper(p) triples = len(graph) print('Number of triples in Graph: %d' % triples) csuri = URIRef(p.concept_scheme.uri) cs_triples = len(list(graph.predicate_objects(csuri))) print('Number of triples in Conceptscheme: %d' % cs_triples) count_concepts = len(list(graph.subjects(RDF.type, SKOS.Concept))) count_collections = len(list(graph.subjects(RDF.type, SKOS.Collection))) avg_concept_triples = (triples - cs_triples) / (count_concepts + count_collections) print('Average number of triples per concept: %d' % avg_concept_triples) counts.append({ 'conceptscheme_id': pid, 'triples': triples, 'conceptscheme_triples': cs_triples, 'avg_concept_triples': avg_concept_triples }) print('Dumping %s to Turtle: %s' % (pid, filename_ttl)) graph.serialize(destination=filename_ttl, format='turtle') print('Dumping %s to RDFxml: %s' % (pid, filename_rdf)) graph.serialize(destination=filename_rdf, format='pretty-xml') print("--- %s seconds ---" % (time.time() - start_time)) print('All files dumped to %s' % dump_location) if rdf2hdt: from subprocess import check_call, CalledProcessError for f in files: print('Converting %s to hdt' % f) hdtf = f.replace('.ttl', '.hdt') try: check_call([rdf2hdt, '-f', 'turtle', f, hdtf]) except CalledProcessError: # Turtle failed, let's try rdfxml rdff = f.replace('.ttl', '.rdf') check_call([rdf2hdt, '-f', 'rdfxml', rdff, hdtf]) print('All hdt files dumped to %s' % dump_location) with transaction.manager: dbsession = request.registry.dbmaker() manager = CountsManager(dbsession) for c in counts: cs_count = ConceptschemeCounts( conceptscheme_id=c['conceptscheme_id'], triples=c['triples'], conceptscheme_triples=c['conceptscheme_triples'], avg_concept_triples=c['avg_concept_triples']) manager.save(cs_count) env['closer']()