<commit_before># TODO definitly need a session object
# with hist & expiry & to_str & to_json etc
import yaml
import json
from vumi.errors import VumiError


class VumiSession():
    key = None
    decision_tree = None

    def __init__(self, **kwargs):
        pass

    def set_decision_tree(self, decision_tree):
        self.decision_tree = decision_tree

    def get_decision_tree(self):
        return self.decision_tree


class DecisionTree():
    pass


class TemplatedDecisionTree(DecisionTree):
    template = None
    template_current = None
    template_history = []

    def load_yaml_template(self, yaml_string):
        self.template = yaml.load(yaml_string)
        self.template_current = self.template.get('__start__')

    def get_template(self):
        return self.template

    def get_data_source(self):
        if self.template:
            if self.template.get('__data__'):
                return {"url":self.template['__data__'].get('url'),
                        "username":self.template['__data__'].get('username'),
                        "password":self.template['__data__'].get('password')}
        return {"username":None, "password":<PASSWORD>, "url":None}

    def get_dummy_data(self):
        if self.template:
            if self.template.get('__data__'):
                return self.template['__data__'].get('json')
        return None





class PopulatedDecisionTree(TemplatedDecisionTree):
    data = None
    # So that I can modify the original data, data_current must
    # be stored by reference as a list/dict, index/key pair
    data_current = ([None],0)
    data_history = []

    def load_json_data(self, json_string):
        self.data = json.loads(json_string)

    def load_dummy_data(self):
        self.load_json_data(self.get_dummy_data())

    def dump_json_data(self):
        return json.dumps(self.data)

    def get_data(self):
        return self.data


    def resolve_dc(self):
        return self.data_current[0][self.data_current[1]]

    def select_dc(self, index_key):
        return (self.resolve_dc(), index_key)

    def update_dc(self, new_value):
        self.data_current[0][self.data_current[1]] = new_value


class TraversedDecisionTree(PopulatedDecisionTree):
    echo = False
    completed = False
    language = "english"

    # The data will be a nested data-structure of a sort that can
    # be deserialized from a JSON string.

    # This means that when traversing it the current object will be one of:
    #     dict    -> in which case the template should auto-select the correct key
    #     list    -> in which case the user should be asked from displayed options
    #     boolean -> as for list, just yes/no questions only
    #     number  -> in which case the user should be prompted for a value
    #     string  -> in which case the user should be prompted for text

    # NB. User entered strings (which include things like dates),
        # should be avoided where possible.


    def resolve_default(self, default):
        return default


    def is_completed(self):
        return self.completed

    def echo_on(self):
        self.echo = True

    def set_language(self, language):
        self.language = language


    def dumps(self, level=1, serialize=repr):
        def wrap(title, serialize, obj):
            return "\n****"+str(title)+"****\n" \
                    + serialize(obj)
        s = ""
        if level >= 1:
            s += wrap("TEMPLATE", serialize, self.template)
        if level >= 0:
            s += wrap("TEMPLATE_CURRENT", serialize, self.template_current)
        if level >= 2:
            s += wrap("TEMPLATE_HISTORY", serialize, self.template_history)
        if level >= 1:
            s += wrap("DATA", serialize, self.data)
        if level >= 0:
            s += wrap("DATA_CURRENT", serialize, self.data_current)
        if level >= 2:
            s += wrap("DATA_HISTORY", serialize, self.data_history)
        return s


    def select(self, template, data):
        self.template_history.append(self.template_current)
        self.data_history.append(self.data_current)
        self.template_current = template
        self.data_current = data


    def go_back(self):
        try:
            self.template_current = self.template_history.pop()
            self.data_current = self.data_history.pop()
            self.completed = False
        except:
            pass


    def go_up(self):
        old_data_current_0 = self.data_current[0]
        self.go_back()
        while old_data_current_0 == self.data_current[0] \
                and len(self.data_history) > 0:
            self.go_back()


    def start(self):
        greeting = ''
        if self.template_current.get('display'):
            greeting += self.template_current['display'].get(
                    self.language, '')
        if self.echo:
            print "\n", greeting
        if not self.template:<commit_msg>try autoselect<commit_after># TODO definitly need a session object
# with hist & expiry & to_str & to_json etc
import yaml
import json
from vumi.errors import VumiError


class VumiSession():
    key = None
    decision_tree = None

    def __init__(self, **kwargs):
        pass

    def set_decision_tree(self, decision_tree):
        self.decision_tree = decision_tree

    def get_decision_tree(self):
        return self.decision_tree


class DecisionTree():
    pass


class TemplatedDecisionTree(DecisionTree):
    template = None
    template_current = None
    template_history = []

    def load_yaml_template(self, yaml_string):
        self.template = yaml.load(yaml_string)
        self.template_current = self.template.get('__start__')

    def get_template(self):
        return self.template

    def get_data_source(self):
        if self.template:
            if self.template.get('__data__'):
                return {"url":self.template['__data__'].get('url'),
                        "username":self.template['__data__'].get('username'),
                        "password":self.template['__data__'].get('password')}
        return {"username":None, "password":<PASSWORD>, "url":None}

    def get_dummy_data(self):
        if self.template:
            if self.template.get('__data__'):
                return self.template['__data__'].get('json')
        return None





class PopulatedDecisionTree(TemplatedDecisionTree):
    data = None
    # So that I can modify the original data, data_current must
    # be stored by reference as a list/dict, index/key pair
    data_current = ([None],0)
    data_history = []

    def load_json_data(self, json_string):
        self.data = json.loads(json_string)

    def load_dummy_data(self):
        self.load_json_data(self.get_dummy_data())

    def dump_json_data(self):
        return json.dumps(self.data)

    def get_data(self):
        return self.data

    def resolve_dc(self):
        return self.data_current[0][self.data_current[1]]

    def update_dc(self, new_value):
        self.data_current[0][self.data_current[1]] = new_value


class TraversedDecisionTree(PopulatedDecisionTree):
    echo = False
    completed = False
    language = "english"

    # The data will be a nested data-structure of a sort that can
    # be deserialized from a JSON string.

    # This means that when traversing it the current object will be one of:
    #     dict    -> in which case the template should auto-select the correct key
    #     list    -> in which case the user should be asked from displayed options
    #     boolean -> as for list, just yes/no questions only
    #     number  -> in which case the user should be prompted for a value
    #     string  -> in which case the user should be prompted for text

    # NB. User entered strings (which include things like dates),
        # should be avoided where possible.


    def resolve_default(self, default):
        return default


    def is_completed(self):
        return self.completed

    def echo_on(self):
        self.echo = True

    def set_language(self, language):
        self.language = language


    def dumps(self, level=1, serialize=repr):
        def wrap(title, serialize, obj):
            return "\n****"+str(title)+"****\n" \
                    + serialize(obj)
        s = ""
        if level >= 1:
            s += wrap("TEMPLATE", serialize, self.template)
        if level >= 0:
            s += wrap("TEMPLATE_CURRENT", serialize, self.template_current)
        if level >= 2:
            s += wrap("TEMPLATE_HISTORY", serialize, self.template_history)
        if level >= 1:
            s += wrap("DATA", serialize, self.data)
        if level >= 0:
            s += wrap("DATA_CURRENT", serialize, self.data_current)
        if level >= 2:
            s += wrap("DATA_HISTORY", serialize, self.data_history)
        return s


    def select(self, template, data):
        self.template_history.append(self.template_current)
        self.data_history.append(self.data_current)
        self.template_current = template
        self.data_current = data


    def try_auto_select(self):
        return False


    def go_back(self):
        try:
            self.template_current = self.template_history.pop()
            self.data_current = self.data_history.pop()
            self.completed = False
        except:
            pass


    def go_up(self):
        old_data_current_0 = self.data_current[0]
        self.go_back()
        while old_data_current_0 == self.data_current[0] \
                and len(self.data_history) > 0:
            self.go_back()


    def start(self):
        greeting = ''
        if self.template_current.get('display'):
            greeting += self.template_current['display'].get(
                    self.language, '')
        if self.echo:
            print "\n", greeting
        if not self.template:
{'fraction_non_alphanumeric': 0.1008838768668089, 'fraction_numerical': 0.0034542314335060447, 'mean_word_length': 2.351719441607082, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 29, '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': '457507', 'n_tokens_mistral': 2914, 'n_tokens_neox': 2771, 'n_words': 742}