annotate LightTools/build.py @ 61:1fe27776627e

Added asserts
author koryspansel
date Mon, 03 Oct 2011 15:05:09 -0700
parents 2444937929ae
children 6d4437a24aeb
rev   line source
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
1 #
46
a379bce1aeb1 Rename map.py to build.py; Add release script
koryspansel
parents: 45
diff changeset
2 # build
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
3 #
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
4
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
5 import sys
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
6 import string
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
7 import struct
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
8 import os
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
9 import functools
51
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
10 import re
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
11
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
12 _pattern = re.compile('\(\s*(\d+)\s*,\s*(\d+)\s*\)')
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
13
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
14 class MapDefinition:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
15 Parse = functools.partial(string.split, sep=',')
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
16
51
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
17 def __init__(self, size, position, direction, grid):
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
18 self.Size = self._ParseVector(size)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
19 self.Position = self._ParseVector(position)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
20 self.Direction = self._ParseVector(direction)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
21 self.Grid = self._ParseList(grid)
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
22
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
23 def Write(self, filename):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
24 with open(filename, 'wb') as handle:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
25 handle.write(struct.pack('i' * len(self.Size), *self.Size)) # 8
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
26 handle.write(struct.pack('i' * len(self.Position), *self.Position)) # 8
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
27 handle.write(struct.pack('i' * len(self.Direction), *self.Direction)) # 4
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
28
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
29 for tower in self.Grid:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
30 handle.write(struct.pack('i' * len(tower), *tower)) # 8 * Size.X * Size.Y
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
31
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
32 def _ParseVector(self, line):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
33 return map(int, self.Parse(line))
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
34
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
35 def _ParseList(self, lines):
52
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
36 return [map(int, item.groups()) for item in _pattern.finditer(''.join(lines))]
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
37
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
38 def ReadDefinition(filename):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
39 def FilterLines(lines):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
40 for line in map(string.strip, lines):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
41 if line and not line.startswith('#'):
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
42 yield line.split('#')[0]
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
43
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
44 with open(filename, 'rt') as handle:
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
45 lines = handle.readlines()
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
46
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
47 definition = tuple(FilterLines(lines))
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
48
51
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
49 size = definition[0]
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
50 position = definition[1]
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
51 direction = definition[2]
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
52 grid = definition[3:]
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
53
51
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
54 return MapDefinition(size, position, direction, grid)
efd2b1ca5b77 Clean up gui
koryspansel <koryspansel@bendbroadband.com>
parents: 46
diff changeset
55
52
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
56 def Compile(input_map, output_map):
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
57 if os.path.isfile(input_map):
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
58 definition = ReadDefinition(input_map)
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
59 if not definition:
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
60 print 'Failed to read definition "%s"' % input_map
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
61
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
62 else:
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
63 folder = os.path.dirname(output_map)
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
64
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
65 if not os.path.isdir(folder):
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
66 os.makedirs(folder)
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
67
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
68 print 'Building %s from %s' % (os.path.basename(output_map), os.path.basename(input_map))
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
69 definition.Write(output_map)
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
70
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
71 if __name__ == '__main__':
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
72 path_root = os.path.dirname(sys.argv[0])
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
73 path_assets = os.path.abspath(os.path.join(path_root, '..', 'Assets', 'Maps'))
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
74 path_build = os.path.abspath(os.path.join(path_root, '..', 'Data', 'Maps'))
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
75
52
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
76 if len(sys.argv) > 1:
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
77 Compile(os.path.join(path_assets, sys.argv[1]), os.path.join(path_build, os.path.splitext(sys.argv[1])[0] + '.map'))
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
78
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
79 else:
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
80
52
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
81 if os.path.isdir(path_assets):
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
82 for filename in os.listdir(path_assets):
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
83 if not filename.endswith('.def'):
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
84 continue
0
7e3a0ae9c016 Initial commit
koryspansel <koryspansel@bendbroadband.com>
parents:
diff changeset
85
52
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
86 Compile(os.path.join(path_assets, filename), os.path.join(path_build, os.path.splitext(filename)[0] + '.map'))
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
87 #definition = ReadDefinition(os.path.join(path_assets, filename))
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
88 #if not definition:
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
89 # continue
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
90
52
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
91 #output = os.path.join(path_build, os.path.splitext(filename)[0] + '.map')
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
92 #folder = os.path.dirname(output)
45
50def8c971d9 Move build functionality into map.py
koryspansel
parents: 0
diff changeset
93
52
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
94 #if not os.path.isdir(folder):
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
95 # os.makedirs(folder)
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
96
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
97 #print 'Building %s from %s' % (os.path.basename(output), os.path.basename(filename))
2444937929ae Update map building script
koryspansel <koryspansel@bendbroadband.com>
parents: 51
diff changeset
98 #definition.Write(output)