Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
idlelib.IOBinding.IOBinding Class Reference
Collaboration diagram for idlelib.IOBinding.IOBinding:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def close
def get_saved
def set_saved
def reset_undo
def set_filename_change_hook
def set_filename
def open
def loadfile
def maybesave
def save
def save_as
def save_a_copy
def writefile
def encode
def fixlastline
def print_window
def askopenfile
def defaultfilename
def asksavefile
def updaterecentfileslist

Public Attributes

 editwin
 text
 fileencoding

Static Public Attributes

 filename_change_hook = None
 filename = None
 dirname = None
string eol = r"(\r\n)|\n|\r"
tuple eol_re = re.compile(eol)
 eol_convention = os.linesep
 opendialog = None
 savedialog = None
list filetypes

Private Member Functions

def _decode

Private Attributes

 __id_open
 __id_save
 __id_saveas
 __id_savecopy
 __id_print

Detailed Description

Definition at line 104 of file IOBinding.py.


Constructor & Destructor Documentation

def idlelib.IOBinding.IOBinding.__init__ (   self,
  editwin 
)

Definition at line 106 of file IOBinding.py.

00106 
00107     def __init__(self, editwin):
00108         self.editwin = editwin
00109         self.text = editwin.text
00110         self.__id_open = self.text.bind("<<open-window-from-file>>", self.open)
00111         self.__id_save = self.text.bind("<<save-window>>", self.save)
00112         self.__id_saveas = self.text.bind("<<save-window-as-file>>",
00113                                           self.save_as)
00114         self.__id_savecopy = self.text.bind("<<save-copy-of-window-as-file>>",
00115                                             self.save_a_copy)
00116         self.fileencoding = None
00117         self.__id_print = self.text.bind("<<print-window>>", self.print_window)

Here is the caller graph for this function:


Member Function Documentation

def idlelib.IOBinding.IOBinding._decode (   self,
  two_lines,
  bytes 
) [private]

Definition at line 239 of file IOBinding.py.

00239 
00240     def _decode(self, two_lines, bytes):
00241         "Create a Unicode string."
00242         chars = None
00243         # Check presence of a UTF-8 signature first
00244         if bytes.startswith(BOM_UTF8):
00245             try:
00246                 chars = bytes[3:].decode("utf-8")
00247             except UnicodeDecodeError:
00248                 # has UTF-8 signature, but fails to decode...
00249                 return None, False
00250             else:
00251                 # Indicates that this file originally had a BOM
00252                 self.fileencoding = 'BOM'
00253                 return chars, False
00254         # Next look for coding specification
00255         try:
00256             enc = coding_spec(two_lines)
00257         except LookupError as name:
00258             tkMessageBox.showerror(
00259                 title="Error loading the file",
00260                 message="The encoding '%s' is not known to this Python "\
00261                 "installation. The file may not display correctly" % name,
00262                 master = self.text)
00263             enc = None
00264         except UnicodeDecodeError:
00265             return None, False
00266         if enc:
00267             try:
00268                 chars = str(bytes, enc)
00269                 self.fileencoding = enc
00270                 return chars, False
00271             except UnicodeDecodeError:
00272                 pass
00273         # Try ascii:
00274         try:
00275             chars = str(bytes, 'ascii')
00276             self.fileencoding = None
00277             return chars, False
00278         except UnicodeDecodeError:
00279             pass
00280         # Try utf-8:
00281         try:
00282             chars = str(bytes, 'utf-8')
00283             self.fileencoding = 'utf-8'
00284             return chars, False
00285         except UnicodeDecodeError:
00286             pass
00287         # Finally, try the locale's encoding. This is deprecated;
00288         # the user should declare a non-ASCII encoding
00289         try:
00290             # Wait for the editor window to appear
00291             self.editwin.text.update()
00292             enc = askstring(
00293                 "Specify file encoding",
00294                 "The file's encoding is invalid for Python 3.x.\n"
00295                 "IDLE will convert it to UTF-8.\n"
00296                 "What is the current encoding of the file?",
00297                 initialvalue = locale_encoding,
00298                 parent = self.editwin.text)
00299 
00300             if enc:
00301                 chars = str(bytes, enc)
00302                 self.fileencoding = None
00303             return chars, True
00304         except (UnicodeDecodeError, LookupError):
00305             pass
00306         return None, False  # None on failure

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 484 of file IOBinding.py.

00484 
00485     def askopenfile(self):
00486         dir, base = self.defaultfilename("open")
00487         if not self.opendialog:
00488             self.opendialog = tkFileDialog.Open(master=self.text,
00489                                                 filetypes=self.filetypes)
00490         filename = self.opendialog.show(initialdir=dir, initialfile=base)
00491         return filename

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 504 of file IOBinding.py.

00504 
00505     def asksavefile(self):
00506         dir, base = self.defaultfilename("save")
00507         if not self.savedialog:
00508             self.savedialog = tkFileDialog.SaveAs(master=self.text,
00509                                                   filetypes=self.filetypes)
00510         filename = self.savedialog.show(initialdir=dir, initialfile=base)
00511         return filename

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 118 of file IOBinding.py.

00118 
00119     def close(self):
00120         # Undo command bindings
00121         self.text.unbind("<<open-window-from-file>>", self.__id_open)
00122         self.text.unbind("<<save-window>>", self.__id_save)
00123         self.text.unbind("<<save-window-as-file>>",self.__id_saveas)
00124         self.text.unbind("<<save-copy-of-window-as-file>>", self.__id_savecopy)
00125         self.text.unbind("<<print-window>>", self.__id_print)
00126         # Break cycles
00127         self.editwin = None
00128         self.text = None
00129         self.filename_change_hook = None

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.defaultfilename (   self,
  mode = "open" 
)

Definition at line 492 of file IOBinding.py.

00492 
00493     def defaultfilename(self, mode="open"):
00494         if self.filename:
00495             return os.path.split(self.filename)
00496         elif self.dirname:
00497             return self.dirname, ""
00498         else:
00499             try:
00500                 pwd = os.getcwd()
00501             except os.error:
00502                 pwd = ""
00503             return pwd, ""

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.encode (   self,
  chars 
)

Definition at line 381 of file IOBinding.py.

00381 
00382     def encode(self, chars):
00383         if isinstance(chars, bytes):
00384             # This is either plain ASCII, or Tk was returning mixed-encoding
00385             # text to us. Don't try to guess further.
00386             return chars
00387         # Preserve a BOM that might have been present on opening
00388         if self.fileencoding == 'BOM':
00389             return BOM_UTF8 + chars.encode("utf-8")
00390         # See whether there is anything non-ASCII in it.
00391         # If not, no need to figure out the encoding.
00392         try:
00393             return chars.encode('ascii')
00394         except UnicodeError:
00395             pass
00396         # Check if there is an encoding declared
00397         try:
00398             # a string, let coding_spec slice it to the first two lines
00399             enc = coding_spec(chars)
00400             failed = None
00401         except LookupError as msg:
00402             failed = msg
00403             enc = None
00404         else:
00405             if not enc:
00406                 # PEP 3120: default source encoding is UTF-8
00407                 enc = 'utf-8'
00408         if enc:
00409             try:
00410                 return chars.encode(enc)
00411             except UnicodeError:
00412                 failed = "Invalid encoding '%s'" % enc
00413         tkMessageBox.showerror(
00414             "I/O Error",
00415             "%s.\nSaving as UTF-8" % failed,
00416             master = self.text)
00417         # Fallback: save as UTF-8, with BOM - ignoring the incorrect
00418         # declared encoding
00419         return BOM_UTF8 + chars.encode("utf-8")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 420 of file IOBinding.py.

00420 
00421     def fixlastline(self):
00422         c = self.text.get("end-2c")
00423         if c != '\n':
00424             self.text.insert("end-1c", "\n")

Here is the caller graph for this function:

Definition at line 130 of file IOBinding.py.

00130 
00131     def get_saved(self):
00132         return self.editwin.get_saved()

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.loadfile (   self,
  filename 
)

Definition at line 202 of file IOBinding.py.

00202 
00203     def loadfile(self, filename):
00204         try:
00205             # open the file in binary mode so that we can handle
00206             # end-of-line convention ourselves.
00207             f = open(filename,'rb')
00208             two_lines = f.readline() + f.readline()
00209             f.seek(0)
00210             bytes = f.read()
00211             f.close()
00212         except IOError as msg:
00213             tkMessageBox.showerror("I/O Error", str(msg), master=self.text)
00214             return False
00215         chars, converted = self._decode(two_lines, bytes)
00216         if chars is None:
00217             tkMessageBox.showerror("Decoding Error",
00218                                    "File %s\nFailed to Decode" % filename,
00219                                    parent=self.text)
00220             return False
00221         # We now convert all end-of-lines to '\n's
00222         firsteol = self.eol_re.search(chars)
00223         if firsteol:
00224             self.eol_convention = firsteol.group(0)
00225             chars = self.eol_re.sub(r"\n", chars)
00226         self.text.delete("1.0", "end")
00227         self.set_filename(None)
00228         self.text.insert("1.0", chars)
00229         self.reset_undo()
00230         self.set_filename(filename)
00231         if converted:
00232             # We need to save the conversion results first
00233             # before being able to execute the code
00234             self.set_saved(False)
00235         self.text.mark_set("insert", "1.0")
00236         self.text.yview("insert")
00237         self.updaterecentfileslist(filename)
00238         return True

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 307 of file IOBinding.py.

00307 
00308     def maybesave(self):
00309         if self.get_saved():
00310             return "yes"
00311         message = "Do you want to save %s before closing?" % (
00312             self.filename or "this untitled document")
00313         confirm = tkMessageBox.askyesnocancel(
00314                   title="Save On Close",
00315                   message=message,
00316                   default=tkMessageBox.YES,
00317                   master=self.text)
00318         if confirm:
00319             reply = "yes"
00320             self.save(None)
00321             if not self.get_saved():
00322                 reply = "cancel"
00323         elif confirm is None:
00324             reply = "cancel"
00325         else:
00326             reply = "no"
00327         self.text.focus_set()
00328         return reply

Here is the call graph for this function:

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.open (   self,
  event = None,
  editFile = None 
)

Definition at line 158 of file IOBinding.py.

00158 
00159     def open(self, event=None, editFile=None):
00160         if self.editwin.flist:
00161             if not editFile:
00162                 filename = self.askopenfile()
00163             else:
00164                 filename=editFile
00165             if filename:
00166                 # If the current window has no filename and hasn't been
00167                 # modified, we replace its contents (no loss).  Otherwise
00168                 # we open a new window.  But we won't replace the
00169                 # shell window (which has an interp(reter) attribute), which
00170                 # gets set to "not modified" at every new prompt.
00171                 try:
00172                     interp = self.editwin.interp
00173                 except AttributeError:
00174                     interp = None
00175                 if not self.filename and self.get_saved() and not interp:
00176                     self.editwin.flist.open(filename, self.loadfile)
00177                 else:
00178                     self.editwin.flist.open(filename)
00179             else:
00180                 self.text.focus_set()
00181             return "break"
00182         #
00183         # Code for use outside IDLE:
00184         if self.get_saved():
00185             reply = self.maybesave()
00186             if reply == "cancel":
00187                 self.text.focus_set()
00188                 return "break"
00189         if not editFile:
00190             filename = self.askopenfile()
00191         else:
00192             filename=editFile
00193         if filename:
00194             self.loadfile(filename)
00195         else:
00196             self.text.focus_set()
00197         return "break"

Here is the call graph for this function:

def idlelib.IOBinding.IOBinding.print_window (   self,
  event 
)

Definition at line 425 of file IOBinding.py.

00425 
00426     def print_window(self, event):
00427         confirm = tkMessageBox.askokcancel(
00428                   title="Print",
00429                   message="Print to Default Printer",
00430                   default=tkMessageBox.OK,
00431                   master=self.text)
00432         if not confirm:
00433             self.text.focus_set()
00434             return "break"
00435         tempfilename = None
00436         saved = self.get_saved()
00437         if saved:
00438             filename = self.filename
00439         # shell undo is reset after every prompt, looks saved, probably isn't
00440         if not saved or filename is None:
00441             (tfd, tempfilename) = tempfile.mkstemp(prefix='IDLE_tmp_')
00442             filename = tempfilename
00443             os.close(tfd)
00444             if not self.writefile(tempfilename):
00445                 os.unlink(tempfilename)
00446                 return "break"
00447         platform = os.name
00448         printPlatform = True
00449         if platform == 'posix': #posix platform
00450             command = idleConf.GetOption('main','General',
00451                                          'print-command-posix')
00452             command = command + " 2>&1"
00453         elif platform == 'nt': #win32 platform
00454             command = idleConf.GetOption('main','General','print-command-win')
00455         else: #no printing for this platform
00456             printPlatform = False
00457         if printPlatform:  #we can try to print for this platform
00458             command = command % filename
00459             pipe = os.popen(command, "r")
00460             # things can get ugly on NT if there is no printer available.
00461             output = pipe.read().strip()
00462             status = pipe.close()
00463             if status:
00464                 output = "Printing failed (exit status 0x%x)\n" % \
00465                          status + output
00466             if output:
00467                 output = "Printing command: %s\n" % repr(command) + output
00468                 tkMessageBox.showerror("Print status", output, master=self.text)
00469         else:  #no printing for this platform
00470             message = "Printing is not enabled for this platform: %s" % platform
00471             tkMessageBox.showinfo("Print status", message, master=self.text)
00472         if tempfilename:
00473             os.unlink(tempfilename)
00474         return "break"

Here is the call graph for this function:

Definition at line 136 of file IOBinding.py.

00136 
00137     def reset_undo(self):
00138         self.editwin.reset_undo()

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.save (   self,
  event 
)

Definition at line 329 of file IOBinding.py.

00329 
00330     def save(self, event):
00331         if not self.filename:
00332             self.save_as(event)
00333         else:
00334             if self.writefile(self.filename):
00335                 self.set_saved(True)
00336                 try:
00337                     self.editwin.store_file_breaks()
00338                 except AttributeError:  # may be a PyShell
00339                     pass
00340         self.text.focus_set()
00341         return "break"

Here is the call graph for this function:

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.save_a_copy (   self,
  event 
)

Definition at line 356 of file IOBinding.py.

00356 
00357     def save_a_copy(self, event):
00358         filename = self.asksavefile()
00359         if filename:
00360             self.writefile(filename)
00361         self.text.focus_set()
00362         self.updaterecentfileslist(filename)
00363         return "break"

Here is the call graph for this function:

def idlelib.IOBinding.IOBinding.save_as (   self,
  event 
)

Definition at line 342 of file IOBinding.py.

00342 
00343     def save_as(self, event):
00344         filename = self.asksavefile()
00345         if filename:
00346             if self.writefile(filename):
00347                 self.set_filename(filename)
00348                 self.set_saved(1)
00349                 try:
00350                     self.editwin.store_file_breaks()
00351                 except AttributeError:
00352                     pass
00353         self.text.focus_set()
00354         self.updaterecentfileslist(filename)
00355         return "break"

Here is the call graph for this function:

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.set_filename (   self,
  filename 
)

Definition at line 147 of file IOBinding.py.

00147 
00148     def set_filename(self, filename):
00149         if filename and os.path.isdir(filename):
00150             self.filename = None
00151             self.dirname = filename
00152         else:
00153             self.filename = filename
00154             self.dirname = None
00155             self.set_saved(1)
00156             if self.filename_change_hook:
00157                 self.filename_change_hook()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 141 of file IOBinding.py.

00141 
00142     def set_filename_change_hook(self, hook):
00143         self.filename_change_hook = hook

def idlelib.IOBinding.IOBinding.set_saved (   self,
  flag 
)

Definition at line 133 of file IOBinding.py.

00133 
00134     def set_saved(self, flag):
00135         self.editwin.set_saved(flag)

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.updaterecentfileslist (   self,
  filename 
)

Definition at line 512 of file IOBinding.py.

00512 
00513     def updaterecentfileslist(self,filename):
00514         "Update recent file list on all editor windows"
00515         if self.editwin.flist:
00516             self.editwin.update_recent_files_list(filename)

Here is the caller graph for this function:

def idlelib.IOBinding.IOBinding.writefile (   self,
  filename 
)

Definition at line 364 of file IOBinding.py.

00364 
00365     def writefile(self, filename):
00366         self.fixlastline()
00367         text = self.text.get("1.0", "end-1c")
00368         if self.eol_convention != "\n":
00369             text = text.replace("\n", self.eol_convention)
00370         chars = self.encode(text)
00371         try:
00372             f = open(filename, "wb")
00373             f.write(chars)
00374             f.flush()
00375             f.close()
00376             return True
00377         except IOError as msg:
00378             tkMessageBox.showerror("I/O Error", str(msg),
00379                                    master=self.text)
00380             return False

Here is the caller graph for this function:


Member Data Documentation

Definition at line 109 of file IOBinding.py.

Definition at line 116 of file IOBinding.py.

Definition at line 110 of file IOBinding.py.

Definition at line 111 of file IOBinding.py.

Definition at line 113 of file IOBinding.py.

Definition at line 145 of file IOBinding.py.

Definition at line 107 of file IOBinding.py.

string idlelib.IOBinding.IOBinding.eol = r"(\r\n)|\n|\r" [static]

Definition at line 198 of file IOBinding.py.

Definition at line 200 of file IOBinding.py.

Definition at line 199 of file IOBinding.py.

Definition at line 115 of file IOBinding.py.

Definition at line 144 of file IOBinding.py.

Definition at line 139 of file IOBinding.py.

Initial value:
[
        ("Python files", "*.py *.pyw", "TEXT"),
        ("Text files", "*.txt", "TEXT"),
        ("All files", "*"),
        ]

Definition at line 478 of file IOBinding.py.

Definition at line 475 of file IOBinding.py.

Definition at line 476 of file IOBinding.py.

Definition at line 108 of file IOBinding.py.


The documentation for this class was generated from the following file: