fileimpl.py
5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
###############################################################################
# Name: Cody Precord #
# Purpose: File Object Interface Implementation #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2009 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
Editra Business Model Library: FileObjectImpl
Implementation of a file object interface class. Objects and methods inside
of this library expect a file object that derives from this interface.
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: fileimpl.py 70309 2012-01-10 00:09:26Z CJP $"
__revision__ = "$Revision: 70309 $"
#--------------------------------------------------------------------------#
# Imports
import os
import sys
# Editra Business Model Imports
import txtutil
import fileutil
#--------------------------------------------------------------------------#
class FileObjectImpl(object):
"""File Object Interface implementation base class"""
def __init__(self, path=u'', modtime=0):
super(FileObjectImpl, self).__init__()
# Attributes
self._path = fileutil.GetPathFromURI(path)
self._modtime = modtime
self._handle = None
self.open = False
self.last_err = None
# Properties
Path = property(lambda self: self.GetPath(),
lambda self, path: self.SetPath(path))
Handle = property(lambda self: self._handle)
ModTime = property(lambda self: self.GetModTime(),
lambda self, tstamp: self.SetModTime(tstamp))
ReadOnly = property(lambda self: self.IsReadOnly())
def ClearLastError(self):
"""Reset the error marker on this file"""
del self.last_err
self.last_err = None
def Clone(self):
"""Clone the file object
@return: FileObject
"""
fileobj = FileObjectImpl(self._path, self._modtime)
fileobj.SetLastError(self.last_err)
return fileobj
def Close(self):
"""Close the file handle
@note: this is normally done automatically after a read/write operation
"""
try:
self._handle.close()
except:
pass
self.open = False
def DoOpen(self, mode):
"""Opens and creates the internal file object
@param mode: mode to open file in
@return: True if opened, False if not
@postcondition: self._handle is set to the open handle
"""
if not len(self._path):
return False
try:
file_h = open(self._path, mode)
except (IOError, OSError), msg:
self.SetLastError(unicode(msg))
return False
else:
self._handle = file_h
self.open = True
return True
def Exists(self):
"""Does the file exist on disk?
@return: bool
"""
if self._path:
return fileutil.PathExists(self._path)
else:
return False
def GetExtension(self):
"""Get the files extension if it has one else simply return the
filename minus the path.
@return: string file extension (no dot)
"""
fname = os.path.split(self._path)
return fname[-1].split(os.extsep)[-1].lower()
def GetHandle(self):
"""Get this files handle"""
return self._handle
def GetLastError(self):
"""Return the last error that occurred when using this file
@return: err traceback or None
"""
errstr = u"None"
if self.last_err:
if not txtutil.IsUnicode(self.last_err):
errstr = unicode(self.last_err)
else:
errstr = self.last_err
return errstr
def GetModTime(self):
"""Get the timestamp of this files last modification"""
return self._modtime
def GetPath(self):
"""Get the path of the file
@return: string
"""
return self._path
def GetSize(self):
"""Get the size of the file
@return: int
"""
if self._path:
return fileutil.GetFileSize(self._path)
else:
return 0
def IsOpen(self):
"""Check if file is open or not
@return: bool
"""
return self.open
def IsReadOnly(self):
"""Is the file Read Only
@return: bool
"""
if os.path.exists(self._path):
return not os.access(self._path, os.R_OK|os.W_OK)
else:
return False
def ResetAll(self):
"""Reset all file attributes"""
self._handle = None
self.open = False
self._path = u''
self._modtime = 0
self.last_err = None
def SetLastError(self, err):
"""Set the last error
@param err: exception object / msg
"""
self.last_err = err
def SetPath(self, path):
"""Set the path of the file
@param path: absolute path to file
"""
self._path = fileutil.GetPathFromURI(path)
def SetModTime(self, mtime):
"""Set the modtime of this file
@param mtime: long int to set modtime to
"""
self._modtime = mtime
#--- SHould be overridden by subclass ---#
def Read(self):
"""Open/Read the file
@return: string (file contents)
"""
txt = u''
if self.DoOpen('rb'):
try:
txt = self._handle.read()
except:
pass
return txt
def Write(self, value):
"""Open/Write the value to disk
@param value: string
"""
if self.DoOpen('wb'):
self._handle.write(value)
self._handle.close()