Changeset 150

Show
Ignore:
Timestamp:
04/20/08 21:10:28 (6 months ago)
Author:
njriley
Message:

standardized/date-stamped logging; info message; better reset handling; workaround for destruct failure; more PEP 8 compliance

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sucrose/trunk/python/keypad_tk.py

    r143 r150  
    22 
    33import Tkinter 
    4 import sys 
    5 import os 
     4import logging, os, sys 
    65 
    76from api import user_interface, authenticator 
     
    1110A Tkinter frontend for selecting an item in a classic keypad manner 
    1211""" 
     12 
     13# XXX change to use SysLogHandler? 
     14logging.basicConfig(level=logging.DEBUG, 
     15                                        format='%(asctime)s %(levelname)-8s %(message)s', 
     16                                        datefmt='%F %T') 
    1317 
    1418class TkinterUI(user_interface.UserInterface): 
     
    3741 
    3842                self.window = Tkinter.Tk() 
     43                self.window.title('Sucrose') 
     44                self.window.geometry('800x600+0+0') 
    3945                if __name__ != '__main__': 
    4046                        cursor_file = os.path.join(os.path.dirname(__file__), 
    4147                                                                           'emptycursor.xbm') 
    4248                        self.window.config(cursor='@%s white' % cursor_file) 
    43                 self.window.title('Sucrose') 
    44                 self.window.geometry('800x600+0+0') 
    45                 self.window.resizable(False, False) 
     49                        self.window.resizable(False, False) 
     50                        self.window.after(10, lambda: 
     51                                                          self.window.event_generate('<<Motion>>', 
     52                                                                                                             warp=True, x=0, y=0)) 
    4653                self.window.focus_set() 
    4754 
     
    6976                                           font=('helvetica', 24, 'bold'), command=self.destroy).\ 
    7077                                           grid(row=0, column=1, sticky=Tkinter.E) 
     78 
     79                Tkinter.Label(self.window, text='Vending error? Suggestions? ' 
     80                                          'Email sucrose@acm.uiuc.edu', 
     81                                          font=('helvetica', 12, 'bold')).\ 
     82                                          grid(row=2, column=0, columnspan=2, sticky=Tkinter.NSEW) 
     83 
     84                logging.info('%s - Authenticated' % self.auth) 
    7185                self.window.mainloop() 
    7286 
     
    98112                self.window.destroy() 
    99113                del self.window 
     114                # XXX we sometimes don't exit here, can't reproduce it though 
    100115 
    101116 
     
    121136 
    122137                self.sucrose = sucrose 
    123                 self.distruct = exit 
     138                self.destruct = exit 
    124139                self.location = location 
    125140                self.tray = None 
     
    191206                self.update_f = update_f 
    192207                self.sucrose = sucrose 
    193                 self.distruct = exit 
     208                self.destruct = exit 
    194209                self.location = location 
    195210 
     
    249264                return lambda: self.button_callback(number) 
    250265 
    251         def report_exception(self, e): 
     266        def report_exception(self, e=''): 
     267                if e and not isinstance(e, ValueError): 
     268                        logging.exception('Unexpected exception') 
    252269                self.location.config(text=self.item_location) 
    253270                self.name.config(text=str(e), fg='red') 
     
    260277 
    261278                        tray = self.sucrose.tray_from_location(self.item_location) 
    262  
    263                 except ValueError, e: 
     279                except Exception, e: 
    264280                        self.report_exception(e) 
    265281                        tray = None 
    266                 except Exception, e: 
    267                         import traceback; traceback.print_exc() 
    268                         self.report_exception(e) 
    269                         tray = None 
    270282 
    271283                self.update_f(tray) 
    272284 
    273285        def reset(self): 
    274                 self.item_location="00" 
    275                 self.location.config(text=self.item_location) 
    276                 self.name.config(text="") 
    277                 self.vend.config(state=Tkinter.DISABLED) 
    278  
     286                self.item_location = '00' 
     287                self.report_exception() 
     288 
     289                self.update_f(None) 
    279290 
    280291        def vend(self): # Vend Sucrose, and exit 
    281292                self.name.config(text='Vending: %s' % self.item_name()) 
     293                logging.info('%s - Vending %s' % (self.auth, self.item.name)) 
    282294                try: 
    283295                        self.sucrose.vend(self.tray,self.auth.uin) 
     296                        self.auth = None # make sure we can't use auth again 
    284297                except Exception, e: 
    285                         import traceback; traceback.print_exc(
     298                        logging.exception('Failed to vend'
    286299                        self.name.config(text=str(e), fg='red') 
    287300                        return 
    288301 
    289                 self.distruct() 
     302                self.destruct() 
    290303 
    291304 
     
    297310                self.update_f = update_f 
    298311                self.sucrose = sucrose 
    299                 self.distruct = exit 
     312                self.destruct = exit 
    300313                self.location = location 
    301314                self.item = None 
     
    317330                for y in range(0,4): 
    318331                        for x in range(0,3): 
    319                                 i=3*y+x+1 
     332                                i = 3*y+x+1 
    320333                                if i >= 10: 
    321334                                    continue 
     
    330343 
    331344                # Special objects 
    332                 reset=Tkinter.Button(self, text="Reset", command=self.reset, 
    333                                                          font=("helvetica", 24)) 
     345                reset = Tkinter.Button(self, text="Reset", command=self.reset, 
     346                                                           font=("helvetica", 24)) 
    334347                reset.grid(row=4,column=2,columnspan=2,sticky=Tkinter.NSEW) 
    335348 
    336                 self.vend=Tkinter.Button(self, text="Vend", command=self.vend, 
    337                                                                  state=Tkinter.DISABLED, 
    338                                                                  font=("helvetica", 24, 'bold')) 
     349                self.vend = Tkinter.Button(self, text="Vend", command=self.vend, 
     350                                                                   state=Tkinter.DISABLED, 
     351                                                                   font=("helvetica", 24, 'bold')) 
    339352                self.vend.grid(row=4,column=4,columnspan=2,sticky=Tkinter.NSEW) 
    340353 
     
    345358if __name__ == "__main__": 
    346359        from api import sucrose 
    347         from dummy import Cardswipe 
     360        from dummy import DummyAuthenticator 
    348361 
    349362        s = sucrose.Sucrose(db, dbonly=True) 
    350 #       import pdb 
    351 #       pdb.set_trace() 
    352  
    353         TkinterUI(s, Cardswipe(s)) 
     363 
     364        TkinterUI(s, DummyAuthenticator(s))