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


Python SpacingHelper.replace_spaces_with_tabs方法代码示例

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


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

示例1: run

# 需要导入模块: from coalib.bearlib.spacing.SpacingHelper import SpacingHelper [as 别名]
# 或者: from coalib.bearlib.spacing.SpacingHelper.SpacingHelper import replace_spaces_with_tabs [as 别名]
    def run(self,
            filename,
            file,
            use_spaces: bool,
            allow_trailing_whitespace: bool=False,
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH):
        """
        Checks the space consistency for each line.

        :param use_spaces:                True if spaces are to be used
                                          instead of tabs.
        :param allow_trailing_whitespace: Whether to allow trailing whitespace
                                          or not.
        :param tab_width:                 Number of spaces representing one
                                          tab.
        """
        results = []

        spacing_helper = SpacingHelper(tab_width)

        for line_number, line in enumerate(file):
            replacement = line

            if not allow_trailing_whitespace:
                replacement = replacement.rstrip(" \t\n") + "\n"

            if use_spaces:
                replacement = spacing_helper.replace_tabs_with_spaces(
                    replacement)
            else:
                replacement = spacing_helper.replace_spaces_with_tabs(
                    replacement)

            if replacement != line:
                diff = Diff()
                diff.change_line(line_number + 1, line, replacement)
                results.append(PatchResult(self,
                                           _("Line contains spacing "
                                             "inconsistencies."),
                                           {filename: diff},
                                           filename,
                                           line_nr=line_number+1))

        return results
开发者ID:AhmedKamal1432,项目名称:coala,代码行数:46,代码来源:SpaceConsistencyBear.py

示例2: SpacingHelperTest

# 需要导入模块: from coalib.bearlib.spacing.SpacingHelper import SpacingHelper [as 别名]
# 或者: from coalib.bearlib.spacing.SpacingHelper.SpacingHelper import replace_spaces_with_tabs [as 别名]
class SpacingHelperTest(unittest.TestCase):

    def setUp(self):
        self.uut = SpacingHelper()

    def test_needed_settings(self):
        self.assertEqual(list(self.uut.get_optional_settings()), ['tab_width'])
        self.assertEqual(list(self.uut.get_non_optional_settings()), [])

    def test_construction(self):
        section = Section('test section')
        self.assertRaises(TypeError, SpacingHelper, 'no integer')
        self.assertRaises(TypeError, self.uut.from_section, 5)

        self.assertEqual(self.uut.tab_width,
                         self.uut.from_section(section).tab_width)

        # This is assumed in some tests. If you want to change this value, be
        # sure to change the tests too
        self.assertEqual(self.uut.DEFAULT_TAB_WIDTH, 4)
        self.assertEqual(self.uut.tab_width, self.uut.DEFAULT_TAB_WIDTH)

    def test_get_indentation(self):
        self.assertRaises(TypeError, self.uut.get_indentation, 5)

        self.assertEqual(self.uut.get_indentation('no indentation'), 0)
        self.assertEqual(self.uut.get_indentation(' indentation'), 1)
        self.assertEqual(self.uut.get_indentation('  indentation'), 2)
        self.assertEqual(self.uut.get_indentation('\tindentation'),
                         self.uut.DEFAULT_TAB_WIDTH)

        # Having a space before the tab shouldn't make any difference
        self.assertEqual(self.uut.get_indentation(' \tindentation'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t indentation'),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation('\t indentation'),
                         self.uut.DEFAULT_TAB_WIDTH+1)

        # same tests but with indentation only
        self.assertEqual(self.uut.get_indentation('\t'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t'),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(' \t '),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation('\t '),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation('\t\t'),
                         self.uut.DEFAULT_TAB_WIDTH*2)

    def test_replace_tabs_with_spaces(self):
        self.assertRaises(TypeError, self.uut.replace_tabs_with_spaces, 5)

        self.assertEqual(self.uut.replace_tabs_with_spaces(''), '')
        self.assertEqual(self.uut.replace_tabs_with_spaces(' '), ' ')
        self.assertEqual(self.uut.replace_tabs_with_spaces('\t'),
                         ' '*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('\t\t'),
                         ' '*self.uut.DEFAULT_TAB_WIDTH*2)
        self.assertEqual(self.uut.replace_tabs_with_spaces(' \t'),
                         ' '*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('  \t'),
                         ' '*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces('d \t '),
                         'd' + ' '*self.uut.DEFAULT_TAB_WIDTH)

    def test_replace_spaces_with_tabs(self):
        self.assertRaises(TypeError, self.uut.replace_spaces_with_tabs, 5)

        self.assertEqual(self.uut.replace_spaces_with_tabs(''), '')
        self.assertEqual(self.uut.replace_spaces_with_tabs(' '), ' ')
        self.assertEqual(self.uut.replace_spaces_with_tabs('    '), '\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   \t'), '\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd  '),
                         '   dd  ')
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd d '),
                         '   dd d ')  # One space shouldnt be replaced
        self.assertEqual(self.uut.replace_spaces_with_tabs('   dd   '),
                         '   dd\t')
        self.assertEqual(
            self.uut.replace_spaces_with_tabs(' \t   a_text   another'),
            '\t   a_text\tanother')
        self.assertEqual(self.uut.replace_spaces_with_tabs('123\t'), '123\t')
        self.assertEqual(self.uut.replace_spaces_with_tabs('d  d'), 'd  d')
开发者ID:Anmolbansal1,项目名称:coala,代码行数:87,代码来源:SpacingHelperTest.py

示例3: run

# 需要导入模块: from coalib.bearlib.spacing.SpacingHelper import SpacingHelper [as 别名]
# 或者: from coalib.bearlib.spacing.SpacingHelper.SpacingHelper import replace_spaces_with_tabs [as 别名]
    def run(self,
            filename,
            file,
            use_spaces: bool,
            allow_trailing_whitespace: bool = False,
            indent_size: int = SpacingHelper.DEFAULT_TAB_WIDTH,
            enforce_newline_at_EOF: bool = True,
            ):
        '''
        Check and correct spacing for all textual data. This includes usage of
        tabs vs. spaces, trailing whitespace and (missing) newlines before
        the end of the file.

        :param use_spaces:
            True if spaces are to be used instead of tabs.
        :param allow_trailing_whitespace:
            Whether to allow trailing whitespace or not.
        :param indent_size:
            Number of spaces per indentation level.
        :param enforce_newline_at_EOF:
            Whether to enforce a newline at the End Of File.
        '''
        spacing_helper = SpacingHelper(indent_size)
        result_texts = []
        additional_info_texts = []

        for line_number, line in enumerate(file, start=1):
            replacement = line

            if enforce_newline_at_EOF:
                # Since every line contains at the end at least one \n, only
                # the last line could potentially not have one. So we don't
                # need to check whether the current line_number is the last
                # one.
                if replacement[-1] != '\n':
                    replacement += '\n'
                    result_texts.append('No newline at EOF.')
                    additional_info_texts.append(
                        "A trailing newline character ('\\n') is missing from "
                        'your file. '
                        '<http://stackoverflow.com/a/5813359/3212182> gives '
                        'more information about why you might need one.')

            if not allow_trailing_whitespace:
                replacement = replacement.rstrip(' \t\n') + '\n'
                if replacement != line.rstrip('\n') + '\n':
                    result_texts.append('Trailing whitespaces.')
                    additional_info_texts.append(
                        'Your source code contains trailing whitespaces. '
                        'Those usually have no meaning. Please consider '
                        'removing them.')

            if use_spaces:
                pre_replacement = replacement
                replacement = replacement.expandtabs(indent_size)
                if replacement != pre_replacement:
                    result_texts.append('Tabs used instead of spaces.')
            else:
                pre_replacement = replacement
                replacement = spacing_helper.replace_spaces_with_tabs(
                    replacement)
                if replacement != pre_replacement:
                    result_texts.append('Spaces used instead of tabs.')

            if len(result_texts) > 0:
                diff = Diff(file)
                diff.change_line(line_number, line, replacement)
                inconsistencies = ''.join('\n- ' + string
                                          for string in result_texts)
                yield Result.from_values(
                    self,
                    'Line contains following spacing inconsistencies:'
                    + inconsistencies,
                    diffs={filename: diff},
                    file=filename,
                    line=line_number,
                    additional_info='\n\n'.join(additional_info_texts))
                result_texts = []
                additional_info_texts = []
开发者ID:Anmolbansal1,项目名称:coala-bears,代码行数:81,代码来源:SpaceConsistencyBear.py

示例4: run

# 需要导入模块: from coalib.bearlib.spacing.SpacingHelper import SpacingHelper [as 别名]
# 或者: from coalib.bearlib.spacing.SpacingHelper.SpacingHelper import replace_spaces_with_tabs [as 别名]
    def run(self,
            filename,
            file,
            use_spaces: bool,
            allow_trailing_whitespace: bool=False,
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH,
            enforce_newline_at_EOF: bool=True):
        '''
        Checks the space consistency for each line.

        :param use_spaces:                True if spaces are to be used instead
                                          of tabs.
        :param allow_trailing_whitespace: Whether to allow trailing whitespace
                                          or not.
        :param tab_width:                 Number of spaces representing one
                                          tab.
        :param enforce_newline_at_EOF:    Whether to enforce a newline at the
                                          End Of File.
        '''
        spacing_helper = SpacingHelper(tab_width)
        result_texts = []

        for line_number, line in enumerate(file, start=1):
            replacement = line

            if enforce_newline_at_EOF:
                # Since every line contains at the end at least one \n, only
                # the last line could potentially not have one. So we don't
                # need to check whether the current line_number is the last
                # one.
                if replacement[-1] != "\n":
                    replacement += "\n"
                    result_texts.append(_("No newline at EOF."))

            if not allow_trailing_whitespace:
                pre_replacement = line
                replacement = replacement.rstrip(" \t\n") + "\n"
                if replacement != pre_replacement:
                    result_texts.append(_("Trailing whitespaces."))

            if use_spaces:
                pre_replacement = replacement
                replacement = spacing_helper.replace_tabs_with_spaces(
                    replacement)
                if replacement != pre_replacement:
                    result_texts.append(_("Tabs used instead of spaces."))
            else:
                pre_replacement = replacement
                replacement = spacing_helper.replace_spaces_with_tabs(
                    replacement)
                if replacement != pre_replacement:
                    result_texts.append(_("Spaces used instead of tabs."))

            if len(result_texts) > 0:
                diff = Diff()
                diff.change_line(line_number, line, replacement)
                inconsistencies = "".join("\n- " + string
                                          for string in result_texts)
                yield Result.from_values(
                    self,
                    _("Line contains following spacing inconsistencies:")
                    + inconsistencies,
                    diffs={filename: diff},
                    file=filename,
                    line=line_number)
                result_texts = []
开发者ID:VCTLabs,项目名称:coala,代码行数:68,代码来源:SpaceConsistencyBear.py

示例5: run

# 需要导入模块: from coalib.bearlib.spacing.SpacingHelper import SpacingHelper [as 别名]
# 或者: from coalib.bearlib.spacing.SpacingHelper.SpacingHelper import replace_spaces_with_tabs [as 别名]
    def run(self,
            filename,
            file,
            use_spaces: bool,
            allow_trailing_whitespace: bool=False,
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH,
            enforce_newline_at_EOF: bool=True):
        '''
        Check and correct spacing for all textual data. This includes usage of
        tabs vs. spaces, trailing whitespace and (missing) newlines before
        the end of the file.

        :param use_spaces:                True if spaces are to be used instead
                                          of tabs.
        :param allow_trailing_whitespace: Whether to allow trailing whitespace
                                          or not.
        :param tab_width:                 Number of spaces representing one
                                          tab.
        :param enforce_newline_at_EOF:    Whether to enforce a newline at the
                                          End Of File.
        '''
        spacing_helper = SpacingHelper(tab_width)
        result_texts = []
        additional_info_texts = []

        for line_number, line in enumerate(file, start=1):
            replacement = line

            if enforce_newline_at_EOF:
                # Since every line contains at the end at least one \n, only
                # the last line could potentially not have one. So we don't
                # need to check whether the current line_number is the last
                # one.
                if replacement[-1] != "\n":
                    replacement += "\n"
                    result_texts.append("No newline at EOF.")
                    additional_info_texts.append(
                        "A trailing newline character ('\\n') is missing from "
                        "your file. "
                        "<http://stackoverflow.com/a/5813359/3212182> gives "
                        "more information about why you might need one.")

            if not allow_trailing_whitespace:
                replacement = replacement.rstrip(" \t\n") + "\n"
                if replacement != line.rstrip("\n") + "\n":
                    result_texts.append("Trailing whitespaces.")
                    additional_info_texts.append(
                        "Your source code contains trailing whitespaces. Those "
                        "usually have no meaning. Please consider removing "
                        "them.")

            if use_spaces:
                pre_replacement = replacement
                replacement = spacing_helper.replace_tabs_with_spaces(
                    replacement)
                if replacement != pre_replacement:
                    result_texts.append("Tabs used instead of spaces.")
            else:
                pre_replacement = replacement
                replacement = spacing_helper.replace_spaces_with_tabs(
                    replacement)
                if replacement != pre_replacement:
                    result_texts.append("Spaces used instead of tabs.")

            if len(result_texts) > 0:
                diff = Diff(file)
                diff.change_line(line_number, line, replacement)
                inconsistencies = "".join("\n- " + string
                                          for string in result_texts)
                yield Result.from_values(
                    self,
                    "Line contains following spacing inconsistencies:"
                    + inconsistencies,
                    diffs={filename: diff},
                    file=filename,
                    line=line_number,
                    additional_info="\n\n".join(additional_info_texts))
                result_texts = []
                additional_info_texts = []
开发者ID:Shreyas94,项目名称:coala-bears,代码行数:81,代码来源:SpaceConsistencyBear.py

示例6: SpacingHelperTest

# 需要导入模块: from coalib.bearlib.spacing.SpacingHelper import SpacingHelper [as 别名]
# 或者: from coalib.bearlib.spacing.SpacingHelper.SpacingHelper import replace_spaces_with_tabs [as 别名]
class SpacingHelperTest(unittest.TestCase):
    def setUp(self):
        self.uut = SpacingHelper()

    def test_needed_settings(self):
        self.assertEqual(list(self.uut.get_optional_settings()), ["tab_width"])
        self.assertEqual(list(self.uut.get_non_optional_settings()), [])

    def test_construction(self):
        section = Section("test section")
        self.assertRaises(TypeError, SpacingHelper, "no integer")
        self.assertRaises(TypeError, self.uut.from_section, 5)

        self.assertEqual(self.uut.tab_width,
                         self.uut.from_section(section).tab_width)

        section.append(Setting("tab_width", "invalid"))
        # Setting won't be converted since it's not possible, SpacingHelper
        # will then complain with TypeError
        self.assertRaises(TypeError, self.uut.from_section, section)

        # This is assumed in some tests. If you want to change this value, be
        # sure to change the tests too
        self.assertEqual(self.uut.DEFAULT_TAB_WIDTH, 4)
        self.assertEqual(self.uut.tab_width, self.uut.DEFAULT_TAB_WIDTH)

    def test_get_indentation(self):
        self.assertRaises(TypeError, self.uut.get_indentation, 5)

        self.assertEqual(self.uut.get_indentation("no indentation"), 0)
        self.assertEqual(self.uut.get_indentation(" indentation"), 1)
        self.assertEqual(self.uut.get_indentation("  indentation"), 2)
        self.assertEqual(self.uut.get_indentation("\tindentation"),
                         self.uut.DEFAULT_TAB_WIDTH)

        # Having a space before the tab shouldn't make any difference
        self.assertEqual(self.uut.get_indentation(" \tindentation"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t indentation"),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation("\t indentation"),
                         self.uut.DEFAULT_TAB_WIDTH+1)

        # same tests but with indentation only
        self.assertEqual(self.uut.get_indentation("\t"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t"),
                         self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.get_indentation(" \t "),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation("\t "),
                         self.uut.DEFAULT_TAB_WIDTH+1)
        self.assertEqual(self.uut.get_indentation("\t\t"),
                         self.uut.DEFAULT_TAB_WIDTH*2)

    def test_replace_tabs_with_spaces(self):
        self.assertRaises(TypeError, self.uut.replace_tabs_with_spaces, 5)

        self.assertEqual(self.uut.replace_tabs_with_spaces(""), "")
        self.assertEqual(self.uut.replace_tabs_with_spaces(" "), " ")
        self.assertEqual(self.uut.replace_tabs_with_spaces("\t"),
                         " "*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("\t\t"),
                         " "*self.uut.DEFAULT_TAB_WIDTH*2)
        self.assertEqual(self.uut.replace_tabs_with_spaces(" \t"),
                         " "*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("  \t"),
                         " "*self.uut.DEFAULT_TAB_WIDTH)
        self.assertEqual(self.uut.replace_tabs_with_spaces("d \t "),
                         "d" + " "*self.uut.DEFAULT_TAB_WIDTH)

    def test_replace_spaces_with_tabs(self):
        self.assertRaises(TypeError, self.uut.replace_spaces_with_tabs, 5)

        self.assertEqual(self.uut.replace_spaces_with_tabs(""), "")
        self.assertEqual(self.uut.replace_spaces_with_tabs(" "), " ")
        self.assertEqual(self.uut.replace_spaces_with_tabs("    "), "\t")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   \t"), "\t")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd  "),
                         "   dd  ")
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd d "),
                         "   dd d ")  # One space shouldnt be replaced
        self.assertEqual(self.uut.replace_spaces_with_tabs("   dd   "),
                         "   dd\t")
        self.assertEqual(
            self.uut.replace_spaces_with_tabs(" \t   a_text   another"),
            "\t   a_text\tanother")
        self.assertEqual(self.uut.replace_spaces_with_tabs("d  d"), "d  d")
开发者ID:VCTLabs,项目名称:coala,代码行数:90,代码来源:SpacingHelperTest.py


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