def openfile(interp, this, open_mode='r', use_include_path=False, w_ctx=None): if open_mode == '': raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileInfo::openFile(): Invalid parameters") ])) args = [ interp.space.wrap(this.file_name), interp.space.wrap(open_mode), interp.space.wrap(use_include_path) ] if w_ctx: if not interp.space.is_resource(w_ctx): raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileInfo::openFile() expects " "parameter 3 to be resource, %s given" % interp.space.get_type_name(w_ctx.tp).lower()) ])) args.append(w_ctx) try: file_object = SplFileObjectClass.call_args(interp, args) return file_object except OSError, e: raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileInfo::openFile(): %s" % os.strerror(e.errno)) ]))
def di_construct(interp, this, path): if path == "": raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "Directory name must not be empty." )])) this.path = path this.file_name = path this.index = 0 if not os.path.isdir(path): raise PHPException(k_UnexpectedValueException.call_args( interp, [interp.space.wrap( "DirectoryIterator::__construct(%s): failed to open dir: No " "such file or directory" % path)])) try: w_dir = W_DirResource(interp.space, path) w_dir_res = w_dir.open() if not isinstance(w_dir_res, W_DirResource): raise OSError # rare case, but annotation fix this.w_dir_res = w_dir_res this.path_name = _di_pathname(this) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "DirectoryIterator::__construct(): error while opening stream" )]))
def sfo_construct(interp, this, filename, open_mode='r', use_include_path=False, w_ctx=None): this.file_name = filename this.path_name = rpath.realpath(filename) this.delimiter = "," this.enclosure = '"' this.flags = 0 this.open_mode = open_mode this.use_include_path = use_include_path this.w_res = None this.max_line_len = 0 if w_ctx: if not interp.space.is_resource(w_ctx): raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileObject::__construct() expects " "parameter 4 to be resource, %s given" % interp.space.get_type_name(w_ctx.tp).lower())])) assert filename is not None if os.path.isdir(filename): raise PHPException(k_LogicException.call_args( interp, [interp.space.wrap( "Cannot use SplFileObject with directories")])) try: this.w_res = _fopen(interp.space, filename, this.open_mode, use_include_path, w_ctx) if this.w_res == interp.space.w_False: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileObject::__construct(): Failed to open stream")])) except FopenError as e: raise PHPException(k_RuntimeException.call_args(interp, [interp.space.wrap(e.reasons.pop())]))
def di_construct(interp, this, path): if path == "": raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "Directory name must not be empty.")])) this.path = path this.file_name = path this.index = 0 if not os.path.isdir(path): raise PHPException(k_UnexpectedValueException.call_args( interp, [interp.space.wrap( "DirectoryIterator::__construct(%s): failed to open dir: No " "such file or directory" % path)])) try: w_dir = W_DirResource(interp.space, path) w_dir_res = w_dir.open() if not isinstance(w_dir_res, W_DirResource): raise OSError # rare case, but annotation fix this.w_dir_res = w_dir_res this.path_name = _di_pathname(this) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "DirectoryIterator::__construct(): error while opening stream" )]))
def sfo_get_current_line(interp, this): try: return _fgets(interp, this) except IOError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileObject::fgets(): File cannot be read")]))
def sfo_rewind(interp, this): try: this.w_res.rewind() except OSError, e: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileObject::rewind(): %s" % os.strerror(e.errno))]))
def rdi_construct(interp, this, path, flags=FI_KEY_AS_PATHNAME | FI_CURRENT_AS_FILEINFO): if not os.path.isdir(path): raise PHPException( k_UnexpectedValueException.call_args(interp, [ interp.space.wrap( "RecursiveDirectoryIterator::__construct(%s): failed to open dir: No " "such file or directory" % path) ])) this.flags = flags this.path = path this.file_name = path this.index = 0 try: w_dir = W_DirResource(interp.space, path, this.flags & FI_SKIP_DOTS) w_dir_res = w_dir.open() if not isinstance(w_dir_res, W_DirResource): raise OSError this.w_dir_res = w_dir_res this.path_name = _di_pathname(this) except OSError: raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "RecursiveDirectoryIterator::__construct(): error while opening stream" ) ]))
def _sfo_readline(interp, sfo): if sfo.open_mode not in ('w', 'a', 'x', 'c'): return sfo.w_res.readline(sfo.flags & SFO_DROP_NEW_LINE) else: raise PHPException( k_RuntimeException.call_args( interp, [interp.space.wrap("SplFileObject: File cannot be read")]))
def sfo_construct(interp, this, filename, open_mode='r', use_include_path=False, w_ctx=None): this.file_name = filename this.path_name = rpath.realpath(filename) this.delimiter = "," this.enclosure = '"' this.flags = 0 this.open_mode = open_mode this.use_include_path = use_include_path this.w_res = None this.max_line_len = 0 if w_ctx: if not interp.space.is_resource(w_ctx): raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileObject::__construct() expects " "parameter 4 to be resource, %s given" % interp.space.get_type_name(w_ctx.tp).lower()) ])) assert filename is not None if os.path.isdir(filename): raise PHPException( k_LogicException.call_args(interp, [ interp.space.wrap("Cannot use SplFileObject with directories") ])) try: this.w_res = _fopen(interp.space, filename, this.open_mode, use_include_path, w_ctx) if this.w_res == interp.space.w_False: raise PHPException( k_RuntimeException.call_args(interp, [ interp.space.wrap( "SplFileObject::__construct(): Failed to open stream") ])) except FopenError as e: raise PHPException( k_RuntimeException.call_args(interp, [interp.space.wrap(e.reasons.pop())]))
def get_linktarget(interp, this): filename = this.file_name assert filename is not None try: return interp.space.wrap(os.readlink(filename)) except OSError, e: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::getLinkTarget(): %s" % os.strerror(e.errno))]))
def get_type(interp, this): filename = this.file_name if not filename: return interp.space.w_False try: return _filetype(interp.space, filename) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::getType(): stat failed for %s" % filename)]))
def sfo_fgets(interp, this, args_w=[]): if len(args_w) != 0: interp.space.ec.warn("SplFileObject::fgets() expects exactly 0 " "parameters, %d given" % len(args_w)) return interp.space.w_Null try: return _fgets(interp, this) except IOError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileObject::fgets(): File cannot be read")]))
def getmtime(interp, this): filename = this.file_name assert filename is not None try: res = os.stat(filename).st_mtime return interp.space.wrap(int(res)) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::getMTime(): " "stat failed for %s" % this.file_name)]))
def get_size(interp, this): filename = this.file_name if not filename: return interp.space.w_False try: res = os.stat(filename).st_size return interp.space.wrap(res) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::getSize(): stat failed for %s" % filename)]))
def openfile(interp, this, open_mode='r', use_include_path=False, w_ctx=None): if open_mode == '': raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::openFile(): Invalid parameters")])) args = [interp.space.wrap(this.file_name), interp.space.wrap(open_mode), interp.space.wrap(use_include_path)] if w_ctx: if not interp.space.is_resource(w_ctx): raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::openFile() expects " "parameter 3 to be resource, %s given" % interp.space.get_type_name(w_ctx.tp).lower())])) args.append(w_ctx) try: file_object = SplFileObjectClass.call_args(interp, args) return file_object except OSError, e: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "SplFileInfo::openFile(): %s" % os.strerror(e.errno))]))
def rdi_construct(interp, this, path, flags= FI_KEY_AS_PATHNAME | FI_CURRENT_AS_FILEINFO): if not os.path.isdir(path): raise PHPException(k_UnexpectedValueException.call_args( interp, [interp.space.wrap( "RecursiveDirectoryIterator::__construct(%s): failed to open dir: No " "such file or directory" % path)])) this.flags = flags this.path = path this.file_name = path this.index = 0 try: w_dir = W_DirResource(interp.space, path, this.flags & FI_SKIP_DOTS) w_dir_res = w_dir.open() if not isinstance(w_dir_res, W_DirResource): raise OSError this.w_dir_res = w_dir_res this.path_name = _di_pathname(this) except OSError: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap( "RecursiveDirectoryIterator::__construct(): error while opening stream" )]))
def _sfo_readline(interp, sfo): if sfo.open_mode not in ('w', 'a', 'x', 'c'): return sfo.w_res.readline(sfo.flags & SFO_DROP_NEW_LINE) else: raise PHPException(k_RuntimeException.call_args( interp, [interp.space.wrap("SplFileObject: File cannot be read")]))