annotate dialogueparsers.py @ 1:4912a6f97c52

Various improvements to the build process including support for self-contained builds. * Note that despite all of these changes PARPG still does not run because asset paths are not standardized, * Modified the SCons script so that by default running `scons` with no arguments creates a self-contained "build" under a build subdirectory to make in-source testing easier. To install PARPG, use `scons install` instead. * Got rid of the binary launcher and replaced it with a shell script for unix and a batch script for Windows (batch script is untested). The binary turned out to be too much trouble to maintain. * Modified the parpg.settings module and parpg.main entry script so that PARPG searches through several default search paths for configuration file(s). PARPG thus no longer crashes if it can't find a configuration file in any particular search path, but will crash it if can't find any configuration files. * Paths supplied to parpg.main are now appended as search paths for the configuration file(s). * Changed the default configuration file name to "parpg.cfg" to simplify searches. * Created the site_scons directory tree where SCons extensions and tools should be placed. * Created a new SCons builder, CopyRecurse, which can copy only certain files and folders from a directory tree using filters (files and folders that start with a leading dot "." e.g. ".svn" are ignored by default). * Added the CPython SCons tool (stands for Compile-Python - I didn't name it!), which provides the InstallPython builder for pre-compiling python sources before they are installed. However, it is currently broken and only installs the python sources.
author M. George Hansen <technopolitica@gmail.com>
date Tue, 31 May 2011 02:46:20 -0700
parents 7a89ea5404b1
children 06145a6ee387
rev   line source
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
1 # This file is part of PARPG.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
2 #
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
3 # PARPG is free software: you can redistribute it and/or modify
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
4 # it under the terms of the GNU General Public License as published by
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
5 # the Free Software Foundation, either version 3 of the License, or
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
6 # (at your option) any later version.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
7 #
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
8 # PARPG is distributed in the hope that it will be useful,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
11 # GNU General Public License for more details.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
12 #
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
13 # You should have received a copy of the GNU General Public License
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/>.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
15 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
16 Contains classes for parsing and validating L{Dialogues<Dialogue>} and other
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
17 dialogue-related data.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
18
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
19 @TODO Technomage 2010-11-13: Exception handling + validation needs work.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
20 Currently YAML files are only crudely validated - the code assumes that
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
21 the file contains valid dialogue data, and if that assumption is
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
22 violated and causes the code to raise any TypeErrors, AttributeErrors or
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
23 ValueErrors the code then raises a DialogueFormatError with the
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
24 original (and mostly unhelpful) error message.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
25 @TODO Technomage 2010-11-13: Support reading and writing unicode.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
26 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
27 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
28 from cStringIO import StringIO
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
29 except ImportError:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
30 from StringIO import StringIO
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
31 from collections import Sequence
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
32 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
33 from collections import OrderedDict
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
34 except ImportError:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
35 # Python version 2.4-2.6 doesn't have the OrderedDict
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
36 from parpg.common.ordereddict import OrderedDict
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
37 import re
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
38 import textwrap
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
39
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
40 import yaml
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
41
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
42 from parpg import COPYRIGHT_HEADER
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
43 from parpg.dialogue import (Dialogue, DialogueSection, DialogueResponse,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
44 DialogueGreeting)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
45 from parpg.dialogueactions import DialogueAction
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
46
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
47 import logging
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
48 logger = logging.getLogger('dialogueparser')
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
49
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
50 class DialogueFormatError(Exception):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
51 """Exception thrown when the DialogueParser has encountered an error."""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
52
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
53
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
54 class AbstractDialogueParser(object):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
55 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
56 Abstract base class defining the interface for parsers responsible for
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
57 constructing a L{Dialogue} from its serialized representation.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
58 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
59 def load(self, stream):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
60 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
61 Parse a stream and attempt to construct a new L{Dialogue} instance from
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
62 its serialized representation.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
63
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
64 @param stream: open stream containing the serialized representation of
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
65 a Dialogue.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
66 @type stream: BufferType
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
67 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
68 raise NotImplementedError('AbstractDialogueParser subclasses must '
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
69 'override the load method.')
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
70
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
71 def dump(self, dialogue, stream):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
72 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
73 Serialize a L{Dialogue} instance and dump it to an open stream.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
74
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
75 @param dialogue: dialogue to serialize.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
76 @type dialogue: L{Dialogue}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
77 @param stream: open stream into which the serialized L{Dialogue} should
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
78 be dumped.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
79 @type stream: BufferType
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
80 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
81 raise NotImplementedError('AbstractDialogueParser subclasses must '
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
82 'override the dump method.')
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
83
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
84 def validate(self, stream):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
85 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
86 Parse a stream and verify that it contains a valid serialization of a
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
87 L{Dialogue instance}.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
88
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
89 @param stream: stream containing the serialized representation of a
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
90 L{Dialogue}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
91 @type stream: BufferType
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
92 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
93 raise NotImplementedError('AbstractDialogueParser subclasses must '
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
94 'override the validate method.')
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
95
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
96
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
97 class YamlDialogueParser(AbstractDialogueParser):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
98 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
99 L{AbstractDialogueParser} subclass responsible for parsing dialogues
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
100 serialized in YAML.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
101 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
102 logger = logging.getLogger('dialogueparser.OldYamlDialogueParser')
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
103
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
104 def load(self, stream, loader_class=yaml.Loader):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
105 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
106 Parse a YAML stream and attempt to construct a new L{Dialogue}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
107 instance.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
108
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
109 @param stream: stream containing the serialized YAML representation of
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
110 a L{Dialogue}.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
111 @type stream: BufferType
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
112 @param loader_class: PyYAML loader class to use for reading the
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
113 serialization.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
114 @type loader_class: yaml.BaseLoader subclass
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
115 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
116 loader = loader_class(stream)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
117 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
118 dialogue = \
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
119 self._constructDialogue(loader, loader.get_single_node())
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
120 except (AssertionError,) as error:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
121 raise DialogueFormatError(str(error))
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
122 return dialogue
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
123
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
124 def dump(self, dialogue, output_stream, dumper_class=yaml.Dumper):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
125 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
126 Serialize a L{Dialogue} instance as YAML and dump it to an open stream.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
127
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
128 @param dialogue: dialogue to serialize.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
129 @type dialogue: L{Dialogue}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
130 @param stream: open stream into which the serialized L{Dialogue} should
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
131 be dumped.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
132 @type stream: BufferType
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
133 @param dumper_class: PyYAML dumper class to use for formatting the
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
134 serialization.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
135 @type dumper_class: yaml.BaseDumper subclass
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
136 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
137 intermediate_stream = StringIO()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
138 # KLUDE Technomage 2010-11-16: The "width" argument seems to be broken,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
139 # as it doesn't take into about current line indentation and fails
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
140 # to correctly wrap at word boundaries.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
141 dumper = dumper_class(intermediate_stream, default_flow_style=False,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
142 indent=4, width=99999, line_break='\n',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
143 allow_unicode=True, explicit_start=True,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
144 explicit_end=True, tags=False)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
145 dialogue_node = self._representDialogue(dumper, dialogue)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
146 dumper.open()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
147 dumper.serialize(dialogue_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
148 dumper.close()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
149 file_contents = intermediate_stream.getvalue()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
150
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
151 file_contents = re.sub(r'(\n|\r|\r\n)(\s*)(GOTO: .*)', r'\1\2\3\1\2',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
152 file_contents)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
153 lines = file_contents.splitlines()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
154 max_line_length = 76 # 79 - 3 chars for escaping newlines
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
155 for i in range(len(lines)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
156 line = lines[i]
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
157 match = re.match(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
158 r'^(\s*(?:-\s+)?)(SAY|REPLY|CONDITION):\s+"(.*)"$',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
159 line
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
160 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
161 if (match and len(line) > max_line_length):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
162 # Wrap long lines for readability.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
163 initial_indent = len(match.group(1))
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
164 subsequent_indent = initial_indent + 4
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
165 text_wrapper = textwrap.TextWrapper(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
166 max_line_length,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
167 subsequent_indent=' ' * subsequent_indent,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
168 break_long_words=False,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
169 break_on_hyphens=False
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
170 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
171 new_lines = text_wrapper.wrap(line)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
172 new_lines = (
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
173 new_lines[:1] + [re.sub(r'^(\s*) (.*)$', r'\1\ \2', l)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
174 for l in new_lines[1:]]
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
175 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
176 lines[i] = '\\\n'.join(new_lines)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
177
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
178 output_stream.write(COPYRIGHT_HEADER)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
179 output_stream.write('\n'.join(lines))
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
180
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
181
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
182 def _representDialogue(self, dumper, dialogue):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
183 dialogue_node = dumper.represent_dict({})
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
184 dialogue_dict = OrderedDict()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
185 dialogue_dict['NPC_NAME'] = dialogue.npc_name
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
186 dialogue_dict['AVATAR_PATH'] = dialogue.avatar_path
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
187 dialogue_dict['DEFAULT_GREETING'] = \
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
188 self._representDialogueSection(dumper,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
189 dialogue.default_greeting)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
190 # NOTE Technomage 2010-11-16: Dialogue stores its sections in an
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
191 # OrderedDict, so a round-trip load, dump, and load will preserve
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
192 # the order of DialogueSections.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
193 if (len(dialogue.greetings) > 0):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
194 greetings_list_node = dumper.represent_list([])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
195 greetings_list = greetings_list_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
196 for greeting in dialogue.greetings:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
197 greeting_node = \
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
198 self._representRootDialogueSection(dumper, greeting)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
199 greetings_list.append(greeting_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
200 dialogue_dict['GREETINGS'] = greetings_list_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
201 if (len(dialogue.setions) > 0):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
202 sections_list_node = dumper.represent_list([])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
203 sections_list = sections_list_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
204 for section in dialogue.sections.values():
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
205 section_node = self._representDialogueSection(dumper, section)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
206 sections_list.append(section_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
207 dialogue_dict['SECTIONS'] = sections_list_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
208
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
209 for key, value in dialogue_dict.items():
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
210 if (isinstance(key, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
211 key_node = key
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
212 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
213 key_node = dumper.represent_data(key)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
214 if (isinstance(value, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
215 value_node = value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
216 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
217 value_node = dumper.represent_data(value)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
218 dialogue_node.value.append((key_node, value_node))
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
219 return dialogue_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
220
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
221 def _representRootDialogueSection(self, dumper, greeting):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
222 greeting_node = dumper.represent_dict({})
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
223 greeting_dict = OrderedDict()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
224 greeting_dict['ID'] = greeting.id
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
225 greeting_dict['CONDITION'] = dumper.represent_scalar(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
226 'tag:yaml.org,2002:str',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
227 greeting.condition,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
228 style='"'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
229 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
230 for key, value in greeting_dict.items():
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
231 if (isinstance(key, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
232 key_node = key
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
233 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
234 key_node = dumper.represent_data(key)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
235 if (isinstance(value, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
236 value_node = value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
237 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
238 value_node = dumper.represent_data(value)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
239 greeting_node.value.append((key_node, value_node))
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
240 return greeting_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
241
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
242 def _representDialogueSection(self, dumper, dialogue_section):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
243 section_node = dumper.represent_dict({})
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
244 section_dict = OrderedDict() # OrderedDict is required to preserve
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
245 # the order of attributes.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
246 section_dict['ID'] = dialogue_section.id
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
247 # KLUDGE Technomage 2010-11-16: Hard-coding the tag like this could be
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
248 # a problem when writing unicode.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
249 section_dict['SAY'] = dumper.represent_scalar('tag:yaml.org,2002:str',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
250 dialogue_section.text,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
251 style='"')
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
252 actions_list_node = dumper.represent_list([])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
253 actions_list = actions_list_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
254 for action in dialogue_section.actions:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
255 action_node = self._representDialogueAction(dumper, action)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
256 actions_list.append(action_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
257 if (actions_list):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
258 section_dict['ACTIONS'] = actions_list_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
259 responses_list_node = dumper.represent_list([])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
260 responses_list = responses_list_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
261 for response in dialogue_section.responses:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
262 response_node = self._representDialogueResponse(dumper, response)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
263 responses_list.append(response_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
264 section_dict['RESPONSES'] = responses_list_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
265
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
266 for key, value in section_dict.items():
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
267 if (isinstance(key, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
268 key_node = key
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
269 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
270 key_node = dumper.represent_data(key)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
271 if (isinstance(value, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
272 value_node = value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
273 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
274 value_node = dumper.represent_data(value)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
275 section_node.value.append((key_node, value_node))
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
276 return section_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
277
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
278 def _representDialogueResponse(self, dumper, dialogue_response):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
279 response_node = dumper.represent_dict({})
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
280 response_dict = OrderedDict()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
281 # KLUDGE Technomage 2010-11-16: Hard-coding the tag like this could be
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
282 # a problem when writing unicode.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
283 response_dict['REPLY'] = dumper.represent_scalar(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
284 'tag:yaml.org,2002:str',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
285 dialogue_response.text,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
286 style='"')
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
287 if (dialogue_response.condition is not None):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
288 response_dict['CONDITION'] = dumper.represent_scalar(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
289 'tag:yaml.org,2002:str',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
290 dialogue_response.condition,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
291 style='"'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
292 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
293 actions_list_node = dumper.represent_list([])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
294 actions_list = actions_list_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
295 for action in dialogue_response.actions:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
296 action_node = self._representDialogueAction(dumper, action)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
297 actions_list.append(action_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
298 if (actions_list):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
299 response_dict['ACTIONS'] = actions_list_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
300 response_dict['GOTO'] = dialogue_response.next_section_id
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
301
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
302 for key, value in response_dict.items():
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
303 if (isinstance(key, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
304 key_node = key
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
305 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
306 key_node = dumper.represent_data(key)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
307 if (isinstance(value, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
308 value_node = value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
309 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
310 value_node = dumper.represent_data(value)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
311 response_node.value.append((key_node, value_node))
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
312 return response_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
313
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
314 def _representDialogueAction(self, dumper, dialogue_action):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
315 action_node = dumper.represent_dict({})
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
316 action_dict = OrderedDict()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
317 args, kwargs = dialogue_action.arguments
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
318 if (args and not kwargs):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
319 arguments = list(args)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
320 elif (kwargs and not args):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
321 arguments = kwargs
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
322 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
323 arguments = [list(args), kwargs]
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
324 action_dict[dialogue_action.keyword] = arguments
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
325
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
326 for key, value in action_dict.items():
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
327 if (isinstance(key, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
328 key_node = key
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
329 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
330 key_node = dumper.represent_data(key)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
331 if (isinstance(value, yaml.Node)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
332 value_node = value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
333 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
334 value_node = dumper.represent_data(value)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
335 action_node.value.append((key_node, value_node))
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
336 return action_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
337
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
338 def _constructDialogue(self, loader, yaml_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
339 npc_name = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
340 avatar_path = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
341 default_greeting = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
342 greetings = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
343 sections = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
344
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
345 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
346 for key_node, value_node in yaml_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
347 key = key_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
348 if (key == u'NPC_NAME'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
349 npc_name = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
350 elif (key == u'AVATAR_PATH'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
351 avatar_path = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
352 elif (key == u'DEFAULT_GREETING'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
353 default_greeting = \
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
354 self._constructDialogueSection(loader, value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
355 elif (key == u'GREETINGS'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
356 for greeting_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
357 greeting = self._constructRootDialogueSection(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
358 loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
359 greeting_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
360 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
361 greetings.append(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
362 greeting
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
363 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
364 elif (key == u'SECTIONS'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
365 for section_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
366 dialogue_section = self._constructDialogueSection(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
367 loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
368 section_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
369 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
370 sections.append(dialogue_section)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
371 except (AttributeError, TypeError, ValueError) as e:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
372 raise DialogueFormatError(e)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
373
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
374 dialogue = Dialogue(npc_name=npc_name, avatar_path=avatar_path,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
375 default_greeting=default_greeting,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
376 greetings=greetings,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
377 sections=sections)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
378 return dialogue
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
379
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
380 def _constructRootDialogueSection(self, loader, greeting_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
381 id = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
382 text = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
383 condition = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
384 responses = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
385 actions = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
386 greeting = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
387
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
388 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
389 for key_node, value_node in greeting_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
390 key = key_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
391 if (key == u'ID'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
392 id = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
393 elif (key == u'SAY'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
394 text = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
395 elif (key == u'CONDITION'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
396 condition = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
397 elif (key == u'RESPONSES'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
398 for response_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
399 dialogue_response = self._constructDialogueResponse(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
400 loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
401 response_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
402 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
403 responses.append(dialogue_response)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
404 elif (key == u'ACTIONS'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
405 for action_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
406 action = self._constructDialogueAction(loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
407 action_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
408 actions.append(action)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
409 except (AttributeError, TypeError, ValueError) as e:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
410 raise DialogueFormatError(e)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
411 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
412 greeting = DialogueSection(id=id, text=text,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
413 condition=condition,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
414 responses=responses,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
415 actions=actions)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
416
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
417 return greeting
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
418
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
419 def _constructDialogueSection(self, loader, section_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
420 id_ = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
421 text = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
422 responses = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
423 actions = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
424 dialogue_section = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
425
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
426 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
427 for key_node, value_node in section_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
428 key = key_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
429 if (key == u'ID'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
430 id_ = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
431 elif (key == u'SAY'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
432 text = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
433 elif (key == u'RESPONSES'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
434 for response_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
435 dialogue_response = self._constructDialogueResponse(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
436 loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
437 response_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
438 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
439 responses.append(dialogue_response)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
440 elif (key == u'ACTIONS'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
441 for action_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
442 action = self._constructDialogueAction(loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
443 action_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
444 actions.append(action)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
445 except (AttributeError, TypeError, ValueError) as e:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
446 raise DialogueFormatError(e)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
447 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
448 dialogue_section = DialogueSection(id_=id_, text=text,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
449 responses=responses,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
450 actions=actions)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
451
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
452 return dialogue_section
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
453
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
454 def _constructDialogueResponse(self, loader, response_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
455 text = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
456 next_section_id = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
457 actions = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
458 condition = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
459
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
460 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
461 for key_node, value_node in response_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
462 key = key_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
463 if (key == u'REPLY'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
464 text = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
465 elif (key == u'ACTIONS'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
466 for action_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
467 action = self._constructDialogueAction(loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
468 action_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
469 actions.append(action)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
470 elif (key == u'CONDITION'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
471 condition = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
472 elif (key == u'GOTO'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
473 next_section_id = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
474 except (AttributeError, TypeError, ValueError) as e:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
475 raise DialogueFormatError(e)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
476
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
477 dialogue_response = DialogueResponse(text=text,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
478 next_section_id=next_section_id,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
479 actions=actions,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
480 condition=condition)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
481 return dialogue_response
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
482
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
483 def _constructDialogueAction(self, loader, action_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
484 mapping = loader.construct_mapping(action_node, deep=True)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
485 keyword, arguments = mapping.items()[0]
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
486 if (isinstance(arguments, dict)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
487 # Got a dictionary of keyword arguments.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
488 args = ()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
489 kwargs = arguments
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
490 elif (not isinstance(arguments, Sequence) or
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
491 isinstance(arguments, basestring)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
492 # Got a single positional argument.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
493 args = (arguments,)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
494 kwargs = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
495 elif (not len(arguments) == 2 or not isinstance(arguments[1], dict)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
496 # Got a list of positional arguments.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
497 args = arguments
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
498 kwargs = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
499 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
500 self.logger.error(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
501 '{0} is an invalid DialogueAction argument'.format(arguments)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
502 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
503 return None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
504
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
505 action_type = DialogueAction.registered_actions.get(keyword)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
506 if (action_type is None):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
507 self.logger.error(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
508 'no DialogueAction with keyword "{0}"'.format(keyword)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
509 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
510 dialogue_action = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
511 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
512 dialogue_action = action_type(*args, **kwargs)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
513 return dialogue_action
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
514
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
515
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
516 class OldYamlDialogueParser(YamlDialogueParser):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
517 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
518 L{YAMLDialogueParser} that can read and write dialogues in the old
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
519 Techdemo1 dialogue file format.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
520
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
521 @warning: This class is deprecated and likely to be removed in a future
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
522 version.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
523 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
524 logger = logging.getLogger('dialogueparser.OldYamlDialogueParser')
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
525
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
526 def __init__(self):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
527 self.response_actions = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
528
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
529 def load(self, stream):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
530 dialogue = YamlDialogueParser.load(self, stream)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
531 # Place all DialogueActions that were in DialogueSections into the
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
532 # DialogueResponse that led to the action's original section.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
533 for section in dialogue.sections.values():
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
534 for response in section.responses:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
535 actions = self.response_actions.get(response.next_section_id)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
536 if (actions is not None):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
537 response.actions = actions
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
538 return dialogue
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
539
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
540 def _constructDialogue(self, loader, yaml_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
541 npc_name = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
542 avatar_path = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
543 start_section_id = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
544 sections = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
545
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
546 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
547 for key_node, value_node in yaml_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
548 key = key_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
549 if (key == u'NPC'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
550 npc_name = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
551 elif (key == u'AVATAR'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
552 avatar_path = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
553 elif (key == u'START'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
554 start_section_id = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
555 elif (key == u'SECTIONS'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
556 for id_node, section_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
557 dialogue_section = self._constructDialogueSection(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
558 loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
559 id_node,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
560 section_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
561 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
562 sections.append(dialogue_section)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
563 except (AttributeError, TypeError, ValueError) as e:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
564 raise DialogueFormatError(e)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
565
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
566 dialogue = Dialogue(npc_name=npc_name, avatar_path=avatar_path,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
567 start_section_id=start_section_id,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
568 sections=sections)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
569 return dialogue
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
570
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
571 def _constructDialogueSection(self, loader, id_node, section_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
572 id = loader.construct_object(id_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
573 text = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
574 responses = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
575 actions = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
576 dialogue_section = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
577
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
578 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
579 for node in section_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
580 key_node, value_node = node.value[0]
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
581 key = key_node.value
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
582 if (key == u'say'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
583 text = loader.construct_object(value_node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
584 elif (key == u'meet'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
585 action = self._constructDialogueAction(loader, node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
586 actions.append(action)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
587 elif (key in [u'start_quest', u'complete_quest', u'fail_quest',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
588 u'restart_quest', u'set_value',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
589 u'decrease_value', u'increase_value',
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
590 u'give_stuff', u'get_stuff']):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
591 action = self._constructDialogueAction(loader, node)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
592 if (id not in self.response_actions.keys()):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
593 self.response_actions[id] = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
594 self.response_actions[id].append(action)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
595 elif (key == u'responses'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
596 for response_node in value_node.value:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
597 dialogue_response = self._constructDialogueResponse(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
598 loader,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
599 response_node
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
600 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
601 responses.append(dialogue_response)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
602 except (AttributeError, TypeError, ValueError) as e:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
603 raise DialogueFormatError(e)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
604 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
605 dialogue_section = DialogueSection(id=id, text=text,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
606 responses=responses,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
607 actions=actions)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
608
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
609 return dialogue_section
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
610
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
611 def _constructDialogueResponse(self, loader, response_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
612 text = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
613 next_section_id = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
614 actions = []
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
615 condition = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
616
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
617 try:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
618 text = loader.construct_object(response_node.value[0])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
619 next_section_id = loader.construct_object(response_node.value[1])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
620 if (len(response_node.value) == 3):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
621 condition = loader.construct_object(response_node.value[2])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
622 except (AttributeError, TypeError, ValueError) as e:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
623 raise DialogueFormatError(e)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
624
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
625 dialogue_response = DialogueResponse(text=text,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
626 next_section_id=next_section_id,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
627 actions=actions,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
628 condition=condition)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
629 return dialogue_response
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
630
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
631 def _constructDialogueAction(self, loader, action_node):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
632 mapping = loader.construct_mapping(action_node, deep=True)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
633 keyword, arguments = mapping.items()[0]
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
634 if (keyword == 'get_stuff'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
635 # Renamed keyword in new syntax.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
636 keyword = 'take_stuff'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
637 elif (keyword == 'set_value'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
638 keyword = 'set_quest_value'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
639 elif (keyword == 'increase_value'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
640 keyword = 'increase_quest_value'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
641 elif (keyword == 'decrease_value'):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
642 keyword = 'decrease_quest_value'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
643 if (isinstance(arguments, dict)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
644 # Got a dictionary of keyword arguments.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
645 args = ()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
646 kwargs = arguments
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
647 elif (not isinstance(arguments, Sequence) or
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
648 isinstance(arguments, basestring)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
649 # Got a single positional argument.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
650 args = (arguments,)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
651 kwargs = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
652 elif (not len(arguments) == 2 or not isinstance(arguments[1], dict)):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
653 # Got a list of positional arguments.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
654 args = arguments
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
655 kwargs = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
656 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
657 self.logger.error(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
658 '{0} is an invalid DialogueAction argument'.format(arguments)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
659 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
660 return None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
661 action_type = DialogueAction.registered_actions.get(keyword)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
662 if (action_type is None):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
663 self.logger.error(
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
664 'no DialogueAction with keyword "{0}"'.format(keyword)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
665 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
666 dialogue_action = None
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
667 else:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
668 dialogue_action = action_type(*args, **kwargs)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
669 return dialogue_action