Exemple #1
0
    def collect_single_file(self, file_path):
        """
        Takes in a list of strings, usually the lines in a text file,
        and collects the AnchorHub tags and auto-generated anchors for the
        file according to the  Collector's converter, strategies, and switches

        :param file_path: string file path of file to examine
        :return: A dictionary mapping AnchorHub tags to auto-generated
            anchors, and a list of containing an entry for each duplicate tag
            found on the page.
        """
        lines = FileToList.to_list(file_path)
        file_anchors = {}
        file_duplicates = []
        for i in range(len(lines)):
            # Flip any switches that are triggered by this line
            self._try_switches(lines, i)
            if self._no_switches_on():
                for s in self._strategies:
                    if s.test(lines, i):
                        # This strategy found an anchor and knows how to parse
                        tag, convert_me = s.get(lines, i)
                        if tag in file_anchors:
                            # Duplicate tag
                            file_duplicates.append(
                                (tag, i + 1, file_anchors[tag]))
                        else:
                            anchor = self._converter(convert_me, file_anchors)
                            file_anchors[tag] = anchor
            self._arm_switches()
        return file_anchors, file_duplicates
Exemple #2
0
    def write_single_file(self, file_path, anchors, opts):
        """

        :param file_path:
        :param anchors:
        :param opts:
        :return: A list of numbers, counting the number of times each
            strategy was used
        """
        lines = FileToList.to_list(file_path)
        new_text = []
        file_is_modified = False  # Will only rewrite file when True
        for i in range(len(lines)):
            modified_line = lines[i]
            # Flip any switches that are triggered by this line
            self._try_switches(lines, i)
            if self._no_switches_on():
                for n, s in enumerate(self._strategies):
                    if s.test(modified_line, lines, i):
                        # Strategy detected that it may modify this line
                        mod = s.modify(modified_line, anchors, file_path)
                        if modified_line != mod:
                            # Strategy modified the line
                            modified_line = mod
                            self._counter[n][0] += 1  # Strategy counter +1
                            file_is_modified = True  # Must rewrite this file
            new_text.append(modified_line)
            self._arm_switches()
        if file_is_modified:
            self._write_with_opts(file_path, new_text, opts)
        return self._counter
Exemple #3
0
    def write_single_file(self, file_path, anchors, opts):
        """

        :param file_path:
        :param anchors:
        :param opts:
        :return: A list of numbers, counting the number of times each
            strategy was used
        """
        lines = FileToList.to_list(file_path)
        new_text = []
        file_is_modified = False  # Will only rewrite file when True
        for i in range(len(lines)):
            modified_line = lines[i]
            # Flip any switches that are triggered by this line
            self._try_switches(lines, i)
            if self._no_switches_on():
                for n, s in enumerate(self._strategies):
                    if s.test(modified_line, lines, i):
                        # Strategy detected that it may modify this line
                        mod = s.modify(modified_line, anchors, file_path)
                        if modified_line != mod:
                            # Strategy modified the line
                            modified_line = mod
                            self._counter[n][0] += 1  # Strategy counter +1
                            file_is_modified = True  # Must rewrite this file
            new_text.append(modified_line)
            self._arm_switches()
        if file_is_modified:
            self._write_with_opts(file_path, new_text, opts)
        return self._counter
Exemple #4
0
    def collect_single_file(self, file_path):
        """
        Takes in a list of strings, usually the lines in a text file,
        and collects the AnchorHub tags and auto-generated anchors for the
        file according to the  Collector's converter, strategies, and switches

        :param file_path: string file path of file to examine
        :return: A dictionary mapping AnchorHub tags to auto-generated
            anchors, and a list of containing an entry for each duplicate tag
            found on the page.
        """
        lines = FileToList.to_list(file_path)
        file_anchors = {}
        file_duplicates = []
        for i in range(len(lines)):
            # Flip any switches that are triggered by this line
            self._try_switches(lines, i)
            if self._no_switches_on():
                for s in self._strategies:
                    if s.test(lines, i):
                        # This strategy found an anchor and knows how to parse
                        tag, convert_me = s.get(lines, i)
                        if tag in file_anchors:
                            # Duplicate tag
                            file_duplicates.append((tag, i + 1,
                                                    file_anchors[tag]))
                        else:
                            anchor = self._converter(convert_me, file_anchors)
                            file_anchors[tag] = anchor
            self._arm_switches()
        return file_anchors, file_duplicates
Exemple #5
0
def test_file_to_list_basic():
    sep = get_path_separator()
    path = get_anchorhub_path() + sep + 'lib' + sep + 'tests' + sep + \
           'test_data' + sep + 'filelist'
    assert FileToList.to_list(path) == ['Hello!\n', 'My name\n', 'is\n', \
                                        'AnchorHub']