source: trunk/essentials/dev-lang/python/Lib/posixfile.py@ 3393

Last change on this file since 3393 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 7.7 KB
Line 
1"""Extended file operations available in POSIX.
2
3f = posixfile.open(filename, [mode, [bufsize]])
4 will create a new posixfile object
5
6f = posixfile.fileopen(fileobject)
7 will create a posixfile object from a builtin file object
8
9f.file()
10 will return the original builtin file object
11
12f.dup()
13 will return a new file object based on a new filedescriptor
14
15f.dup2(fd)
16 will return a new file object based on the given filedescriptor
17
18f.flags(mode)
19 will turn on the associated flag (merge)
20 mode can contain the following characters:
21
22 (character representing a flag)
23 a append only flag
24 c close on exec flag
25 n no delay flag
26 s synchronization flag
27 (modifiers)
28 ! turn flags 'off' instead of default 'on'
29 = copy flags 'as is' instead of default 'merge'
30 ? return a string in which the characters represent the flags
31 that are set
32
33 note: - the '!' and '=' modifiers are mutually exclusive.
34 - the '?' modifier will return the status of the flags after they
35 have been changed by other characters in the mode string
36
37f.lock(mode [, len [, start [, whence]]])
38 will (un)lock a region
39 mode can contain the following characters:
40
41 (character representing type of lock)
42 u unlock
43 r read lock
44 w write lock
45 (modifiers)
46 | wait until the lock can be granted
47 ? return the first lock conflicting with the requested lock
48 or 'None' if there is no conflict. The lock returned is in the
49 format (mode, len, start, whence, pid) where mode is a
50 character representing the type of lock ('r' or 'w')
51
52 note: - the '?' modifier prevents a region from being locked; it is
53 query only
54"""
55
56
57class _posixfile_:
58 """File wrapper class that provides extra POSIX file routines."""
59
60 states = ['open', 'closed']
61
62 #
63 # Internal routines
64 #
65 def __repr__(self):
66 file = self._file_
67 return "<%s posixfile '%s', mode '%s' at %s>" % \
68 (self.states[file.closed], file.name, file.mode, \
69 hex(id(self))[2:])
70
71 #
72 # Initialization routines
73 #
74 def open(self, name, mode='r', bufsize=-1):
75 import __builtin__
76 return self.fileopen(__builtin__.open(name, mode, bufsize))
77
78 def fileopen(self, file):
79 import types
80 if repr(type(file)) != "<type 'file'>":
81 raise TypeError, 'posixfile.fileopen() arg must be file object'
82 self._file_ = file
83 # Copy basic file methods
84 for maybemethod in dir(file):
85 if not maybemethod.startswith('_'):
86 attr = getattr(file, maybemethod)
87 if isinstance(attr, types.BuiltinMethodType):
88 setattr(self, maybemethod, attr)
89 return self
90
91 #
92 # New methods
93 #
94 def file(self):
95 return self._file_
96
97 def dup(self):
98 import posix
99
100 if not hasattr(posix, 'fdopen'):
101 raise AttributeError, 'dup() method unavailable'
102
103 return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
104