• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python file.blockreplace函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中salt.modules.file.blockreplace函数的典型用法代码示例。如果您正苦于以下问题:Python blockreplace函数的具体用法?Python blockreplace怎么用?Python blockreplace使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: test_no_modifications

    def test_no_modifications(self):
        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 4', backup=False)
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 4', backup=False)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)
开发者ID:1mentat,项目名称:salt,代码行数:7,代码来源:file_test.py


示例2: test_replace_prepend

    def test_replace_prepend(self):
        new_content = "Well, I didn't vote for you."

        self.assertRaises(
            CommandExecutionError,
            filemod.blockreplace,
            self.tfile.name,
            '#-- START BLOCK 2',
            '#-- END BLOCK 2',
            new_content,
            prepend_if_not_found=False,
            backup=False
        )
        with salt.utils.fopen(self.tfile.name, 'r') as fp:
            self.assertNotIn(
                '#-- START BLOCK 2' + "\n"
                + new_content + "\n" + '#-- END BLOCK 2',
                fp.read())

        filemod.blockreplace(self.tfile.name,
                             '#-- START BLOCK 2', '#-- END BLOCK 2',
                             new_content,
                             backup=False,
                             prepend_if_not_found=True)

        with salt.utils.fopen(self.tfile.name, 'r') as fp:
            self.assertTrue(
                fp.read().startswith(
                    '#-- START BLOCK 2'
                    + "\n" + new_content
                    + "\n" + '#-- END BLOCK 2'))
开发者ID:HowardMei,项目名称:saltstack,代码行数:31,代码来源:file_test.py


示例3: test_show_changes

    def test_show_changes(self):
        ret = filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 6', backup=False, show_changes=True)

        self.assertTrue(ret.startswith('---')) # looks like a diff

        ret = filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 7', backup=False, show_changes=False)

        self.assertIsInstance(ret, bool)
开发者ID:1mentat,项目名称:salt,代码行数:8,代码来源:file_test.py


示例4: test_replace_multiline

    def test_replace_multiline(self):
        new_multiline_content = "Who's that then?\nWell, how'd you become king, then?\nWe found them. I'm not a witch.\nWe shall say 'Ni' again to you, if you do not appease us."
        filemod.blockreplace(self.tfile.name, '#-- START BLOCK 1', '#-- END BLOCK 1', new_multiline_content, backup=False)

        with open(self.tfile.name, 'rb') as fp:
            filecontent=fp.read()
        self.assertIn('#-- START BLOCK 1'+"\n"+new_multiline_content+"\n"+'#-- END BLOCK 1', filecontent)
        self.assertNotIn('old content part 1', filecontent)
        self.assertNotIn('old content part 2', filecontent)
开发者ID:1mentat,项目名称:salt,代码行数:9,代码来源:file_test.py


示例5: test_dry_run

    def test_dry_run(self):
        before_ctime = os.stat(self.tfile.name).st_mtime
        filemod.blockreplace(self.tfile.name,
                             '// START BLOCK',
                             '// END BLOCK',
                             'new content 5',
                             dry_run=True)
        after_ctime = os.stat(self.tfile.name).st_mtime

        self.assertEqual(before_ctime, after_ctime)
开发者ID:HowardMei,项目名称:saltstack,代码行数:10,代码来源:file_test.py


示例6: test_replace_partial_marked_lines

    def test_replace_partial_marked_lines(self):
        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 1', backup=False)

        with open(self.tfile.name, 'rb') as fp:
            filecontent=fp.read()
        self.assertIn('new content 1', filecontent)
        self.assertNotIn('to be removed', filecontent)
        self.assertIn('first part of start line', filecontent)
        self.assertIn('first part of end line', filecontent)
        self.assertIn('part of start line not removed', filecontent)
        self.assertIn('part of end line not removed', filecontent)
开发者ID:1mentat,项目名称:salt,代码行数:11,代码来源:file_test.py


示例7: test_backup

    def test_backup(self):
        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 2', backup=fext)

        self.assertTrue(os.path.exists(bak_file))
        os.unlink(bak_file)
        self.assertFalse(os.path.exists(bak_file))

        fext = '.bak'
        bak_file = '{0}{1}'.format(self.tfile.name, fext)

        filemod.blockreplace(self.tfile.name, '// START BLOCK', '// END BLOCK', 'new content 3', backup=False)

        self.assertFalse(os.path.exists(bak_file))
开发者ID:1mentat,项目名称:salt,代码行数:16,代码来源:file_test.py


示例8: test_replace_append_newline_at_eof

 def test_replace_append_newline_at_eof(self):
     '''
     Check that file.blockreplace works consistently on files with and
     without newlines at end of file.
     '''
     base = 'bar'
     args = {
             'marker_start': '#start',
             'marker_end': '#stop',
             'content': 'baz',
             'append_if_not_found': True,
     }
     block = '{marker_start}\n{content}\n{marker_end}\n'.format(**args)
     expected = base + '\n' + block
     # File ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write(base + '\n')
         tfile.flush()
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # File not ending with a newline
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         tfile.write(base)
         tfile.flush()
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), expected)
     # A newline should not be added in empty files
     with tempfile.NamedTemporaryFile(mode='w+') as tfile:
         filemod.blockreplace(tfile.name, **args)
         with salt.utils.fopen(tfile.name) as tfile2:
             self.assertEqual(tfile2.read(), block)
开发者ID:HowardMei,项目名称:saltstack,代码行数:33,代码来源:file_test.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python file.replace函数代码示例发布时间:2022-05-27
下一篇:
Python modules.state_std函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap