Beispiel #1
0
    def __init__(self, fp, line_span=None, language_hints=(), **kwargs):
        language = ''

        for hint in language_hints:
            language = get_language(hint)

            if language:
                break

        if not language:
            try:
                language = get_language(fp.name)
            except AttributeError:
                pass

        content, _, file_language = guess_file_traits(fp.read())

        language = file_language or language
        lines = content.split('\n')

        super().__init__(prefix=f'```{language}', suffix='```', **kwargs)

        if line_span:
            line_span = sorted(line_span)

            if min(line_span) < 1 or max(line_span) > len(lines):
                raise ValueError("Linespan goes out of bounds.")

            lines = lines[line_span[0] - 1:line_span[1]]

        for line in lines:
            self.add_line(line)
Beispiel #2
0
    def __init__(self, fp, line_span=None, language_hints=(), **kwargs):
        language = ''

        for hint in language_hints:
            language = get_language(hint)

            if language:
                break

        if not language:
            try:
                language = get_language(fp.name)
            except AttributeError:
                pass

        raw_content = fp.read()

        try:
            lines = raw_content.decode('utf-8').split('\n')
        except UnicodeDecodeError as exc:
            # This file isn't UTF-8.

            #  By Python and text-editor convention,
            # there may be a hint as to what the actual encoding is
            # near the start of the file.

            encoding_match = self.__encoding_regex.search(raw_content[:128])

            if encoding_match:
                encoding = encoding_match.group(1)
            else:
                raise exc

            try:
                lines = raw_content.decode(
                    encoding.decode('utf-8')).split('\n')
            except UnicodeDecodeError as exc2:
                raise exc2 from exc

        del raw_content

        # If the first line is a shebang,
        if lines[0].startswith('#!'):
            # prioritize its declaration over the extension.
            language = get_language(lines[0]) or language

        super().__init__(prefix=f'```{language}', suffix='```', **kwargs)

        if line_span:
            line_span = sorted(line_span)

            if min(line_span) < 1 or max(line_span) > len(lines):
                raise ValueError("Linespan goes out of bounds.")

            lines = lines[line_span[0] - 1:line_span[1]]

        for line in lines:
            self.add_line(line)
Beispiel #3
0
    def __init__(self, fp, line_span=None, **kwargs):
        language = ""

        try:
            language = get_language(fp.name)
        except AttributeError:
            pass

        raw_content = fp.read()

        try:
            lines = raw_content.decode("utf-8").split("\n")
        except UnicodeDecodeError as exc:
            # This file isn't UTF-8.

            #  By Python and text-editor convention,
            # there may be a hint as to what the actual encoding is
            # near the start of the file.

            encoding_match = self.__encoding_regex.search(raw_content[:128])

            if encoding_match:
                encoding = encoding_match.group(1)
            else:
                raise exc

            try:
                lines = raw_content.decode(encoding.decode("utf-8")).split("\n")
            except UnicodeDecodeError as exc2:
                raise exc2 from exc

        del raw_content

        first_line = lines[0]

        # If the first line is a shebang,
        if first_line.startswith("#!"):
            # prioritize its declaration over the extension.
            language = get_language(first_line) or language

        super().__init__(prefix=f"```{language}", suffix="```", **kwargs)

        line_number = len(lines)

        if line_span:
            line_span = sorted(line_span)
            if min(line_span) < 1 or max(line_span) > line_number:
                raise ValueError("Linespan goes out of bounds.")
            lines = lines[line_span[0] - 1 : line_span[1]]

        for line in lines:
            self.add_line(line)
Beispiel #4
0
    async def jsk_curl(self, ctx: commands.Context, url: str):
        """
        Download and display a text file from the internet.

        This command is similar to jsk cat, but accepts a URL.
        """

        # remove embed maskers if present
        url = url.lstrip("<").rstrip(">")

        async with ReplResponseReactor(ctx.message):
            async with aiohttp.ClientSession() as session:
                async with session.get(url) as response:
                    data = await response.read()
                    hints = (response.content_type, url)
                    code = response.status

            if not data:
                return await ctx.send(
                    f"HTTP response was empty (status code {code}).")

            # Guild's advertised limit minus 1KiB for the HTTP content
            filesize_threshold = (ctx.guild.filesize_limit
                                  if ctx.guild else 8 * 1024 * 1024) - 1024

            if len(data) < filesize_threshold:
                # Shallow language detection
                language = None

                for hint in hints:
                    language = get_language(hint)

                    if language:
                        break

                await ctx.send(
                    file=discord.File(filename=f"response.{language or 'txt'}",
                                      fp=io.BytesIO(data)))
            else:
                try:
                    paginator = WrappedFilePaginator(io.BytesIO(data),
                                                     language_hints=hints,
                                                     max_size=1985)
                except UnicodeDecodeError:
                    return await ctx.send(
                        f"Couldn't determine the encoding of the response. (status code {code})"
                    )
                except ValueError as exc:
                    return await ctx.send(
                        f"Couldn't read response (status code {code}), {exc}")

                interface = PaginatorInterface(ctx.bot,
                                               paginator,
                                               owner=ctx.author)
                await interface.send_to(ctx)
Beispiel #5
0
 def test_hljs(self):
     self.assertEqual(get_language('base.py'), 'py')
     self.assertEqual(get_language('config.yml'), 'yml')
     self.assertEqual(get_language('requirements.txt'), '')
     self.assertEqual(get_language('#!/usr/bin/env python'), 'python')
     self.assertEqual(get_language('#!/usr/bin/unknown'), '')
Beispiel #6
0
def test_hljs(filename, language):
    assert get_language(filename) == language