<commit_before># Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Sphinx extension to add a ``task`` directive

This directive allows sharing code between documentation and provisioning code.

.. code-block:: rest

   .. task:: name_of_task

``name_of_task`` must the name of a task in ``flocker.provision._tasks``,
without the ``task_`` prefix. A task must take a single runner argument.
"""

from inspect import getsourcefile
from docutils.parsers.rst import Directive
from docutils import nodes
from docutils.statemachine import StringList

from flocker import __version__ as version

from flocker.docs import get_installable_version
from flocker.docs.version_extensions import PLACEHOLDER

from . import _tasks as tasks
from ._install import Run, Sudo, Comment, Put


def run(command):
    return command.command


def sudo(command):
    return "sudo %s" % (command.command,)


def comment(command):
    return "# %s" % (command.comment)


def put(command):
    return [
        "cat <<EOF > %s" % (command.path,),
    ] + command.content.splitlines() + [
        "EOF",
    ]


HANDLERS = {
    Run: run,
    Sudo: sudo,
    Comment: comment,
    Put: put,
}


class TaskDirective(Directive):
    """
    Implementation of the C{task} directive.
    """
    required_arguments = 1
    optional_arguments = 1
    final_argument_whitespace = True

    option_spec = {
        'prompt': str
    }

    def run(self):
        task = getattr(tasks, 'task_%s' % (self.arguments[0],))
        prompt = self.options.get('prompt', '$')
        if len(self.arguments) > 1:
            latest = get_installable_version(version)
            task_arguments = [item.replace(PLACEHOLDER, latest) for
                              item in self.arguments[1].split()]
        else:
            task_arguments = []

        commands = task(*task_arguments)
        lines = ['.. prompt:: bash %s,> auto' % (prompt,), '']
<commit_msg>Comment about replacing latest version<commit_after># Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Sphinx extension to add a ``task`` directive

This directive allows sharing code between documentation and provisioning code.

.. code-block:: rest

   .. task:: name_of_task

``name_of_task`` must the name of a task in ``flocker.provision._tasks``,
without the ``task_`` prefix. A task must take a single runner argument.
"""

from inspect import getsourcefile
from docutils.parsers.rst import Directive
from docutils import nodes
from docutils.statemachine import StringList

from flocker import __version__ as version

from flocker.docs import get_installable_version
from flocker.docs.version_extensions import PLACEHOLDER

from . import _tasks as tasks
from ._install import Run, Sudo, Comment, Put


def run(command):
    return command.command


def sudo(command):
    return "sudo %s" % (command.command,)


def comment(command):
    return "# %s" % (command.comment)


def put(command):
    return [
        "cat <<EOF > %s" % (command.path,),
    ] + command.content.splitlines() + [
        "EOF",
    ]


HANDLERS = {
    Run: run,
    Sudo: sudo,
    Comment: comment,
    Put: put,
}


class TaskDirective(Directive):
    """
    Implementation of the C{task} directive.
    """
    required_arguments = 1
    optional_arguments = 1
    final_argument_whitespace = True

    option_spec = {
        'prompt': str
    }

    def run(self):
        task = getattr(tasks, 'task_%s' % (self.arguments[0],))
        prompt = self.options.get('prompt', '$')
        if len(self.arguments) > 1:
            # Some tasks can include the latest installable version as (part
            # of) an argument. This replaces a placeholder with that version.
            latest = get_installable_version(version)
            task_arguments = [item.replace(PLACEHOLDER, latest) for
                              item in self.arguments[1].split()]
        else:
            task_arguments = []

        commands = task(*task_arguments)
        lines = ['.. prompt:: bash %s,> auto' % (prompt,), '']

{'fraction_non_alphanumeric': 0.10825506673257539, 'fraction_numerical': 0.002471576866040534, 'mean_word_length': 2.9026036644165862, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7459145', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1173, 'n_words': 370}