Commit 2b793cf5 authored by Ross Jones's avatar Ross Jones
Browse files

Working branch

parent bc14d62f
import collections
import logging
import datetime
import os
import re
import time
import sys
import resource
import lxml.etree as e
from pylons import config
from ckan.lib.cli import CkanCommand
# No other CKAN imports allowed until _load_config is run,
# or logging is disabled
NSMAP = {
"dcat": "http://www.w3.org/ns/dcat#",
"foaf": "http://xmlns.com/foaf/0.1/",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"dct": "http://purl.org/dc/terms/"
}
class CatalogCommand(CkanCommand):
"""
The catalog command will generate a DCAT description of the catalog.
To generate the DCAT description, you should run the following command:
paster catalog generate <OUTPUT_FILE> -c <PATH_TO_CONFIG>
"""
summary = __doc__.split('\n')[0]
usage = __doc__
max_args = 2
min_args = 2
def __init__(self, name):
super(CatalogCommand, self).__init__(name)
def command(self):
"""
"""
self._load_config()
self.log = logging.getLogger(__name__)
import ckan.model as model
model.Session.remove()
model.Session.configure(bind=model.meta.engine)
model.repo.new_revision()
self.log.info("Database access initialised")
if len(self.args) != 2:
self.log.error("You must specify the command and the output file")
return
cmd, output = self.args
if cmd == 'generate':
self.generate(output)
def generate(self, output):
import ckan.model as model
from ckan.lib.helpers import url_for
def T(ns, tag):
# Generate a namespaced tag
return '{%s}%s' % (NSMAP[ns], tag,)
def E(ns, tag, text=None):
# Generate an element with a namespaced tag
element = e.Element(T(ns, tag), nsmap=NSMAP)
if text:
element.text = text
return element
def D(package):
# Build a dataset element from a package
element = E('dcat', 'dataset')
ds = E('dcat', 'Dataset')
ds.attrib[T('rdf', 'about')] = "http://data.gov.uk" + url_for(controller='package',
action='read', id=package.name, )
ds.append(E('dct', 'description', e.CDATA(package.notes or u'')))
ds.append(E('dct', 'identifier', package.name))
ds.append(E('dct', 'issued', package.metadata_created.isoformat()))
ds.append(E('dct', 'modified', package.metadata_modified.isoformat()))
element.append(ds)
return element
root = E('rdf','RDF')
doc = e.ElementTree(root)
cat = E('dcat', 'Catalog')
# TODO: Pull this from site-url and root folder.
cat.attrib[T('rdf', 'about')] = "http://data.gov.uk/data"
root.append(cat)
cat.append(E('dct', 'title', 'A DCAT feed of datasets published on data.gov.uk'))
cat.append(E('dct', 'modified', datetime.datetime.now().isoformat()))
# TODO: Pull this from site-url and root folder.
cat.append(E('foaf', 'homepage', 'http://data.gov.uk/data'))
for dataset in model.Session.query(model.Package)\
.filter(model.Package.state=='active').yield_per(50):
self.log.info("%s" % resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
cat.append(D(dataset))
doc.write(output, pretty_print=True, xml_declaration=True, encoding='utf-8')
<html xmlns:py="http://genshi.edgewall.org/"
xmlns:i18n="http://genshi.edgewall.org/i18n"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:dc="http://purl.org/dc/terms/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dcat="http://www.w3.org/ns/dcat#"
typeof="dcat:Dataset"
about=""
py:strip="">
<py:def function="page_title">${c.pkg_dict.get('title', c.pkg_dict['name'])} - Datasets</py:def>
<py:match path="breadcrumbs">
<li><a href="/data/search">Datasets</a></li>
<li><a href="${h.url_for(controller='package',action='read',id=c.pkg_dict['name'])}">${c.pkg_dict.get('title', c.pkg_dict['name'])}</a></li>
<li><a href="${h.url_for(controller='related',action='read',id=c.pkg_dict['name'])}">Apps</a></li>
</py:match>
<py:def function="package_content">
<h2>Dataset Feedback</h2>
<div class="dataset-feedback-container">
${feedback_summary(c.pkg_dict,render_title=False,large=True)}
<a class="view-feedback-link" href="${h.url_for(controller='ckanext.dgu.controllers.feedback:FeedbackController',action='view',id=c.pkg.name)}">View all feedback &raquo;</a>
</div>
<div id="comments-container">
<!-- TODO do not use id=comments! It's a duplicate id with Drupal's content. -->
<div style="height: 50px;" class="comments-spinner"></div>
</div>
</py:def>
<xi:include href="read_common.html" />
</html>
......@@ -94,6 +94,7 @@ setup(
inventory_init = ckanext.dgu.commands.inventory_init:InitDB
commitment_init = ckanext.dgu.commands.commitment_init:InitDB
ingest = ckanext.dgu.commands.ingester:Ingester
catalog = ckanext.dgu.commands.catalog:CatalogCommand
""",
test_suite = 'nose.collector',
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment