Mercurial > jouvence
annotate fontaine/document.py @ 2:59fe8cb6190d
Add lots of tests, fix lots of bugs.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 03 Jan 2017 09:05:28 -0800 |
parents | 74b83e3d921e |
children |
rev | line source |
---|---|
0 | 1 import sys |
2 | |
3 | |
4 class FontaineDocument: | |
5 def __init__(self): | |
6 self.title_values = {} | |
7 self.scenes = [] | |
8 | |
9 def addScene(self, header=None): | |
10 s = FontaineScene() | |
11 if header: | |
12 s.header = header | |
13 self.scenes.append(s) | |
14 return s | |
15 | |
16 def lastScene(self, auto_create=True): | |
17 try: | |
18 return self.scenes[-1] | |
19 except IndexError: | |
20 if auto_create: | |
21 s = self.addScene() | |
22 return s | |
23 return None | |
24 | |
25 def lastParagraph(self): | |
26 s = self.lastScene(False) | |
27 if s: | |
28 return s.lastParagraph() | |
29 return None | |
30 | |
31 | |
32 class FontaineScene: | |
33 def __init__(self): | |
34 self.header = None | |
35 self.paragraphs = [] | |
36 self._adders = {} | |
37 | |
38 def __getattr__(self, name): | |
39 if name.startswith('add'): | |
40 add_type_name = name[3:] | |
41 try: | |
42 adder = self._adders[add_type_name] | |
43 except KeyError: | |
44 module = sys.modules[__name__] | |
45 add_type = getattr(module, | |
46 'TYPE_%s' % add_type_name.upper()) | |
47 | |
48 def _type_adder(_text): | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
49 new_p = FontaineSceneElement(add_type, _text) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
50 self.paragraphs.append(new_p) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
51 return new_p |
0 | 52 |
53 adder = _type_adder | |
54 self._adders[add_type_name] = adder | |
55 return adder | |
56 else: | |
57 raise AttributeError | |
58 | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
59 def addPageBreak(self): |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
60 self.paragraphs.append(FontaineSceneElement(TYPE_PAGEBREAK, None)) |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
61 |
0 | 62 def lastParagraph(self): |
63 try: | |
64 return self.paragraphs[-1] | |
65 except IndexError: | |
66 return None | |
67 | |
68 | |
69 class FontaineSceneElement: | |
70 def __init__(self, el_type, text): | |
71 self.type = el_type | |
72 self.text = text | |
73 | |
74 def __str__(self): | |
75 return '%s: %s' % ( | |
76 _scene_element_type_str(self.type), | |
77 _ellipsis(self.text, 15)) | |
78 | |
79 | |
80 TYPE_ACTION = 0 | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
81 TYPE_CENTEREDACTION = 1 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
82 TYPE_CHARACTER = 2 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
83 TYPE_DIALOG = 3 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
84 TYPE_PARENTHETICAL = 4 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
85 TYPE_TRANSITION = 5 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
86 TYPE_LYRICS = 6 |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
87 TYPE_PAGEBREAK = 7 |
2
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
88 TYPE_EMPTYLINES = 8 |
0 | 89 |
90 | |
91 def _scene_element_type_str(t): | |
92 if t == TYPE_ACTION: | |
93 return 'ACTION' | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
94 if t == TYPE_CENTEREDACTION: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
95 return 'CENTEREDACTION' |
0 | 96 if t == TYPE_CHARACTER: |
97 return 'CHARACTER' | |
98 if t == TYPE_DIALOG: | |
99 return 'DIALOG' | |
100 if t == TYPE_PARENTHETICAL: | |
101 return 'PARENTHETICAL' | |
1
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
102 if t == TYPE_TRANSITION: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
103 return 'TRANSITION' |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
104 if t == TYPE_LYRICS: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
105 return 'LYRICS' |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
106 if t == TYPE_PAGEBREAK: |
74b83e3d921e
Add more states, add more tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
107 return 'PAGEBREAK' |
2
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
108 if t == TYPE_EMPTYLINES: |
59fe8cb6190d
Add lots of tests, fix lots of bugs.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
109 return 'EMPTYLINES' |
0 | 110 raise NotImplementedError() |
111 | |
112 | |
113 def _ellipsis(text, length): | |
114 if len(text) > length: | |
115 return text[:length - 3] + '...' | |
116 return text |