Mercurial > jouvence
annotate jouvence/document.py @ 31:9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 15 Jan 2017 22:41:49 -0800 |
parents | 2ef526c301cc |
children |
rev | line source |
---|---|
13 | 1 import sys |
2 | |
3 | |
4 class JouvenceDocument: | |
31
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
5 """Represents a Fountain screenplay in a structured way. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
6 |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
7 A screenplay contains: |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
8 |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
9 * A title page (optional) with a key/value dictionary of settings. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
10 * A list of scenes. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
11 * Each scene contains a list of paragraphs of various types. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
12 """ |
13 | 13 def __init__(self): |
14 self.title_values = {} | |
15 self.scenes = [] | |
16 | |
17 def addScene(self, header=None): | |
31
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
18 """Adds a scene with the specified header.""" |
13 | 19 s = JouvenceScene() |
20 if header: | |
21 s.header = header | |
22 self.scenes.append(s) | |
23 return s | |
24 | |
25 def lastScene(self, auto_create=True): | |
31
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
26 """Gets the last scene in the screenplay. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
27 |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
28 `auto_create` |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
29 If ``True``, and the screenplay has no scenes, create |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
30 a scene with an empty header text. Otherwise, return |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
31 ``None``. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
32 """ |
13 | 33 try: |
34 return self.scenes[-1] | |
35 except IndexError: | |
36 if auto_create: | |
37 s = self.addScene() | |
38 return s | |
39 return None | |
40 | |
41 def lastParagraph(self): | |
31
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
42 """Gets the last paragraph of the last scene in the screenplay. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
43 |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
44 If there's no scene in the screenplay, return ``None``. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
45 """ |
13 | 46 s = self.lastScene(False) |
47 if s: | |
48 return s.lastParagraph() | |
49 return None | |
50 | |
51 | |
52 class JouvenceScene: | |
31
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
53 """A scene in a screenplay.""" |
13 | 54 def __init__(self): |
55 self.header = None | |
56 self.paragraphs = [] | |
57 self._adders = {} | |
58 | |
59 def __getattr__(self, name): | |
60 if name.startswith('add'): | |
61 add_type_name = name[3:] | |
62 try: | |
63 adder = self._adders[add_type_name] | |
64 except KeyError: | |
65 module = sys.modules[__name__] | |
66 add_type = getattr(module, | |
67 'TYPE_%s' % add_type_name.upper()) | |
68 | |
69 def _type_adder(_text): | |
70 new_p = JouvenceSceneElement(add_type, _text) | |
71 self.paragraphs.append(new_p) | |
72 return new_p | |
73 | |
74 adder = _type_adder | |
75 self._adders[add_type_name] = adder | |
76 return adder | |
77 else: | |
78 raise AttributeError | |
79 | |
80 def addPageBreak(self): | |
31
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
81 """Adds a page break (paragraph with ``TYPE_PAGEBREAK`` type).""" |
13 | 82 self.paragraphs.append(JouvenceSceneElement(TYPE_PAGEBREAK, None)) |
83 | |
24
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
84 def addSection(self, depth, text): |
31
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
85 """Adds a section (a :class:`~JouvenceSceneSection` instance).""" |
24
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
86 self.paragraphs.append(JouvenceSceneSection(depth, text)) |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
87 |
13 | 88 def lastParagraph(self): |
89 try: | |
90 return self.paragraphs[-1] | |
91 except IndexError: | |
92 return None | |
93 | |
94 | |
95 class JouvenceSceneElement: | |
31
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
96 """An element of a screenplay scene, _e.g._ an action, a dialogue |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
97 line, a parenthetical, etc. |
9ae14e9615e6
docs: Add Sphynx documentation and code docstrings.
Ludovic Chabant <ludovic@chabant.com>
parents:
24
diff
changeset
|
98 """ |
13 | 99 def __init__(self, el_type, text): |
100 self.type = el_type | |
101 self.text = text | |
102 | |
103 def __str__(self): | |
104 return '%s: %s' % ( | |
105 _scene_element_type_str(self.type), | |
106 _ellipsis(self.text, 15)) | |
107 | |
108 | |
24
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
109 class JouvenceSceneSection(JouvenceSceneElement): |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
110 def __init__(self, depth, text): |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
111 super().__init__(TYPE_SECTION, text) |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
112 self.depth = depth |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
113 |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
114 |
13 | 115 TYPE_ACTION = 0 |
116 TYPE_CENTEREDACTION = 1 | |
117 TYPE_CHARACTER = 2 | |
118 TYPE_DIALOG = 3 | |
119 TYPE_PARENTHETICAL = 4 | |
120 TYPE_TRANSITION = 5 | |
121 TYPE_LYRICS = 6 | |
122 TYPE_PAGEBREAK = 7 | |
24
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
123 TYPE_SECTION = 8 |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
124 TYPE_SYNOPSIS = 9 |
13 | 125 |
126 | |
127 def _scene_element_type_str(t): | |
128 if t == TYPE_ACTION: | |
129 return 'ACTION' | |
130 if t == TYPE_CENTEREDACTION: | |
131 return 'CENTEREDACTION' | |
132 if t == TYPE_CHARACTER: | |
133 return 'CHARACTER' | |
134 if t == TYPE_DIALOG: | |
135 return 'DIALOG' | |
136 if t == TYPE_PARENTHETICAL: | |
137 return 'PARENTHETICAL' | |
138 if t == TYPE_TRANSITION: | |
139 return 'TRANSITION' | |
140 if t == TYPE_LYRICS: | |
141 return 'LYRICS' | |
142 if t == TYPE_PAGEBREAK: | |
143 return 'PAGEBREAK' | |
24
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
144 if t == TYPE_SECTION: |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
145 return 'SECTION' |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
146 if t == TYPE_SYNOPSIS: |
2ef526c301cc
Add support for sections and synopsis.
Ludovic Chabant <ludovic@chabant.com>
parents:
13
diff
changeset
|
147 return 'SYNOPSIS' |
13 | 148 raise NotImplementedError() |
149 | |
150 | |
151 def _ellipsis(text, length): | |
152 if len(text) > length: | |
153 return text[:length - 3] + '...' | |
154 return text |