当前位置: 首页>>代码示例>>Python>>正文


Python LineReader.bytesRead方法代码示例

本文整理汇总了Python中PyFoam.Basics.LineReader.LineReader.bytesRead方法的典型用法代码示例。如果您正苦于以下问题:Python LineReader.bytesRead方法的具体用法?Python LineReader.bytesRead怎么用?Python LineReader.bytesRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyFoam.Basics.LineReader.LineReader的用法示例。


在下文中一共展示了LineReader.bytesRead方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BasicWatcher

# 需要导入模块: from PyFoam.Basics.LineReader import LineReader [as 别名]
# 或者: from PyFoam.Basics.LineReader.LineReader import bytesRead [as 别名]
class BasicWatcher(object):
    """Base class for watching the output of commands

    Works like the UNIX-command 'tail -f <file>': the last lines of the file are output.
    If the file grows then these lines are output as they arrive"""

    def __init__(self,filenames,
                 silent=False,
                 tailLength=1000,
                 sleep=0.1,
                 follow=True):
        """:param filename: name of the logfile to watch
        :param silent: if True no output is sent to stdout
        :param tailLength: number of bytes at the end of the fail that should be output.
        :param follow: if the end of the file is reached wait for further input
        Because data is output on a per-line-basis
        :param sleep: interval to sleep if no line is returned"""

        if type(filenames) is list:
            meshTimes=[]
            createMesh="Create mesh for time = "
            for fName in filenames:
                meshTime=None
                with open(fName) as f:
                    for l in f.readlines()[:100]:
                        if l.find(createMesh)==0:
                            meshTime=float(l[len(createMesh):])
                            break
                meshTimes.append((fName,meshTime))
            meshTimes.sort(key=lambda x:1e50 if x[1] is None else x[1])
            filenames=[m[0] for m in meshTimes]
            self.filename=filenames[0]
            self.nextFiles=filenames[1:]
            self.changeTimes=[m[1] for m in meshTimes[1:]]
        else:
            self.filename=filenames
            self.nextFiles=[]
            self.changeTimes=[]

        self._changeFileHooks=[]

        self.silent=silent
        self.tail=tailLength
        self.sleep=sleep
        self.follow=follow
        self.isTailing=False

        if not path.exists(self.filename):
            print_("Error: Logfile ",self.filename,"does not exist")

        self.reader=LineReader(config().getboolean("SolverOutput","stripSpaces"))

    def getSize(self,filename):
        """:return: the current size (in bytes) of the file"""
        return os.stat(filename)[stat.ST_SIZE]

    def addChangeFileHook(self,func):
        self._changeFileHooks.append(func)

    def changeFile(self,filename):
        currSize=self.getSize(filename)

        fn,ext=path.splitext(filename)
        if ext=='.gz':
            fh=gzip.open(filename)
        else:
            fh=open(filename)

        for f in self._changeFileHooks:
            f()

        return fh,currSize

    def start(self):
        """Reads the file and does the processing"""

        fh,currSize=self.changeFile(self.filename)
        switchTime=None if len(self.changeTimes)==0 else self.changeTimes[0]
        self.changeTimes=self.changeTimes[1:]

        self.startHandle()

        while self.follow or currSize>self.reader.bytesRead() or len(self.nextFiles)>0:
            if not currSize>self.reader.bytesRead() or self.analyzer.isPastTime(switchTime):
                if len(self.nextFiles)>0:
                    print_("\n\nSwitching from logfile",self.filename,
                           "to",self.nextFiles[0],"\n\n")
                    self.filename=self.nextFiles[0]
                    self.nextFiles=self.nextFiles[1:]
                    fh,currSize=self.changeFile(self.filename)
                    switchTime=None if len(self.changeTimes)==0 else self.changeTimes[0]
                    self.changeTimes=self.changeTimes[1:]
                    self.reader.reset()
                    self.analyzer.resetFile()
                    if currSize==0:
                        continue
                else:
                    if not self.follow:
                        break
            try:
#.........这里部分代码省略.........
开发者ID:Unofficial-Extend-Project-Mirror,项目名称:openfoam-extend-Breeder-other-scripting-PyFoam,代码行数:103,代码来源:BasicWatcher.py

示例2: BasicWatcher

# 需要导入模块: from PyFoam.Basics.LineReader import LineReader [as 别名]
# 或者: from PyFoam.Basics.LineReader.LineReader import bytesRead [as 别名]
class BasicWatcher(object):
    """Base class for watching the output of commands

    Works like the UNIX-command 'tail -f <file>': the last lines of the file are output.
    If the file grows then these lines are output as they arrive"""

    def __init__(self,filename,
                 silent=False,
                 tailLength=1000,
                 sleep=0.1,
                 follow=True):
        """@param filename: name of the logfile to watch
        @param silent: if True no output is sent to stdout
        @param tailLength: number of bytes at the end of the fail that should be output.
        @param follow: if the end of the file is reached wait for further input
        Because data is output on a per-line-basis
        @param sleep: interval to sleep if no line is returned"""

        self.filename=filename
        self.silent=silent
        self.tail=tailLength
        self.sleep=sleep
        self.follow=follow
        self.isTailing=False

        if not path.exists(self.filename):
            print_("Error: Logfile ",self.filename,"does not exist")

        self.reader=LineReader()

    def getSize(self):
        """@return: the current size (in bytes) of the file"""
        return os.stat(self.filename)[stat.ST_SIZE]

    def start(self):
        """Reads the file and does the processing"""

        currSize=self.getSize()

        fn,ext=path.splitext(self.filename)
        if ext=='.gz':
            fh=gzip.open(self.filename)
        else:
            fh=open(self.filename)

        self.startHandle()

        while self.follow or currSize>self.reader.bytesRead():
            try:
                status=self.reader.read(fh)
                if status:
                    line=self.reader.line
                    if (currSize-self.reader.bytesRead())<=self.tail:
                        if not self.isTailing:
                            self.isTailing=True
                            self.timeHandle()
                            self.tailingHandle()

                        if not self.silent:
                            print_(line)

                    self.lineHandle(line)
                else:
                    if self.reader.userSaidStop():
                        break
                    sleep(self.sleep)
            except KeyboardInterrupt:
                print_("Watcher: Keyboard interrupt")
                break

        self.stopHandle()

        fh.close()

    def startHandle(self):
        """to be called before the program is started"""
        pass

    def stopHandle(self):
        """called after the program has stopped"""
        pass

    def tailingHandle(self):
        """called when the first line is output"""
        pass

    def lineHandle(self,line):
        """called every time a new line is read"""
        pass
开发者ID:LeeRuns,项目名称:PyFoam,代码行数:91,代码来源:BasicWatcher.py


注:本文中的PyFoam.Basics.LineReader.LineReader.bytesRead方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。