-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (65 loc) · 2.13 KB
/
main.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
#!/usr/bin/env python
DEBUG = True # This is key.
import sys
import os
import shutil
import zipfile
import xml.etree.ElementTree as ET
import parsers
import gui
DREDLY_EXTENSIONS = ['inf', 'dredly', 'drd']
def makeZip(path):
''' Makes a zip out of a given folder with the same name as the folder.
It creates in the directory this was run out of. '''
if not os.path.isdir(path):
raise ValueError('Must be a directory')
zf = zipfile.ZipFile(os.path.join(os.path.curdir, os.path.basename(path)+'.zip'), 'w')
for root, dirs, files in os.walk(path):
for filename in files:
zf.write(os.path.join(root, filename))
# print root, filename
zf.close()
def parseFolder(path, parser, tmp_path = os.path.join(os.path.curdir,'tmp')):
try:
os.mkdir(tmp_path)
except OSError:
shutil.rmtree(tmp_path)
os.mkdir(tmp_path)
# TODO: (M) Make it generate foldres based on content instead.
os.mkdir(os.path.join(tmp_path,'mod'))
os.mkdir(os.path.join(tmp_path,'sprites'))
os.mkdir(os.path.join(tmp_path,'items'))
for root, dirs, files in os.walk(path):
for filename in files:
if filename.split('.')[-1] in DREDLY_EXTENSIONS: # If it's a dredly file then parse
# print "Parsing", filename
f = open(os.path.join(root, filename), 'r')
parser.parseFile(f)
else: # Else just copy
# print filename
# print root
shutil.copy(os.path.join(root,filename), # The file to be copied
os.path.join(tmp_path,root.split(path)[1].partition(os.sep)[2],filename)) # Gets the equivalent location
# Now that you've parsed everything make the xml
parser.createXML()
for f in parser.xml:
if f[0] != None: # If there is xml, skips if it was empty.
ET.ElementTree(f[0]).write(os.path.join(tmp_path, f[1]))
makeZip(tmp_path)
shutil.rmtree(tmp_path)
def main():
#gui.main()
# Parse the syntax.
if not DEBUG:
path = raw_input('Please enter syntax filepath:')
else:
path = './dredly syntax/tests/test.drd'
f = open(path ,'r')
global Parser
Parser = parsers.Parser(f)
# Now run it on the test mod!
if not DEBUG:
path = raw_input('Please enter mod filepath:')
else:
path = './dredly syntax/tests/content'
parseFolder(path,Parser)