Exemple #1
0
    def load_snippets(cls, src_path, violation_lines):
        """
        Load snippets from the file at `src_path` to show
        violations on lines in the list `violation_lines`
        (list of line numbers, starting at index 0).

        The file at `src_path` should be a text file (not binary).

        Returns a list of `Snippet` instances.

        Raises an `IOError` if the file could not be loaded.
        """
        # Load the contents of the file
        with openpy.open(src_path) as src_file:
            contents = src_file.read()

        # Convert the source file to unicode (Python < 3)
        if isinstance(contents, six.binary_type):
            contents = contents.decode('utf-8', 'replace')

        # Construct a list of snippet ranges
        src_lines = contents.split('\n')
        snippet_ranges = cls._snippet_ranges(len(src_lines), violation_lines)

        # Parse the source into tokens
        token_stream = cls._parse_src(contents, src_path)

        # Group the tokens by snippet
        token_groups = cls._group_tokens(token_stream, snippet_ranges)

        return [
            Snippet(tokens, src_path, start, violation_lines)
            for (start, _), tokens in six.iteritems(token_groups)
        ]
Exemple #2
0
    def load_snippets(cls, src_path, violation_lines):
        """
        Load snippets from the file at `src_path` to show
        violations on lines in the list `violation_lines`
        (list of line numbers, starting at index 0).

        The file at `src_path` should be a text file (not binary).

        Returns a list of `Snippet` instances.

        Raises an `IOError` if the file could not be loaded.
        """
        # Load the contents of the file
        with openpy.open(src_path) as src_file:
            contents = src_file.read()

        # Convert the source file to unicode (Python < 3)
        if isinstance(contents, six.binary_type):
            contents = contents.decode('utf-8', 'replace')

        # Construct a list of snippet ranges
        src_lines = contents.split('\n')
        snippet_ranges = cls._snippet_ranges(len(src_lines), violation_lines)

        # Parse the source into tokens
        token_stream = cls._parse_src(contents, src_path)

        # Group the tokens by snippet
        token_groups = cls._group_tokens(token_stream, snippet_ranges)

        return [
            Snippet(tokens, src_path, start, violation_lines)
            for (start, _), tokens in six.iteritems(token_groups)
        ]
Exemple #3
0
 def fload(self):
     """Load file object."""
     # read data and parse into blocks
     if hasattr(self, 'fobj') and self.fobj is not None:
        self.fobj.close()
     if hasattr(self.src, "read"):
          # It seems to be a file or a file-like object
         self.fobj = self.src
     else:
          # Assume it's a string or something that can be converted to one
         self.fobj = openpy.open(self.fname)
Exemple #4
0
 def fload(self):
     """Load file object."""
     # read data and parse into blocks
     if hasattr(self, 'fobj') and self.fobj is not None:
         self.fobj.close()
     if hasattr(self.src, "read"):
         # It seems to be a file or a file-like object
         self.fobj = self.src
     else:
         # Assume it's a string or something that can be converted to one
         self.fobj = openpy.open(self.fname)