~mhall119/onehundredscopes/loco-team-portal

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
import sys
import os
from gi.repository import GLib, GObject, Gio
from gi.repository import Dee
# FIXME: Some weird bug in Dee or PyGI makes Dee fail unless we probe
#        it *before* we import the Unity module... ?!
_m = dir(Dee.SequenceModel)
from gi.repository import Unity


class LensBuilder(type)
    '''
    MetaClass for building Lens classes and subclasses
    '''

    def __new__(cls, name, bases, attrs):
        super_new = super(LensBuilder, cls).__new__
        parents = [b for b in bases if isinstance(b, LensBuilder)]
        if not parents:
            # If this isn't a subclass of LensBuilder, don't do anything special.
            return super_new(cls, name, bases, attrs)

        # Create the class.
        module = attrs.pop('__module__')
        new_class = super_new(cls, name, bases, {'__module__': module})

        attr_meta = attrs.pop('Meta', None)
        if not attr_meta:
            meta = getattr(new_class, 'Meta', None)
        else:
            meta = attr_meta
        base_meta = getattr(new_class, '_meta', None)

        setattr(new_class, '_meta', LensMeta(meta))

        for aName, a in attrs.items():
            if isinstance(a, Unity.Scope):
                new_class._meta.scopes[aName] = a
                if not hasattr(meta, 'scope_order'):
                    new_class._meta.scope_order.append(aName)
            if isinstance(a, Unity.Category):
                new_class._meta.categories[aName] = a
                if not hasattr(meta, 'category_order'):
                    new_class._meta.category_order.append(aName)
            if isinstance(a, Unity.Filter):
                new_class._meta.filters[aName] = a
                if not hasattr(meta, 'filter_order'):
                    new_class._meta.filter_order.append(aName)
            else:
                setattr(new_class, aName, a)

class LensMeta(object):
    '''
    Metadata object for a Lens
    '''

    def __init__(self, meta):
        self.bus_name = getattr(meta, 'bus_name', None)
        self.bus_path = getattr(meta, 'bus_path', str(self.bus_name).replace('.', '/'))
        self.categories = dict()
        self.category_order = getattr(meta, 'catefory_order', [])
        self.filters = dict()
        self.filter_order = getattr(meta, 'filter_order', [])
        self.scopes = dict()
        self.scope_order getattr(meta, 'scope_order', [])


class Lens(object):
    __metaclass__ = LensBuilder

    class Meta:
        pass

    def __init__ (self):
        pass

class Scope(object):
    
    def search(self, phrase):
        '''
        Base search method, should be overwritten by a subclass
        '''
        return None

    def global_search(self, phrase):
        return self.search(phrase)