1
|
|
|
#!/usr/bin/env python |
2
|
|
|
|
3
|
|
|
import os |
4
|
|
|
|
5
|
|
|
import tcod as libtcod |
6
|
|
|
|
7
|
|
|
def test_parser(): |
8
|
|
|
print ('***** File Parser test *****') |
9
|
|
|
parser=libtcod.parser_new() |
10
|
|
|
struct=libtcod.parser_new_struct(parser, b'myStruct') |
11
|
|
|
libtcod.struct_add_property(struct, b'bool_field', libtcod.TYPE_BOOL, True) |
12
|
|
|
libtcod.struct_add_property(struct, b'char_field', libtcod.TYPE_CHAR, True) |
13
|
|
|
libtcod.struct_add_property(struct, b'int_field', libtcod.TYPE_INT, True) |
14
|
|
|
libtcod.struct_add_property(struct, b'float_field', libtcod.TYPE_FLOAT, True) |
15
|
|
|
libtcod.struct_add_property(struct, b'color_field', libtcod.TYPE_COLOR, True) |
16
|
|
|
libtcod.struct_add_property(struct, b'dice_field', libtcod.TYPE_DICE, True) |
17
|
|
|
libtcod.struct_add_property(struct, b'string_field', libtcod.TYPE_STRING, |
18
|
|
|
True) |
19
|
|
|
libtcod.struct_add_list_property(struct, b'bool_list', libtcod.TYPE_BOOL, |
20
|
|
|
True) |
21
|
|
|
libtcod.struct_add_list_property(struct, b'char_list', libtcod.TYPE_CHAR, |
22
|
|
|
True) |
23
|
|
|
libtcod.struct_add_list_property(struct, b'integer_list', libtcod.TYPE_INT, |
24
|
|
|
True) |
25
|
|
|
libtcod.struct_add_list_property(struct, b'float_list', libtcod.TYPE_FLOAT, |
26
|
|
|
True) |
27
|
|
|
libtcod.struct_add_list_property(struct, b'string_list', libtcod.TYPE_STRING, |
28
|
|
|
True) |
29
|
|
|
libtcod.struct_add_list_property(struct, b'color_list', libtcod.TYPE_COLOR, |
30
|
|
|
True) |
31
|
|
|
## # dice lists doesn't work yet |
32
|
|
|
## libtcod.struct_add_list_property(struct, b'dice_list', libtcod.TYPE_DICE, |
33
|
|
|
## True) |
34
|
|
|
|
35
|
|
|
# default listener |
36
|
|
|
print ('***** Default listener *****') |
37
|
|
|
libtcod.parser_run(parser, os.path.join('libtcod', 'data', 'cfg', 'sample.cfg')) |
38
|
|
|
print ('bool_field : ', \ |
39
|
|
|
libtcod.parser_get_bool_property(parser, b'myStruct.bool_field')) |
40
|
|
|
print ('char_field : ', \ |
41
|
|
|
libtcod.parser_get_char_property(parser, b'myStruct.char_field')) |
42
|
|
|
print ('int_field : ', \ |
43
|
|
|
libtcod.parser_get_int_property(parser, b'myStruct.int_field')) |
44
|
|
|
print ('float_field : ', \ |
45
|
|
|
libtcod.parser_get_float_property(parser, b'myStruct.float_field')) |
46
|
|
|
print ('color_field : ', \ |
47
|
|
|
libtcod.parser_get_color_property(parser, b'myStruct.color_field')) |
48
|
|
|
print ('dice_field : ', \ |
49
|
|
|
libtcod.parser_get_dice_property(parser, b'myStruct.dice_field')) |
50
|
|
|
print ('string_field : ', \ |
51
|
|
|
libtcod.parser_get_string_property(parser, b'myStruct.string_field')) |
52
|
|
|
print ('bool_list : ', \ |
53
|
|
|
libtcod.parser_get_list_property(parser, b'myStruct.bool_list', |
54
|
|
|
libtcod.TYPE_BOOL)) |
55
|
|
|
print ('char_list : ', \ |
56
|
|
|
libtcod.parser_get_list_property(parser, b'myStruct.char_list', |
57
|
|
|
libtcod.TYPE_CHAR)) |
58
|
|
|
print ('integer_list : ', \ |
59
|
|
|
libtcod.parser_get_list_property(parser, b'myStruct.integer_list', |
60
|
|
|
libtcod.TYPE_INT)) |
61
|
|
|
print ('float_list : ', \ |
62
|
|
|
libtcod.parser_get_list_property(parser, b'myStruct.float_list', |
63
|
|
|
libtcod.TYPE_FLOAT)) |
64
|
|
|
print ('string_list : ', \ |
65
|
|
|
libtcod.parser_get_list_property(parser, b'myStruct.string_list', |
66
|
|
|
libtcod.TYPE_STRING)) |
67
|
|
|
print ('color_list : ', \ |
68
|
|
|
libtcod.parser_get_list_property(parser, b'myStruct.color_list', |
69
|
|
|
libtcod.TYPE_COLOR)) |
70
|
|
|
## print ('dice_list : ', \ |
71
|
|
|
## libtcod.parser_get_list_property(parser, b'myStruct.dice_list', |
72
|
|
|
## libtcod.TYPE_DICE)) |
73
|
|
|
|
74
|
|
|
# custom listener |
75
|
|
|
print ('***** Custom listener *****') |
76
|
|
View Code Duplication |
class MyListener: |
|
|
|
|
77
|
|
|
def new_struct(self, struct, name): |
78
|
|
|
print ('new structure type', libtcod.struct_get_name(struct), \ |
79
|
|
|
' named ', name ) |
80
|
|
|
return True |
81
|
|
|
def new_flag(self, name): |
82
|
|
|
print ('new flag named ', name) |
83
|
|
|
return True |
84
|
|
|
def new_property(self,name, typ, value): |
85
|
|
|
type_names = ['NONE', 'BOOL', 'CHAR', 'INT', 'FLOAT', 'STRING', \ |
86
|
|
|
'COLOR', 'DICE'] |
87
|
|
|
type_name = type_names[typ & 0xff] |
88
|
|
|
if typ & libtcod.TYPE_LIST: |
89
|
|
|
type_name = 'LIST<%s>' % type_name |
90
|
|
|
print ('new property named ', name,' type ',type_name, \ |
91
|
|
|
' value ', value) |
92
|
|
|
return True |
93
|
|
|
def end_struct(self, struct, name): |
94
|
|
|
print ('end structure type', libtcod.struct_get_name(struct), \ |
95
|
|
|
' named ', name) |
96
|
|
|
return True |
97
|
|
|
def error(self,msg): |
98
|
|
|
print ('error : ', msg) |
99
|
|
|
return True |
100
|
|
|
libtcod.parser_run(parser, os.path.join('libtcod','data','cfg','sample.cfg'), MyListener()) |
101
|
|
|
|
102
|
|
|
if __name__ == '__main__': |
103
|
|
|
test_parser() |
104
|
|
|
|