Example #1
0
    def _substitute_vars(self, message):
        # edit message to replace {count_name} with count number
        for variable in re.findall('{(.*?)}', message):
            variable = variable.strip()

            # if it's a count name, return the count
            found_count = counts.get_count(variable)
            if found_count is not None:
                message = message.replace('{{{}}}'.format(variable), str(found_count))
                continue

            # if it's a list, return a random one or use the index
            list_params = variable.split()
            list_name = list_params[0]
            if list_name not in [list_dict['name'] for list_dict in lists.list_lists()]:
                continue

            index = None
            try:
                # accounting for negative indexes
                pulled_index = int(list_params[1])-1
                if pulled_index < lists.get_list_size(name=list_name):
                    index = pulled_index
            except (ValueError, IndexError):
                pass

            found_item, index = lists.get_list_item(name=list_name, index=index)
            if found_item is not None:
                message = message.replace('{{{}}}'.format(variable), found_item)
                continue

        return message
Example #2
0
def list_lists_get():
    if request.method == 'GET':
        try:
            all_lists = lists.list_lists()
        except Exception as e:
            raise InvalidUsage(str(e))
        return jsonify(all_lists)
    else:
        data = request.get_json()
        try:
            lists.import_lists(data)
        except Exception as e:
            raise InvalidUsage(str(e))
        return jsonify({'message': 'Lists imported'})
Example #3
0
def test_remove_list(import_lists):
    lists.remove_list('list3')
    assert lists.list_lists() == LISTS_IMPORT
Example #4
0
def test_import_export(import_lists):
    assert lists.list_lists() == LISTS_IMPORT
Example #5
0
 def list_lists(self):
     all_lists = ', '.join([count['name'] for count in lists.list_lists()])
     if all_lists:
         self.chat('Lists: {}'.format(all_lists))