OSDN Git Service

implement morph
[meshio/pymeshio.git] / pymeshio / compatibility.py
1 #!/usr/bin/python
2 # coding: utf-8
3 """
4 utitlities for pmd and vmd loading. 
5
6 - python2 and python3 compatibility.
7
8 """
9 import sys
10 import os.path
11
12
13 """
14 utility functions for python2 and python3 compatibility.
15 """
16 if sys.version_info[0]>=3:
17     xrange=range
18
19 if sys.version_info[0]<3:
20     def truncate_zero(src):
21         """
22         drop after 0x00
23         """
24         assert(type(src)==bytes)
25         pos = src.find(b"\x00")
26         if pos >= 0:
27             return src[:pos]
28         else:
29             return src
30 else:
31     def truncate_zero(src):
32         """
33         drop after 0x00
34         """
35         assert(type(src)==bytes)
36         pos = src.find(b"\x00")
37         if pos >= 0:
38             return src[:pos].decode('cp932')
39         else:
40             return src.decode('cp932')
41
42
43 if sys.version_info[0]<3:
44     def to_str(src):
45         """
46         str or unicode to str
47         """
48         t=type(src)
49         if t==unicode:
50             return src.encode('cp932')
51         elif t==str:
52             return src
53         else:
54             raise "INVALID str: %s" % t
55
56     def from_str(src):
57         """
58         do nothing
59         """
60         return src
61
62 else:
63     def to_str(src):
64         """
65         bytes or str to str
66         """
67         t=type(src)
68         if t==str:
69             return src
70         elif t==bytes:
71             return src.decode('cp932')
72         else:
73             raise "INVALID str: %s" % t
74
75     def from_str(src):
76         """
77         str to bytes
78         """
79         return src.encode('cp932')
80
81