home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-python-addon-1.4.9-installer.exe / tkFileDialog.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-10-01  |  6.3 KB  |  160 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. from tkCommonDialog import Dialog
  5.  
  6. class _Dialog(Dialog):
  7.     
  8.     def _fixoptions(self):
  9.         
  10.         try:
  11.             self.options['filetypes'] = tuple(self.options['filetypes'])
  12.         except KeyError:
  13.             pass
  14.  
  15.  
  16.     
  17.     def _fixresult(self, widget, result):
  18.         if result:
  19.             import os
  20.             
  21.             try:
  22.                 result = result.string
  23.             except AttributeError:
  24.                 pass
  25.  
  26.             (path, file) = os.path.split(result)
  27.             self.options['initialdir'] = path
  28.             self.options['initialfile'] = file
  29.         
  30.         self.filename = result
  31.         return result
  32.  
  33.  
  34.  
  35. class Open(_Dialog):
  36.     '''Ask for a filename to open'''
  37.     command = 'tk_getOpenFile'
  38.     
  39.     def _fixresult(self, widget, result):
  40.         if isinstance(result, tuple):
  41.             result = []([ getattr(r, 'string', r) for r in result ])
  42.             return result
  43.         
  44.         if not widget.tk.wantobjects() and 'multiple' in self.options:
  45.             return self._fixresult(widget, widget.tk.splitlist(result))
  46.         
  47.         return _Dialog._fixresult(self, widget, result)
  48.  
  49.  
  50.  
  51. class SaveAs(_Dialog):
  52.     '''Ask for a filename to save as'''
  53.     command = 'tk_getSaveFile'
  54.  
  55.  
  56. class Directory(Dialog):
  57.     '''Ask for a directory'''
  58.     command = 'tk_chooseDirectory'
  59.     
  60.     def _fixresult(self, widget, result):
  61.         if result:
  62.             
  63.             try:
  64.                 result = result.string
  65.             except AttributeError:
  66.                 pass
  67.  
  68.             self.options['initialdir'] = result
  69.         
  70.         self.directory = result
  71.         return result
  72.  
  73.  
  74.  
  75. def askopenfilename(**options):
  76.     '''Ask for a filename to open'''
  77.     return Open(**options).show()
  78.  
  79.  
  80. def asksaveasfilename(**options):
  81.     '''Ask for a filename to save as'''
  82.     return SaveAs(**options).show()
  83.  
  84.  
  85. def askopenfilenames(**options):
  86.     '''Ask for multiple filenames to open
  87.     
  88.     Returns a list of filenames or empty list if 
  89.     cancel button selected
  90.     '''
  91.     options['multiple'] = 1
  92.     return Open(**options).show()
  93.  
  94.  
  95. def askopenfile(mode = 'r', **options):
  96.     '''Ask for a filename to open, and returned the opened file'''
  97.     filename = Open(**options).show()
  98.     if filename:
  99.         return open(filename, mode)
  100.     
  101.     return None
  102.  
  103.  
  104. def askopenfiles(mode = 'r', **options):
  105.     '''Ask for multiple filenames and return the open file
  106.     objects
  107.     
  108.     returns a list of open file objects or an empty list if 
  109.     cancel selected
  110.     '''
  111.     files = askopenfilenames(**options)
  112.     if files:
  113.         ofiles = []
  114.         for filename in files:
  115.             ofiles.append(open(filename, mode))
  116.         
  117.         files = ofiles
  118.     
  119.     return files
  120.  
  121.  
  122. def asksaveasfile(mode = 'w', **options):
  123.     '''Ask for a filename to save as, and returned the opened file'''
  124.     filename = SaveAs(**options).show()
  125.     if filename:
  126.         return open(filename, mode)
  127.     
  128.     return None
  129.  
  130.  
  131. def askdirectory(**options):
  132.     '''Ask for a directory, and return the file name'''
  133.     return Directory(**options).show()
  134.  
  135. if __name__ == '__main__':
  136.     enc = 'utf-8'
  137.     import sys
  138.     
  139.     try:
  140.         import locale
  141.         locale.setlocale(locale.LC_ALL, '')
  142.         enc = locale.nl_langinfo(locale.CODESET)
  143.     except (ImportError, AttributeError):
  144.         pass
  145.  
  146.     openfilename = askopenfilename(filetypes = [
  147.         ('all files', '*')])
  148.     
  149.     try:
  150.         fp = open(openfilename, 'r')
  151.         fp.close()
  152.     except:
  153.         print 'Could not open File: '
  154.         print sys.exc_info()[1]
  155.  
  156.     print 'open', openfilename.encode(enc)
  157.     saveasfilename = asksaveasfilename()
  158.     print 'saveas', saveasfilename.encode(enc)
  159.  
  160.