| 1 | """Document class.""" |
| 2 | |
| 3 | __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>" |
| 4 | __cvsid__ = "$Id$" |
| 5 | __revision__ = "$Revision$"[11:-2] |
| 6 | |
| 7 | import os |
| 8 | |
| 9 | |
| 10 | class Document: |
| 11 | """Document class.""" |
| 12 | |
| 13 | def __init__(self, filename=None): |
| 14 | """Create a Document instance.""" |
| 15 | self.filename = filename |
| 16 | self.filepath = None |
| 17 | self.filedir = None |
| 18 | self.filebase = None |
| 19 | self.fileext = None |
| 20 | if self.filename: |
| 21 | self.filepath = os.path.realpath(self.filename) |
| 22 | self.filedir, self.filename = os.path.split(self.filepath) |
| 23 | self.filebase, self.fileext = os.path.splitext(self.filename) |
| 24 | |
| 25 | def read(self): |
| 26 | """Return contents of file.""" |
| 27 | if self.filepath and os.path.exists(self.filepath): |
| 28 | f = file(self.filepath, 'rb') |
| 29 | try: |
| 30 | return f.read() |
| 31 | finally: |
| 32 | f.close() |
| 33 | else: |
| 34 | return '' |
| 35 | |
| 36 | def write(self, text): |
| 37 | """Write text to file.""" |
| 38 | try: |
| 39 | f = file(self.filepath, 'wb') |
| 40 | f.write(text) |
| 41 | finally: |
| 42 | if f: |
| 43 | f.close() |