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


Python Grammar.translate方法代码示例

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


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

示例1: ScriptFile

# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import translate [as 别名]
class ScriptFile(object):
    """
    Encapsulates a script file. The migration
    process can be run by calling migrate
    """
    _filename = None
    dobackup = True
    backup_ext = '.mantidbackup'
    backup_filename = None
    grammar = None

    def getfilename(self):
        return self._filename

    def setfilename(self, filename):
        """Set the filename along with the backup filename
            @param filename The full filename of the input
        """
        if os.path.exists(filename):
            self._filename = filename
            self.backup_filename = filename + self.backup_ext
        else:
            raise ValueError("Invalid path '%s' passed to ScriptFile" % (filename))

    filename = property(getfilename, setfilename)

    def __init__(self, filename, backup = True):
        self.setfilename(filename)
        self.dobackup = backup
        self.grammar = Grammar()

    def migrate(self):
        """
        Migrate the script to the new API
        """
        self.backup()
        input_file = open(self.filename, 'r')
        input_as_str = input_file.read()
        input_file.close()

        converted_str, errors = self.grammar.translate(input_as_str)

        filename = self.filename
        if len(errors) > 0:
            filename = self.filename + ".partial"
            errors.append("Partial translation stored in '%s'" % filename)

        output_file = open(filename, 'w')
        output_file.write(converted_str)
        output_file.close()

        if len(errors) > 0:
            raise RuntimeError("\n".join(errors))

        outcome = MigrationStatus(MigrationStatus.Migrated)
        return Report(self.filename, outcome)

    def backup(self):
        """
        Backup the file by copying it to
        a different filename with the
        extension defined by self.backup_ext
        """
        if self.dobackup and self.filename is not None:
            messages.notify("Backing up %s to %s" % (self.filename, self.backup_filename))
            shutil.copy(self.filename, self.backup_filename)

    def restore_backup(self):
        """
        Copies the file from the backup to the original
        location
        """
        if not self.dobackup:
            messages.notify("Cannot restore from backup, no backup was requested")
        if os.path.exists(self.backup_filename):
            shutil.copy(self.backup_filename, self.filename)
开发者ID:liyulun,项目名称:mantid,代码行数:78,代码来源:migrate.py


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