annotate pytouhou/formats/hint.py @ 402:88e2a2485b2b

Add a version attribute to ANM0 and latest discovered instruction.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 16 Feb 2013 19:31:29 +0100
parents 7aa70f0def38
children 6c0cb3eee33e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
400
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 from collections import OrderedDict
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 def _read_n_int(value):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 values = value.split(', ')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 return tuple(int(value) for value in values)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 class Stage(list):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 def __init__(self, number):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 list.__init__(self)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 self.number = number
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 class Hint(object):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 _fields = {'Stage': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 'Tips': None,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 'Remain': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 'Text': lambda v: v[1:-1],
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 'Pos': _read_n_int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 'Count': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 'Base': str,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 'Align': str,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 'Time': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 'Alpha': int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 'Color': _read_n_int,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 'Scale': float,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 'End': None,
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 'StageEnd': None}
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 def __init__(self):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 self.version = 0.0
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 self.stages = []
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 @classmethod
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 def read(cls, file):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 tokens = []
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 for line in file:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 line = line.strip()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 if not line:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 continue
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 if line[0] == '#':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 continue
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 if line == 'Version = 0.0':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 continue
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 field, _, value = line.partition(':')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 field = field.rstrip()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 value = value.lstrip()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 parser = cls._fields[field]
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 if parser:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 tokens.append((field, parser(value)))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 else:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 tokens.append((field, None))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 stage_mode = False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 tip_mode = False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 stage = None
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 hints = cls()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 stages = hints.stages
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 for token in tokens:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 key = token[0]
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 value = token[1]
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 if stage_mode:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 if key != 'StageEnd':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 if tip_mode:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 if key != 'End':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 tip[key] = value
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 else:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 assert tip_mode == True
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 stage.append(tip)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 tip_mode = False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 elif key == 'Tips':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 assert tip_mode == False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 tip = OrderedDict()
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 tip_mode = True
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 else:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 assert stage_mode == True
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 stages.append(stage)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 stage_mode = False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 elif key == 'Stage':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 assert stage_mode == False
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 stage = Stage(value)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 stage_mode = True
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 return hints
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 def write(self, file):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 file.write('# Hints file generated with PyTouhou\n\n\n')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 file.write('Version = {}\n\n'.format(self.version))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 for stage in self.stages:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 file.write('# ================================== \n')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 file.write('Stage : {}\n\n'.format(stage.number))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 for tip in stage:
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 file.write('Tips\n')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 for key, value in tip.items():
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 if key == 'Text':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 value = '"{}"'.format(value)
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 elif key == 'Pos':
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 key = 'Pos\t'
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 if isinstance(value, tuple):
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 value = str(value)[1:-1]
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 file.write('\t{}\t: {}\n'.format(key, value))
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 file.write('End\n\n')
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133
7aa70f0def38 Add support for MoF’s hint format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 file.write('StageEnd\n')