8 from sgflib
import SGFParser
11 wrong_part
= exc
.object[exc
.start
:exc
.end
+1]
13 us
= wrong_part
.decode('utf-8')
14 return (us
, exc
.end
+1)
16 diff
= exc
.end
-exc
.start
18 logging
.warn("sgf_load.py : Long chain of chars (%d) badly encoded."%diff)
19 return (u
'?'*(exc
.end
-exc
.start
), exc
.end
)
21 codecs
.register_error('my_err', my_err
)
23 class ParseError(Exception):
26 def load_sgf_file_headers(filename
):
27 """Returns list of dictionaries.
28 Each dictionary contains all header fields of corresponding gametree."""
29 with
open(filename
, 'r') as f
:
33 collection
= SGFParser(sgfdata
).parse()
38 for gametree
in collection
:
39 ret
.append(process_gametree(gametree
))
43 def list_attributes(node
):
44 return node
.data
.keys()
46 def get_attribute(node
, atr
):
48 atr
= node
.data
[atr
].data
[0]
49 ret
= atr
.decode(errors
='my_err')
54 def process_gametree(gametree
):
55 # cursor for tree traversal
57 # first node is the header
60 attributes
= list_attributes(header
)
62 for key
in attributes
:
63 atr
= get_attribute(header
, key
)
68 if __name__
== '__main__':
70 print load_sgf_file_headers('../TEST_FILES/games/1930-01-00a.sgf')