aboutsummaryrefslogtreecommitdiff
path: root/modules/styles.py
blob: 85b78fad9b4a72e8c5ba3b9510593e3ad1be03c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import csv
import os.path
from collections import namedtuple

PromptStyle = namedtuple("PromptStyle", ["name", "text"])


def load_styles(filename):
    res = {"None": PromptStyle("None", "")}

    if os.path.exists(filename):
        with open(filename, "r", encoding="utf8", newline='') as file:
            reader = csv.DictReader(file)

            for row in reader:
                res[row["name"]] = PromptStyle(row["name"], row["text"])

    return res


def apply_style_text(style_text, prompt):
    if style_text == "":
        return prompt

    return prompt + ", " + style_text if prompt else style_text


def apply_style(p, style):
    if type(p.prompt) == list:
        p.prompt = [apply_style_text(style.text, x) for x in p.prompt]
    else:
        p.prompt = apply_style_text(style.text, p.prompt)


def save_style(filename, style):
    with open(filename, "a", encoding="utf8", newline='') as file:
        atstart = file.tell() == 0

        writer = csv.DictWriter(file, fieldnames=["name", "text"])

        if atstart:
            writer.writeheader()

        writer.writerow({"name": style.name, "text": style.text})