Commits

kenko committed dd47e49

don't mutate Answer objects

Comments (0)

Files changed (4)

pyline/answers.py

         return chosen # trivial conversion
     def convert(self, chosen, entered):
         res = self.do_convert(chosen, entered)
-        self.result = self.validate(res)
+        return self.validate(res)
 
 class Boundable(Answer):
     def __init__(self, below=None, above=None):
             answer = self.get_response(q)
             answer = q.answer_or_default(answer)
             try:
-                q.convert(answer)
+                ans = q.convert(answer)
             except BadAnswer, e:
                 handler = utils.get_by_class(e, q.responses)
                 if not handler:
                 if q.confirm is True:
                     confirmprompt = "Are you sure?  "
                 else:
-                    confirmprompt = q.confirm(q.answer.result)
+                    confirmprompt = q.confirm(ans)
                 if not self.agree(prompt=confirmprompt):
                     q.explain_error(self)
                     continue
-            return q.answer.result
+            return ans
+#            return q.answer.result
 
     def get_response(self, q):
         if q.first_answer:
 
     def get_line(self, q=None):
         ## uncertain about python's readline module: ignore it for now.
-        line = self.inp.readline()
+        if self.inp == sys.stdin:
+            line = raw_input()
+        else:
+            line = self.inp.readline()
         if not line: raise EOFError
         return line
 

pyline/question.py

     def convert(self, ans):
         cands = self.answer.get_candidates()
         if cands == None:
+            return self.answer.convert(ans, ans)
             self.answer.convert(ans, ans)
         else:
             choice = self.winnow(cands, ans)
+            return self.answer.convert(choice, ans)
             self.answer.convert(choice, ans)
 
     def answer_or_default(self, ans):

test/test_menu.py

     out = sio()
     p = pyline.pyline.PyLine(inp=inp,out=out)
     items = [pyline.menu.MenuChoice(*t) for t in (
-        ("double", lambda k,rest:rest+' '+rest),
-        ("reverse", lambda k, rest: rest[::-1]),
-        ("ls", lambda k, rest: os.listdir(os.path.split(__file__)[0])),
+        ("double", lambda choice, menu, rest:rest+' '+rest),
+        ("reverse", lambda choice, menu, rest: rest[::-1]),
+        ("ls", lambda choice, menu, rest: os.listdir(os.path.split(__file__)[0])),
         )]
     m = pyline.menu.Menu(items, shell=True, header="look at these lovely choices", prompt="what to do> ")
     res = p.choose(m)