summaryrefslogtreecommitdiff
path: root/interfaces.py
blob: baea478c7cee4fb7706ee05a8b8c3f7ecedbbe92 (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
#!/usr/bin/python
'''this file more or less describes the grammar used for constructing things out of Lego
not done. see http://mr-bucket.co.uk/GLIDE/LCD_File_Format.html#Implimentation
for part examples see http://guide.lugnet.com/partsref/search.cgi?q=XXXX
or http://img.lugnet.com/ld/XXXX.gif
'''
import os
import skdb

class Joint:
    '''should pull in some kinematics from elsewhere; PyODE?'''
    pass

class SphericalJoint(Joint): pass

class PlanarJoint(Joint): pass

class RevoluteJoint(Joint): pass

class PrismaticJoint(Joint): pass

class GearJoint(Joint): pass


class Discrete:
    '''allows only a certain set of values'''
    pass

grammar = {}

class Feature(skdb.Interface):
    yaml_tag='!lego_feature'
    yaml_flow_style=False
    def __init__(self, name=None, type=None, part=None):
        skdb.Interface.__init__(self, name=name, part=part)
        self.type=type
        
    def post_init_hook(self):
        try:
            type = self.type
            self.overlay(grammar[type])
        except KeyError: self.type = None
        try: name = self.name
        except AttributeError: name = None
        
    def compatible(self, other):
        if type(self.complement) is not list: self.complement = [self.complement]
        if type(other.complement) is not list: other.complement = [other.complement]
        if other.type in self.complement:
            assert self.type in other.complement, 'vice versa not fulfilled for "%s" and "%s"' %(self.type, other.type)
            return True
        else: return False
        
    def __repr__(self):
        try: part_name = self.part.name
        except AttributeError: part_name = None
        #return "%s(part=%s,name=%s, type=%s)" % (self.__class__.__name__, part_name, self.name, self.type)
        return "%s(name=%s)" % (self.__class__.__name__, self.name)
        
    def example_picture(self):
        '''example should be an ldraw number'''
        return "http://img.lugnet.com/ld/"+self.example+".gif"
        
    def example_lugnet(self):
        return "http://guide.lugnet.com/partsref/search.cgi?q="+self.example
        
    def example_peeron(self):
        return "http://www.peeron.com/inv/parts/"+self.example

class PressFit(Feature):
    '''this should probably link up with some other class'''
    pass

class SnapFit(Feature):
    '''same as PressFit but with less friction, and a restorative force'''
    pass

class Hinge(RevoluteJoint, SnapFit): pass

#class Grammar(skdb.FennObject, dict):
#    yaml_tag='!lego_grammar'

fh = skdb.package_file('lego', 'grammar.yaml')
grammar = skdb.load(fh)['features']

#stuff values
for key in grammar.keys():
    grammar[key].name = key
    grammar[key].type = key
def dump_grammar_file():
    '''you probably should pipe through 'grep -v name'''''
    import yaml
    return yaml.dump(grammar)

if __name__ == '__main__':
    print dump_grammar_file()