def _open(self, filename): """ _open(filename) -> Load the specified file. """ self._aac_file = open(filename, 'rb', buffering=0) self._length = os_getsize(filename.encode('utf-8', 'surrogateescape')) # Read the first 4 bytes from the file, so we can get the # channel count, depth, and sample rate of the file. data_size = 4 data = self._aac_file.read(data_size) # Make init_size a ctypes c_ulong. data_size = _neaacdec.c_ulong(data_size) # Cast the data to a C pointer to use in the decoder. data_p = bytes_to_pointer(_neaacdec.c_ubyte, data) # Seek back to the start. self._aac_file.seek(0) aac_decoder = AACDecoder(data_p, data_size) self._channels = aac_decoder.channels self._rate = aac_decoder.rate self._depth = aac_decoder.depth self._closed = False return aac_decoder
def _open(self, filename): """ _open(filename) -> Load the specified file. """ self._aac_file = open(filename, 'rb', buffering=0) self._length = os_getsize(filename.encode('utf-8', 'surrogateescape')) # Read the first 4 bytes from the file, so we can get the # channel count, depth, and sample rate of the file. data_size = 4 data = self._aac_file.read(data_size) # Make init_size a ctypes c_ulong. data_size = _neaacdec.c_ulong(data_size) # Cast the data to a C pointer to use in the decoder. data = _neaacdec.cast(data, _neaacdec.POINTER(_neaacdec.c_ubyte)) # Seek back to the start. self._aac_file.seek(0) aac_decoder = AACDecoder(data, data_size) self._channels = aac_decoder.channels self._rate = aac_decoder.rate self._depth = aac_decoder.depth self._closed = False return aac_decoder
def __init__(self, filename, mode='r', sentence_endings='.!?', **kwargs): """ TextFile(filename, mode='r', sentence_endings='.!?') -> Just a regular file object. """ super(TextFile, self).__init__(filename, mode) self._rate = 0 # Only read one line/sentence at a time. self._buffer_size = 1 # Open the file with the correct mode. self._fileobj = open(filename, mode) # Memory mapped file for quick searching. mmapobj = mmap(self._fileobj.fileno(), 0, access=ACCESS_READ) # Regex to match sentences. sentence_endings = '.*?[%s]' % re.escape(sentence_endings) re_flags = re.DOTALL | re.IGNORECASE | re.MULTILINE # Compile the sentence regex. sentence_regex = re.compile(sentence_endings.encode(), re_flags) # List of sentences in file sentence_list = sentence_regex.findall(mmapobj) # No need for the mmap to be open anymore mmapobj.close() # Check for sentence endings. if 'r' in mode and sentence_list: self._line_list = [sentence.decode() for sentence in sentence_list] elif 'r' in mode: self._line_list = self._fileobj.readlines() else: self._line_list = [] if self._line_list: # The length is the length of the line list. self._length = len(self._line_list) else: self._length = os_getsize(filename) # Current index. self._index = 0 self._closed = False
def grabFinished(self, data=""): try: self.request.setHeader('Content-Disposition', 'inline; filename=screenshot.%s;' % self.fileformat) self.request.setHeader('Content-Type','image/%s' % self.fileformat) self.request.setHeader('Content-Length', '%i' % os_getsize(self.filepath)) file = open(self.filepath) self.request.write(file.read()) file.close() self.request.finish() except Exception, error: self.request.setResponseCode(http.OK) self.request.write("Error creating screenshot:\n %s" % error) self.request.finish()
def __init__(self, filename: str, mode: str = 'r', depth: int = 16, rate: int = 44100, channels: int = 2, **_): """Initialize the playback settings of the player.""" super(RawFile, self).__init__(filename, mode, depth, rate, channels) if 'r' in mode: self._length = os_getsize(filename) self._file = self._open(filename) self.seek = self._file.seek self.tell = self._file.tell
def __init__(self, filename, mode='r', depth=16, rate=44100, channels=2, **kwargs): """ RawFile(filename, mode='r', depth=16, rate=44100, channels=2) -> Initialize the playback settings of the player. """ super(RawFile, self).__init__(filename, mode, depth, rate, channels) if 'r' in mode: self._length = os_getsize(filename) self._file = self._open(filename) self.seek = self._file.seek self.tell = self._file.tell self.write = self._file.write
def grabFinished(self, data=""): try: self.request.setHeader( 'Content-Disposition', 'inline; filename=screenshot.%s;' % self.fileformat) self.request.setHeader('Content-Type', 'image/%s' % self.fileformat) self.request.setHeader('Content-Length', '%i' % os_getsize(self.filepath)) file = open(self.filepath) self.request.write(file.read()) file.close() self.request.finish() except Exception, error: self.request.setResponseCode(http.OK) self.request.write("Error creating screenshot:\n %s" % error) self.request.finish()