-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfilefactory.py
47 lines (36 loc) · 1.19 KB
/
filefactory.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
#!/usr/bin/env python
# coding: utf-8
import os
import reffile
import binfile
import rawfile
binnames = [
'dfhack.init',
'dfhack.init-example',
]
rawnames = [
'init.txt',
'd_init.txt',
'colors.txt',
'interface.txt',
'announcements.txt',
'world_gen.txt',
'overrides.txt',
]
def filefactory(path, log=None, **kwargs): # TODO: move this elsewhere and make it more easily configurable
basename = os.path.basename(path)
try:
if basename.endswith('.txt'):
with open(path, 'rb') as txt:
if txt.readline().strip() == os.path.splitext(basename)[0]:
txt.seek(0)
return rawfile.rawfile(path=path, file=txt, **kwargs)
elif basename in binnames:
return binfile.binfile(path=path, **kwargs)
elif basename in rawnames:
return rawfile.rawfile(path=path, file=txt, **kwargs)
except Exception as e:
if log:
log.warning('Failed to read file from path %s. Defauling to reading as a reffile, which should (hopefully) work despite.' % path)
log.debug(traceback.format_exc())
return reffile.reffile(path=path, **kwargs)