/usr/share/pyshared/circuits/io/file.py is in python-circuits 2.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | # Module: file
# Date: 4th August 2004
# Author: James Mills <prologic@shortcircuit.net.au>
"""File I/O
This module implements a wrapper for basic File I/O.
"""
try:
from os import O_NONBLOCK
except ImportError:
#If it fails, that's fine. the fcntl import
#will fail anyway.
pass
from os import write
from collections import deque
from sys import getdefaultencoding
from socket import error as SocketError
from errno import ENOTCONN, EPIPE, EWOULDBLOCK
from circuits.tools import tryimport
from circuits.core.utils import findcmp
from circuits.six import binary_type, PY3
from circuits.core import handler, Component, Event
from circuits.core.pollers import BasePoller, Poller
if PY3:
from io import FileIO
FileType = FileIO
else:
FileType = file
fcntl = tryimport("fcntl")
TIMEOUT = 0.2
BUFSIZE = 4096
class EOF(Event):
"""EOF Event"""
class Seek(Event):
"""Seek Event"""
class Read(Event):
"""Read Event"""
class Close(Event):
"""Close Event"""
class Write(Event):
"""Write Event"""
class Error(Event):
"""Error Event"""
class Open(Event):
"""Open Event"""
class Opened(Event):
"""Opened Event"""
class Closed(Event):
"""Closed Event"""
class Ready(Event):
"""Ready Event"""
class File(Component):
channel = "file"
def init(self, filename, mode="r", bufsize=BUFSIZE, encoding=None,
channel=channel):
self._mode = mode
self._bufsize = bufsize
self._filename = filename
self._encoding = encoding or getdefaultencoding()
self._fd = None
self._poller = None
self._buffer = deque()
self._closeflag = False
@property
def closed(self):
return getattr(self._fd, "closed", True) \
if hasattr(self, "_fd") else True
@property
def filename(self):
return getattr(self, "_filename", None)
@property
def mode(self):
return getattr(self, "_mode", None)
@handler("ready")
def _on_ready(self, component):
self.fire(Open(), self.channel)
@handler("open")
def _on_open(self, filename=None, mode=None, bufsize=None):
self._filename = filename or self._filename
self._bufsize = bufsize or self._bufsize
self._mode = mode or self._mode
if isinstance(self._filename, FileType):
self._fd = self._filename
self._mode = self._fd.mode
self._filename = self._fd.name
self._encoding = getattr(self._fd, "encoding", self._encoding)
else:
kwargs = {"encoding": self._encoding} if PY3 else {}
self._fd = open(self.filename, self.mode, **kwargs)
if fcntl is not None:
# Set non-blocking file descriptor (non-portable)
flag = fcntl.fcntl(self._fd, fcntl.F_GETFL)
flag = flag | O_NONBLOCK
fcntl.fcntl(self._fd, fcntl.F_SETFL, flag)
if "r" in self.mode or "+" in self.mode:
self._poller.addReader(self, self._fd)
self.fire(Opened(self.filename, self.mode))
@handler("registered", channel="*")
def _on_registered(self, component, manager):
if self._poller is None:
if isinstance(component, BasePoller):
self._poller = component
self.fire(Ready(self))
else:
if component is not self:
return
component = findcmp(self.root, BasePoller)
if component is not None:
self._poller = component
self.fire(Ready(self))
else:
self._poller = Poller().register(self)
self.fire(Ready(self))
@handler("stopped", channel="*")
def _on_stopped(self, component):
self.fire(Close())
@handler("prepare_unregister", channel="*")
def _on_prepare_unregister(self, event, c):
if event.in_subtree(self):
self._close()
def _close(self):
if self.closed:
return
self._poller.discard(self._fd)
self._buffer.clear()
self._closeflag = False
self._connected = False
try:
self._fd.cllse()
except:
pass
self.fire(Closed())
def close(self):
if not self._buffer:
self._close()
elif not self._closeflag:
self._closeflag = True
def _read(self):
try:
data = self._fd.read(self._bufsize)
if not isinstance(data, binary_type):
data = data.encode(self._encoding)
if data:
self.fire(Read(data)).notify = True
else:
self.fire(EOF())
if not any(m in self.mode for m in ("a", "+")):
self.close()
else:
self._poller.discard(self._fd)
except SocketError as e:
if e.args[0] == EWOULDBLOCK:
return
else:
self.fire(Error(e))
self._close()
def seek(self, offset, whence=0):
self._fd.seek(offset, whence)
def _write(self, data):
try:
if not isinstance(data, binary_type):
data = data.encode(self._encoding)
nbytes = write(self._fd.fileno(), data)
if nbytes < len(data):
self._buffer.appendleft(data[nbytes:])
except SocketError as e:
if e.args[0] in (EPIPE, ENOTCONN):
self._close()
else:
self.fire(Error(e))
def write(self, data):
if self._poller is not None and not self._poller.isWriting(self._fd):
self._poller.addWriter(self, self._fd)
self._buffer.append(data)
@handler("_disconnect", filter=True)
def __on_disconnect(self, sock):
self._close()
@handler("_read", filter=True)
def __on_read(self, sock):
self._read()
@handler("_write", filter=True)
def __on_write(self, sock):
if self._buffer:
data = self._buffer.popleft()
self._write(data)
if not self._buffer:
if self._closeflag:
self._close()
elif self._poller.isWriting(self._fd):
self._poller.removeWriter(self._fd)
|