1# Copyright (c) 2004 Divmod.
2# See LICENSE for details.
3
4import formless
5from zope.interface import implements
6
7class IBar(formless.TypedInterface):
8    bar = formless.String()
9
10
11class Bar:
12    implements(IBar)
13
14    def __init__(self, bar):
15        self.bar = bar
16
17    def __str__(self):
18        return "A Bar: %s" % self.bar
19
20
21class IFrob(formless.TypedInterface):
22    integer = formless.Integer()
23
24
25class Frob:
26    implements(IFrob)
27
28    def __init__(self, integer):
29        self.integer = integer
30
31    def frobazz(self, other):
32        return Frob(self.integer ** other.integer)
33
34    def __str__(self):
35        return "A frob of value %s" % self.integer
36
37
38class IObjectTest(formless.TypedInterface):
39    def someMethod(one=formless.Object(interface=IBar), two=formless.Integer(description="an integer please")):
40        """Some Method.
41
42        This method takes an IBar instance.
43        """
44        return None
45    someMethod = formless.autocallable(someMethod)
46
47    def frobber(frobber=formless.Object(interface=IFrob), frobee=formless.Object(IFrob)):
48        """Frobber.
49
50        Takes two frobs and raises one to the power of the other.
51        """
52        return IFrob
53    frobber = formless.autocallable(frobber)
54
55    someList = formless.List()
56
57
58class ObjectTester:
59    implements(IObjectTest)
60
61    def __init__(self):
62        self.someList = [
63            Bar("boop"), Bar("bap"),
64            Frob(5), Frob(9), Frob(23), Frob(1234)
65        ]
66
67    def someMethod(self, one, two):
68        print "ONE TWO", `one`, `two`
69
70    def frobber(self, frobber, frobee):
71        return frobber.frobazz(frobee)
72
73
74class CompoundChecker(formless.Compound):
75    def coerce(self, data):
76        one, two = data
77        if (one, two) != (6, 9):
78            raise formless.InputError("What do you get when you multiply six by nine?")
79
80
81class IAnotherTest(formless.TypedInterface):
82    def aBarMethod(abar=formless.Object(interface=IBar)):
83        """A Bar Method
84
85        This method takes a bar, but there are no bar instances on this page.
86        You'll have to use the shelf.
87        """
88        return str
89    aBarMethod = formless.autocallable(aBarMethod)
90
91    def aFrobMethod(aFrob=formless.Object(interface=IFrob)):
92        """A Frob Method
93
94        This method takes a frob, but there are no frob instances on this page.
95        You'll have to use the shelf.
96        """
97        return str
98    aFrobMethod = formless.autocallable(aFrobMethod)
99
100    def whatIsMyClass(anObj=formless.Object()):
101        """What is my class?
102
103        Pass an object and get back the class in your hand.
104        """
105        return formless.Object()
106    whatIsMyClass = formless.autocallable(whatIsMyClass)
107
108    def setBreakpoint(breakpoint=formless.String()):
109        """Set a breakpoint
110
111        Set a breakpoint at the given filename and line number. String passed is equivalent
112        to doing b(reak) ([file:]lineno | function) in pdb.
113        """
114        return None
115    setBreakpoint = formless.autocallable(setBreakpoint)
116
117    breakpoints = formless.List()
118
119    def compoundTest(
120        aCompound = formless.Compound(
121            [formless.String(label="firstname"), formless.String(label="lastname")],
122            label="Full Name"),
123        anInt = formless.Integer()):
124        """Compound Test
125
126        A test of a widget/controller which renders multiple fields, triggers multiple
127        validators, but gathers the result into one method argument. There can
128        be an additional validation step which validates that the compound data
129        as a whole is valid.
130        """
131        return str
132    compoundTest = formless.autocallable(compoundTest)
133
134    def compoundChecker(
135        theAnswer = CompoundChecker(
136            [formless.Integer(label="six"), formless.Integer(label="nine")],
137            label="The Answer",
138            description="What is the meaning of life, the universe, and everything?")
139        ):
140        """The Answer
141
142        Please type the integer six in the first box, and nine in the second.
143        """
144        return formless.Object(label="The Answer", interface=formless.Integer)
145    compoundChecker = formless.autocallable(compoundChecker)
146
147
148class AnotherTest:
149    implements(IAnotherTest)
150
151    def aBarMethod(self, abar):
152        return "You passed me %s" % abar
153
154    def aFrobMethod(self, aFrob):
155        return "You passed me %s" % aFrob
156
157    def whatIsMyClass(self, anObj):
158        if hasattr(anObj, '__class__'):
159            return anObj.__class__
160        return type(anObj)
161
162    def _getDebugger(self):
163        import sys, pdb
164        debugInstance = sys.modules.get('debugInstance')
165        if debugInstance is None:
166            sys.modules['debugInstance'] = debugInstance = pdb.Pdb()
167            debugInstance.reset()
168        return debugInstance
169
170    def setBreakpoint(self, breakpoint):
171        import sys
172        debugInstance = self._getDebugger()
173        debugInstance.do_break(debugInstance.precmd(breakpoint))
174        debugInstance.quitting = True
175        sys.settrace(debugInstance.trace_dispatch)
176        debugInstance.quitting = False
177
178    def _currentBreakpoints(self):
179        debugInstance = self._getDebugger()
180        class BreakpointRemover(list):
181            def remove(self, removal):
182                debugInstance.breaks[removal.fn].remove(removal.ln)
183                if not debugInstance.breaks[removal.fn]:
184                    del debugInstance.breaks[removal.fn]
185                list.remove(self, removal)
186        class Dummy(formless.TypedInterface): pass
187        class BP:
188            implements(Dummy)
189            def __init__(self, fn, ln):
190                self.fn=fn
191                self.ln=ln
192            def __str__(self):
193                return "Breakpoint in file %s at line %s" % (self.fn, self.ln)
194
195        breakpoints = BreakpointRemover()
196        for fn in debugInstance.breaks.keys():
197            for lineno in debugInstance.breaks[fn]:
198                breakpoints.append(BP(fn, lineno))
199        return breakpoints
200    breakpoints = property(_currentBreakpoints)
201
202    def compoundTest(self, aCompound, anInt):
203        return "COMPOUND! %s %s" % (aCompound, anInt)
204
205    def compoundChecker(self, theAnswer):
206        return 42
207
208