On Sun, 24 Apr 2011 19:24:58 +0100, Ricardo Gladwell wrote:
That would be great, thanks!

--
Ricardo Gladwell <[email protected]>
http://www.google.com/profiles/ricardo.gladwell
Twitter: @rgladwell - MSN: [email protected]


On Sun, 2011-04-24 at 14:11 -0400, Jonathan Ryan wrote:
On Sun, 24 Apr 2011 19:53:17 +0200, Luca Bruno wrote:
> On Sun, Apr 24, 2011 at 06:48:39PM +0100, Ricardo Gladwell wrote:
>> Hi Everyone
>>
>> Is there anyway to find out if a class is annotated with an
>> attribute,
>> and retrieve values from these attributes?
>
> The only way is using libvala to parse your own code.
>
>>
>> If not, are there any plans to provide such a feature?
>
> Not at the moment, it could be implemented for GObject classes
> though.

 You can also do it using GObject-Introspection by parsing Girs.
 I wrote a small python script to do it if you would like to see it.


_______________________________________________
vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

--
Jonathan Ryan
#! /usr/bin/env python
# Copyright 2011, Curious Computing LLC
from xml.parsers import expat

class Annotation:
    def __init__(self, type, on, key, value):
        self.type = type
        self.on = on
        self.name = on['glib:type-name']
        self.key = key
        self.value = value

    def __str__(self):
        return '<Annotation on %s %s: key=%s, value=%s>' %  (self.type,
        self.name, self.key, self.value)

    def __repr__(self):
        return "{u'type': u'%s', u'on': %s, u'key': '%s', u'value': '%s'}" % (self.type,
        self.on, self.key, self.value)

class AnnotationParser:
    def __init__(self):
        self._parser = expat.ParserCreate()
        self._parser.StartElementHandler = self.start
        self._parser.EndElementHandler = self.end
        #self._parser.CharacterDataHandler = self.data
        self.namespace = None
        self.version = None
        self.package = None
        self.enclosed = []
        self.annotations = {}
        self.classes = [] 

    def feed(self, data):
        self._parser.Parse(data, 0)

    def close(self):
        self._parser.Parse("", 1) # end of data
        del self._parser # get rid of circular references

    def start(self, tag, attrs):
        self.parse_tag(tag, attrs)
        self.enclosed.append((tag, attrs))

    def parse_tag(self, tag, attrs):
        if tag == 'annotation':
            a = Annotation(self.enclosed[-1][0], self.enclosed[-1][1], attrs['key'], attrs['value'])
            self.annotations.setdefault(a.name, {})[a.key] = a.value
        elif tag == 'class':
            self.classes.append(attrs['glib:type-name'])
        elif tag == 'namespace':
            self.namespace = attrs['name']
            self.version = attrs['version']
        elif tag == 'package':
            self.package = attrs['name']

    def end(self, tag):
        self.enclosed.pop()

    def data(self, data):
        pass

if __name__ == '__main__':
    import sys
    import subprocess
    import os
    for f_name in sys.argv[1:]:
        p = AnnotationParser() 
        f = open(f_name)
        p.feed(f.read())

        d = {
            'memory-to-actor-serialization.from' : [],
            'file-to-memory-serialization.to' : [],
            'file-to-memory-serialization.extensions' : [],
            'classes' : [],
            'versions' : []
        }

        for k,v in p.annotations.items():
            for k2,v2 in v.items():
                for i in v2.split(' '):
                    d[k2].append(i)
                    if k2 == 'file-to-memory-serialization.extensions':
                        try:
                            d[k2].append(v['file-to-memory-serialization.to'])
                        except KeyError:
                            print(k + '[' + k2 + ']' + ' is missing a to attribute')
                            raise
                    else:
                        d[k2].append(k)

        module_name = p.namespace
        for c in p.classes:
            d['classes'].extend([c, module_name]);
    
        d['versions'].extend([module_name, p.version])
        
        curious_home = os.environ.get('CURIOUS_HOME')
        if not curious_home:
            curious_home = os.environ.get('HOME') + '/.local/share/curious'
            print("WARNING: CURIOUS_HOME environment variable not set! guessing...")

        def cache(map, pairs):
            if pairs:
                cmd =['curious-cache', curious_home + '/' + map, '-p'] 
                cmd.extend(pairs)
                subprocess.call(cmd)

        cache('system/type_to_module.map', d['classes'])
        cache('system/module_to_version.map', d['versions'])
        cache('system/serializations/from_memory_to_actor.map', 
              d['memory-to-actor-serialization.from'])

        cache('system/serializations/from_file_to_memory.map',
              d['file-to-memory-serialization.to'])

        cache('system/serializations/extensions_to_memory.map',
              d['file-to-memory-serialization.extensions'])

# vim: ft=python ts=4 sts=4 sw=4 et
_______________________________________________
vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to