def test_read_write(manager, watcher, tmpdir): filename = str(tmpdir.ensure("helloworld.txt")) app = FileApp(filename, "w").register(manager) assert watcher.wait("opened", app.file.channel) app.fire(write(b"Hello World!"), app.file.channel) assert watcher.wait("write", app.file.channel) app.fire(close(), app.file.channel) assert watcher.wait("closed", app.file.channel) assert app.closed app.unregister() assert watcher.wait("unregistered") app = FileApp(filename, "r").register(manager) assert watcher.wait("opened", app.file.channel) assert watcher.wait("eof", app.file.channel) assert app.eof app.fire(close(), app.file.channel) assert watcher.wait("closed", app.file.channel) assert app.closed app.unregister() assert watcher.wait("unregistered") s = app.buffer.getvalue() assert s == b"Hello World!"
def rotate(self): dirname = path.dirname(self.filename) filename = path.basename(self.filename) channel, _ = parse_logfile(filename) logfile = generate_logfile(channel) self.fire(close(), self.channel) self.fire(open(path.join(dirname, logfile), "a"), self.channel) interval = datetime.fromordinal(( date.today() + timedelta(1) ).toordinal()) Timer(interval, rotate(), self.channel).register(self)
def rotatefile(self): channel_dir = path.dirname(self.filename) output_dir = path.dirname(channel_dir) channel = path.basename(channel_dir) logfile = generate_logfile(channel) self.fire(close(), self.channel) self.fire(_open(path.join(output_dir, logfile), "a"), self.channel) interval = datetime.fromordinal(( date.today() + timedelta(1) ).toordinal()) log("Next log roration set for: {}".format(interval.strftime("%Y-%m-%d %H:%M:%S"))) Timer(interval, rotatefile(), self.channel).register(self)
def rotate(self): channel_dir = path.dirname(self.filename) output_dir = path.dirname(channel_dir) channel = path.basename(channel_dir) logfile = generate_logfile(channel) self.fire(close(), self.channel) self.fire(_open(path.join(output_dir, logfile), "a"), self.channel) interval = datetime.fromordinal( (date.today() + timedelta(1)).toordinal()) print("Next log roration set for: {}".format( interval.strftime("%Y-%m-%d %H:%M:%S"))) Timer(interval, rotate(), self.channel).register(self)