summaryrefslogtreecommitdiffstats
path: root/ontogen.py
blob: 75318a0fa138e772cbce19fa1c48385d7bdb0e3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# vim: set fileencoding=UTF-8:
# Copyright 2013 Red Hat, Inc.
# Author: Jan Pokorný <jpokorny at redhat dot com>
# Licensed under LGPL v2.1 (the same as original ns-schema.xsl)

from sys import stdout, stderr
from datetime import date
from textwrap import TextWrapper, dedent
from os import extsep, mkdir, symlink, remove
from os.path import basename, exists, isdir, islink, splitext
from subprocess import call
from shlex import split
#import codecs

import logging

logging.basicConfig()
log = logging.getLogger(__name__)


VERSIONSEP = '/'
ONTSEP = '#'

TEMPLATE_GENSHI = 'ontogen.template'
TEMPLATE_XSLT = 'ns-schema.xsl'
TEXT_INDENT = '      '
TW = TextWrapper(initial_indent=TEXT_INDENT, subsequent_indent=TEXT_INDENT)
tw = lambda x: TW.fill(x)
twd = lambda x: tw(dedent(x))
norm = lambda x: reduce(
    lambda a, b: a and a + (b.islower() and b or '-' + b.lower()) or b.lower(),
    x,
    ''
).replace('_', '-')


#
# Helpers
#


# classics: http://stackoverflow.com/a/1383402
class ClassProperty(property):
    def __get__(self, this, owner):
        return self.fget.__get__(None, owner)()


# another common fixer (adjusted): http://stackoverflow.com/a/13937525
class InheritDocstring(type):
    def __init__(this, name, bases, attrs):
        if getattr(this, '__doc__', None) is None:
            setattr(this, '__doc__', super(this, this).__doc__)


def force_symlink(source, link_name):
    if exists(link_name):
        if islink(link_name):
            remove(link_name)
        else:
            raise RuntimeError('{0} is in our way'.format(link_name))
    symlink(source, link_name)


#
# Pure documentation level
#


class Example(object):
    """Important data regarding examples"""
    @ClassProperty
    @classmethod
    def comment(this):
        return tw('\n'.join(this.__doc__.strip().splitlines()[:1]))

    @ClassProperty
    @classmethod
    def pfx(this):
        return ''

    @ClassProperty
    @classmethod
    def code(this):
        return '\n'.join(map(str.rstrip, this.__doc__.strip().splitlines()[2:]))

    @ClassProperty
    @classmethod
    def image(this):
        return ''


#
# Abstract RDF level
#


class RDFEntity(object):
    """Common base for property, class and ontology"""

    __metaclass__ = InheritDocstring

    @ClassProperty
    @classmethod
    def id(this):
        return ONTSEP + norm(this.__name__)

    @ClassProperty
    @classmethod
    def namespaces(this):
        return {}

    @ClassProperty
    @classmethod
    def label(this):
        return '\n'.join(this.__doc__.strip().splitlines()[:1]).strip()

    @ClassProperty
    @classmethod
    def comment(this):
        return '\n\n'.join(
            map(
                twd,
                '\n'.join(map(
                    str.rstrip,
                    this.__doc__.strip().splitlines()[2:])).split('\n\n')
            )
        )

    @ClassProperty
    @classmethod
    def status(this):
        return ''


#
# RDF entities
#


class Class(RDFEntity):
    """Important data regarding RDF class"""

    # decorators

    @classmethod
    def inDomainOf(this, prop):
        """Tells that decorated Property has decorating Class as a domain"""
        assert issubclass(prop, Property)
        if not prop.domain:
            prop.domain = this.id
        else:
            log.warning('{0} already has domain set'
                        .format(prop))
        return prop

    @classmethod
    def inRangeOf(this, prop):
        """Tells that decorated Property has decorating Class as a range"""
        assert issubclass(prop, Property)
        if not prop.range:
            prop.range = this.id
        else:
            log.warning('{0} already has range set'
                        .format(prop))
        return prop

    # members

    @ClassProperty
    @classmethod
    def subClassOf(this):
        return '' if this.__base__ == Class else this.__base__.id


class Property(RDFEntity):
    """Important data regarding RDF property"""

    # members

    @ClassProperty
    @classmethod
    def domain(this):
        return ''

    @ClassProperty
    @classmethod
    def range(this):
        return ''

    @ClassProperty
    @classmethod
    def subPropertyOf(this):
        return '' if this.__base__ == Property else this.__base__.id


class Ontology(RDFEntity):
    """Important data regarding ontology"""

    # decorators

    @classmethod
    def supersededBy(this, onto):
        """Tells that decorated Ontology supersedes the decorating Ontology"""
        assert issubclass(onto, Ontology)
        if onto.priorVersion is None:
            onto.priorVersion = this
        else:
            log.warning('{0} already has priorVersion set'.format(onto))
        return onto

    # members

    @ClassProperty
    @classmethod
    def base_uri(this):
        raise NotImplementedError

    @ClassProperty
    @classmethod
    def version(this):
        # sorting key
        raise NotImplementedError

    @ClassProperty
    @classmethod
    def base(this):
        return this.base_uri + VERSIONSEP + this.version

    @ClassProperty
    @classmethod
    def creator(this):
        return ''

    @ClassProperty
    @classmethod
    def issued(this):
        return ''

    @ClassProperty
    @classmethod
    def modified(this):
        return date.today().isoformat()

    @ClassProperty
    @classmethod
    def priorVersion(this):
        return None

    @ClassProperty
    @classmethod
    def classes(this):
        return []

    @ClassProperty
    @classmethod
    def properties(this):
        return []

    @ClassProperty
    @classmethod
    def examples(this):
        return []

    # extras

    @classmethod
    def valid(this):
        proper_subclasses = all((
            all(issubclass(c, Class) for c in this.classes),
            all(issubclass(p, Property) for p in this.properties),
            all(issubclass(e, Example) for e in this.examples),
        ))
        if not proper_subclasses:
            log.warning('Not proper subclasses used')

        ids = [
            x.id for x in
                reduce(lambda a, b: a + b,
                       (getattr(this, ent_name) for ent_name in ('classes',
                                                                 'properties')))
        ]
        unique_ids = len(ids) == len(set(ids))
        if not unique_ids:
            log.warning('Not unique IDs used')

        return proper_subclasses and unique_ids

    @classmethod
    def _auto_filename(this):
        base = this.base_uri.rpartition('/')[2]
        if not exists(base):
            mkdir(base)
        elif not isdir(base):
            raise RuntimeError('{0} is not a directory'.format(base))
        return (base + VERSIONSEP + this.version + extsep + 'rdf',
                base + extsep + 'rdf')

    @classmethod
    def gendoc(this, infile, outfile, template=TEMPLATE_XSLT):
        cmd = 'xsltproc --stringparam xmlfile {reference} -o {outfile}' \
              ' {template} {infile}'.format(
                  reference=basename(infile),
                  infile=infile,
                  outfile=outfile,
                  template=template
              )
        log.debug('running {0}'.format(split(cmd)))
        if call(split(cmd)):
            raise RuntimeError('Something went wrong while running {0}'
                               .format(cmd))

    @classmethod
    def generate(this, ontologies=(), template=None, outfile='-', gendoc=True):
        from genshi.template import MarkupTemplate

        namespaces = {}
        for ns, ns_uri in reduce(
                lambda a, b: a + b.namespaces.items(),
                [this] + this.classes + this.properties,
                []
        ):
            if namespaces.setdefault('xmlns:' + ns, ns_uri) != ns_uri:
                print >>stderr, 'NS clash: {0} vs {1}'.format(
                                namespaces['xmlns:' + ns], ns_uri)

        tmpl_filename = template or TEMPLATE_GENSHI
        tmpl_fileobj = open(tmpl_filename)
        #tmpl_fileobj = codecs.open(tmpl_filename, encoding='UTF-8')
        tmpl = MarkupTemplate(tmpl_fileobj, tmpl_filename)
        tmpl_fileobj.close()

        symfile = None
        if outfile is '-':
            outobj = stdout
        else:
            if outfile == 'AUTO':
                outfile, symfile = this._auto_filename()
            outobj = open(outfile, "w")

        tmpldict = dict(Ontology=this, namespaces=namespaces)
        print >>outobj, \
              tmpl.generate(**tmpldict).render('xml', strip_whitespace=False)
        outobj.flush()  # usually failing without this

        # htmldoc
        htmlfile = None
        if gendoc and outfile != '-':
            htmlfile = splitext(outfile)[0] + extsep + 'html'
            this.gendoc(outfile, htmlfile)

        # symlink to base (non-versioned) only for the newest ontology
        if ontologies[-1] is this and symfile:
            force_symlink(outfile, symfile)
            if htmlfile:
                force_symlink(htmlfile, splitext(symfile)[0] + extsep + 'html')

        if outobj is not stdout:
            outobj.close()

    @classmethod
    def __str__(this):
        this.generate()


#
# Generalized view
#

class Ontologies(object):
    """Groups multiple versions of ontologies (presumably of the same base)"""
    def __init__(self, ontologies=()):
        self._ontologies = []
        if ontologies:
            self.include(*ontologies)

    # decorators

    def include(self, *ontologies):
        assert all(map(lambda o: o.valid(), ontologies))
        assert reduce(lambda a, b: issubclass(a, b) or issubclass(b, a),
                      ontologies)
        merge = self._ontologies + list(ontologies)
        self._ontologies = list(sorted(set(merge), key=lambda o: o.version))
        # make it decorator-compatible
        return (lambda first=None, *others: first)(*ontologies)

    # members

    def generate_latest(self, **kwargs):
        self._ontologies[-1].generate(tuple(self._ontologies), **kwargs)