-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathessence.py
158 lines (124 loc) · 4.92 KB
/
essence.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from collections import defaultdict
import logging
import json
import itertools
from omegaconf import OmegaConf
from config import Config, Prompts
logger = logging.getLogger(__name__)
DEFAULT_PROPERTY_TYPE = 'default'
# Prompts = OmegaConf.load('prompts.yaml')
class InstanceCounterMeta(type):
""" Metaclass to make instance counter not share count with descendants
"""
def __init__(cls, name, bases, attrs):
super().__init__(name, bases, attrs)
cls._ids = itertools.count(1)
class Essence(metaclass=InstanceCounterMeta):
def __init__(self, name, description, essence_name):
self.id = next(self.__class__._ids)
self.name = name if name is not None else f"{essence_name}-{self.id}"
self.description = description
self.property = defaultdict(list)
self._init_essence(essence_name)
def _init_essence(self, essence_name=None):
# if essence_name is None:
# essence_name = self.__class__.__name__
self.essence_name = essence_name
setattr(self, f"add_{essence_name}", self.add_property)
setattr(self, f"get_{essence_name}", self.get_property)
def _check_type(self, type):
if type is None:
if len(self.property) == 1:
return list(self.property.keys())[0]
else:
logger.warning(f"Not specify type for {self.name}, use default type {DEFAULT_PROPERTY_TYPE}")
return DEFAULT_PROPERTY_TYPE
else:
return type
def add_property(self, type=None, prop=None):
type = self._check_type(type)
# return a function if prop is None
if prop is None:
return lambda prop: self.property[type].append(prop)
if isinstance(prop, list):
self.property[type].extend(prop)
else:
self.property[type].append(prop)
def get_property(self, type=None):
result = self.property[type]
if len(result) == 1:
return result[0]
else:
return result
def to_json(self):
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
def from_json(self, json_str):
json_dict = json.loads(json_str)
self.__dict__.update(json_dict)
return self
def get_essence_typeid(self):
return f"[{self.essence_name}-{self.id}:{self.name}]"
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'description': self.description,
'property': self.property,
'essence_name': self.essence_name,
'essence_typeid': self.get_essence_typeid(),
**{
type: self.get_property(type) for type in self.property
}
}
def to_prompt(self):
# NOTE: SUBCLASS SHOULD OVERRIDE THIS METHOD
return getattr(Prompts, self.__class__.__name__.lower()).format(self.to_dict())
# StoryLine contains Dialog List
class StoryLine(Essence):
def __init__(self, name, description):
super().__init__(name, description, 'storyline')
# DialogTemplate =
class Dialog(Essence):
def __init__(self, description, place, characters, content, summary, next_place=None):
super().__init__(None, description, 'dialog')
self.add_property('place', place)
self.add_property('characters', characters)
self.add_property('content', content)
self.add_property('summary', summary)
self.add_property('next_place', next_place)
def get_essence_typeid(self):
return f"[{self.essence_name}-{self.id}]"
class Character(Essence):
def __init__(self, name, description, appearance, personality, background, relationship, dialog, summary):
super().__init__(name, description, 'character')
self.add_property('appearance', appearance)
self.add_property('personality', personality)
self.add_property('background', background)
self.add_property('relationship', relationship)
self.add_property('dialog', dialog)
self.add_property('summary', summary)
class Place(Essence):
def __init__(self, name, description, dialog, summary):
super().__init__(name, description, 'place')
self.add_property('dialog', dialog)
self.add_property('summary', summary)
class Flag(Essence):
def __init__(self, name, description):
super().__init__(name, description, 'flag')
class EssenceDB:
def __init__(self):
self.storyline = []
self.character = []
self.place = []
self.flag = []
self.dialog = []
def query(self, essence_type, essence_name):
return getattr(self, essence_type).get(essence_name)
def add_dialog(self, dialog):
# TODO
# parse dialog
# add dialog to place
# add dialog to character
# add dialog to storyline
# add dialog to dialog
pass