File: lexicon.py

package info (click to toggle)
pythoncard 0.8.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 8,452 kB
  • sloc: python: 56,787; makefile: 56; sh: 22
file content (145 lines) | stat: -rw-r--r-- 4,956 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python

"""
A fairly simple implementation of Conway's Game of Life.
"""
__version__ = "$Revision: 1.19 $"
__date__ = "$Date: 2005/12/13 11:13:23 $"

from PythonCard import model, util
import wx

import os

from util import readLifeFile, translateClipboardPattern

"""
:101: (p5)  Found by Achim Flammenkamp in August 1994.  The name was
   suggested by Bill Gosper, noting that the phase shown below displays
   the period in binary.
    ....**......**....
    ...*.*......*.*...
    ...*..........*...
    **.*..........*.**
    **.*.*..**..*.*.**
    ...*.*.*..*.*.*...
    ...*.*.*..*.*.*...
    **.*.*..**..*.*.**
    **.*..........*.**
    ...*..........*...
    ...*.*......*.*...
    ....**......**....

"""

def readLexiconFile(path):
    try:
        fp = open(path)
        data = fp.readlines()
        fp.close()
    except IOError:
        return None
    lexicon = {}
    # first collect the header
    # up to the first row of
    # ------
    # then collect the definitions and patterns
    # and finally the bibliography after
    # the second row of
    # -----
    inIntroduction = 1
    inBibliography = 0
    bibliography = ''
    introduction = ''
    term = ''
    for line in data:
        stripped = line.strip()
        if inIntroduction:
            if line.startswith('-----'):
                inIntroduction = 0
            else:
                introduction += line
        elif inBibliography:
            bibliography += line
        else:
            if line.startswith('-----'):
                inBibliography = 1
            else:
                # collecting an entry
                if stripped == '':
                    continue
                elif stripped.startswith(':'):
                    # start of new term
                    if term != '':
                        lexicon[term] = (description, pattern)
                    offset = stripped.find(':', 1)
                    term = stripped[1:offset]
                    description = stripped[offset + 1:].lstrip()
                    #crud, term, description = stripped.split(':')
                    pattern = ''
                elif stripped.startswith('.') or stripped.startswith('*'):
                    pattern += stripped + '\n'
                else:
                    if line.startswith('    '):
                        description += '\n\n' + stripped
                    else:
                        description += '\n' + stripped
    if term != '':
        lexicon[term] = (description, pattern)
    lexicon['INTRODUCTION'] = (introduction, '')
    lexicon['BIBLIOGRAPHY'] = (bibliography, '')
    #print len(lexicon)
    return lexicon

class Lexicon(model.Background):

    def on_initialize(self, event):
        #self.initSizers()
        self.components.fldDescription.lineNumbersVisible = False
        self.components.fldDescription.setEditorStyle('text')
        self.lexiconPath = self.getParent().lexiconPath
        self.populatePatternsList()

    def populatePatternsList(self):
        self.lexicon = readLexiconFile(self.lexiconPath)
        if self.lexicon is None:
            self.getParent().menuBar.setEnabled('menuAutomataLexicon', False)
            self.visible = False
        else:
            items = self.lexicon.keys()
            items = util.caseinsensitive_sort(items)
            self.components.lstPatterns.items = items
            self.components.lstPatterns.selection = 0
            # now simulate the user doing a selection
            self.on_lstPatterns_select(None)
            self.getParent().menuBar.setEnabled('menuAutomataLexicon', True)
            self.visible = True

    def loadPattern(self, name):
        #filename = name + '.lif'
        #path = os.path.join(self.application.applicationDirectory, 'patterns', filename)
        description, patternString = self.lexicon[name]
        if patternString != '':
            crud, patterns, topLeft, size = translateClipboardPattern(patternString)
            #print "topLeft:", topLeft, "size", size
            self.getParent().initAndPlacePatterns(patterns, topLeft, size)

    def on_lstPatterns_select(self, event):
        name = self.components.lstPatterns.stringSelection
        description, patternString = self.lexicon[name]
        if patternString == '':
            self.components.fldDescription.text = description
            self.components.stcSize.text = ""
        else:
            crud, patterns, topLeft, size = translateClipboardPattern(patternString)
            self.components.fldDescription.text = description + "\n\n" + patternString
            self.components.stcSize.text = "Size: (%d, %d)" % size

    def on_btnLoad_mouseClick(self, event):
        self.loadPattern(self.components.lstPatterns.stringSelection)

    def on_lstPatterns_mouseDoubleClick(self, event):
        self.loadPattern(self.components.lstPatterns.stringSelection)

    def on_close(self, event):
        self.visible = False