<commit_before>""" smashlib.prompt.component
"""

import os
import addict
from goulash._fabric import qlocal
from smashlib.util.reflect import from_dotpath


class PromptError(ValueError):
    pass


class PromptComponent(addict.Dict):

    TYPES = 'python shell env literal'.split()

    def __call__(self):
        if self.type == 'python':
            fxn = from_dotpath(self.value)
            if not callable(fxn):
                err = ("prompt component {0} references "
                       "something that's not callable!")
                err = err.format(dict(self))
                raise PromptError(err)
            result = fxn()
        elif self.type == 'literal':
            result = self.value
        elif self.type == 'shell':
            result = qlocal(self.value, capture=True).strip()
        elif self.type == 'env':
            if self.value.startswith('$'):
                value = self.value[1:]
            else:
                value = self.value
            result = os.environ.get(value, '')
        else:
            err = 'invalid prompt component: {0}'
            err = err.format(self)
            raise Exception(err)
        if result and \
                self.space_margins and \
                self.space_margins.lower() == 'true':<commit_msg>fix minor bug in parsing prompt.json<commit_after>""" smashlib.prompt.component
"""

import os
import addict
from goulash._fabric import qlocal
from smashlib.util.reflect import from_dotpath


class PromptError(ValueError):
    pass


class PromptComponent(addict.Dict):

    TYPES = 'python shell env literal'.split()

    def __call__(self):
        if self.type == 'python':
            fxn = from_dotpath(self.value)
            if not callable(fxn):
                err = ("prompt component {0} references "
                       "something that's not callable!")
                err = err.format(dict(self))
                raise PromptError(err)
            result = fxn()
        elif self.type == 'literal':
            result = self.value
        elif self.type == 'shell':
            result = qlocal(self.value, capture=True).strip()
        elif self.type == 'env':
            if self.value.startswith('$'):
                value = self.value[1:]
            else:
                value = self.value
            result = os.environ.get(value, '')
        else:
            err = 'invalid prompt component: {0}'
            err = err.format(self)
            raise Exception(err)
        if result and self.space_margins:
{'fraction_non_alphanumeric': 0.10974643423137877, 'fraction_numerical': 0.002377179080824089, 'mean_word_length': 1.7777777777777777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3839450', 'n_tokens_mistral': 721, 'n_tokens_neox': 683, 'n_words': 179}