Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Unnamed
Friday, July 28th, 2006 at 6:52:53am MDT 

  1. #
  2.  
  3. # interactive_console.py
  4.  
  5. #
  6.  
  7. # An implementation of a trivial interactive Python console for
  8.  
  9. # Series 60 Python environment. Based on 'code' module and
  10.  
  11. # 'series60_console'.
  12.  
  13. #     
  14.  
  15. # Copyright (c) 2005 Nokia Corporation
  16.  
  17. #
  18.  
  19. # Licensed under the Apache License, Version 2.0 (the "License");
  20.  
  21. # you may not use this file except in compliance with the License.
  22.  
  23. # You may obtain a copy of the License at
  24.  
  25. #
  26.  
  27. #     http://www.apache.org/licenses/LICENSE-2.0
  28.  
  29. #
  30.  
  31. # Unless required by applicable law or agreed to in writing, software
  32.  
  33. # distributed under the License is distributed on an "AS IS" BASIS,
  34.  
  35. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  36.  
  37. # See the License for the specific language governing permissions and
  38.  
  39. # limitations under the License.
  40.  
  41. #
  42.  
  43.  
  44.  
  45. import appuifw
  46.  
  47. from key_codes import EKeyDevice3
  48.  
  49.  
  50.  
  51. class Py_console:
  52.  
  53.     def __init__(self, console):
  54.  
  55.         self.co = console
  56.  
  57.         self.co.control.bind(EKeyDevice3, self.handle_ok_press)
  58.  
  59.         self.history = ['','','','','']
  60.  
  61.         self.history_item = 4
  62.  
  63.  
  64.  
  65.     def handle_ok_press(self):
  66.  
  67.         self.co.write(u'\n')
  68.  
  69.         self.co.input_wait_lock.signal()
  70.  
  71.  
  72.  
  73.     def readfunc(self, prompt):
  74.  
  75.         self.co.write(unicode(prompt))
  76.  
  77.         user_input = self.co.readline()
  78.  
  79.         self.history.append(user_input)
  80.  
  81.         self.history.pop(0)
  82.  
  83.         self.history_item = 4
  84.  
  85.         return user_input
  86.        
  87.     def clear_screen(self):
  88.         self.co.clear()
  89.         self.co.input_wait_lock.signal()
  90.         self.readfunc(sys.ps1)
  91.  
  92.  
  93.  
  94.     def previous_input(self):
  95.  
  96.         if self.history_item == -1:
  97.  
  98.             return
  99.  
  100.         elif self.history_item == 4:
  101.  
  102.             self.line_beg = self.co.control.len()
  103.  
  104.         else:
  105.  
  106.             self.co.control.delete(self.line_beg,
  107.  
  108.                                    self.line_beg+\
  109.  
  110.                                    len(self.history[self.history_item]))
  111.  
  112.         self.co.control.set_pos(self.line_beg)
  113.  
  114.         self.co.write(self.history[self.history_item])
  115.  
  116.         self.history_item -= 1
  117.  
  118.  
  119.  
  120.     def interactive_loop(self, scope = locals()):
  121.  
  122.         import code
  123.  
  124.         appuifw.app.menu.append((u"Clear screen", self.co.clear_screen))
  125.         appuifw.app.menu.append((u"Previous command", self.previous_input))
  126.         self.co.clear()
  127.  
  128.         code.interact(None, self.readfunc, scope)
  129.  
  130.         self.co.control.bind(EKeyDevice3, None)
  131.  
  132.         self.history = []
  133.  
  134.         appuifw.app.menu = []
  135.  
  136.  
  137.  
  138. if __name__ == '__main__':
  139.  
  140.     try:
  141.  
  142.         console = my_console
  143.  
  144.     except NameError:
  145.  
  146.         import series60_console
  147.  
  148.         console = series60_console.Console()
  149.  
  150.     appuifw.app.body = console.control
  151.  
  152.     Py_console(console).interactive_loop()

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right