This repository has been archived by the owner on Dec 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
103 lines (78 loc) · 2.36 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
"""
Utilitary functions
"""
import os.path
def getfilename(val):
"""
Returns "item" from /home/adys/Item.dbc
"""
return os.path.splitext(os.path.basename(val))[0].lower()
def generate_structure(db):
"""
Generates a DBStructure based on header data
TODO improve it, guess floats and shorter fields.
"""
from .structures import GeneratedStructure
if db.header.field_count * 4 == db.header.reclen:
structure_string = "i" * db.header.field_count
else:
raise NotImplementedError
return GeneratedStructure(structure_string)
def fopen(f, build=0, structure=None, environment={}):
from .structures import StructureNotFound, getstructure
if isinstance(f, basestring):
# open() the file only if passing a path
f = open(f, "rb")
filename = getfilename(f.name)
f.seek(0)
signature = f.read(4)
if signature == "WDB2" or signature == "WCH2":
from .db2 import DB2File
cls = DB2File
try:
_structure = structure or getstructure(filename)
except StructureNotFound:
pass
elif signature == "WDBC":
from .dbc import DBCFile, InferredDBCFile
cls = DBCFile
try:
_structure = structure or getstructure(filename)
except StructureNotFound:
pass
else:
cls = DBCFile
if getattr(_structure, "implicit_id", None):
cls = InferredDBCFile
elif not signature:
raise IOError()
elif f.name.endswith(".wcf"):
from .dbc import WCFFile
cls = WCFFile
structure = structure or getstructure(filename)
else:
from .wdb import WDBFile
cls = WDBFile
f.seek(0)
return cls.open(f, build=build, structure=structure, environment=environment)
def new(name, build=0, structure=None, environment={}):
from .structures import getstructure
file = open(name, "wb")
if not structure:
structure = getstructure(getfilename(name), build=build)
if structure.signature == "WDBC":
from .dbc import DBCFile
return DBCFile(file, build=build, structure=structure, environment=environment)
from .wdb import WDBFile
return WDBFile(file, build=build, structure=structure, environment=environment)
__envcache = {}
def get(name, build, locale="enUS"):
from ..environment import Environment, highestBuild
if build == -1:
build = highestBuild()
if build not in __envcache:
__envcache[build] = {}
if locale not in __envcache[build]:
__envcache[build][locale] = Environment(build, locale)
return __envcache[build][locale].dbFile(name)