-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathparser.py
executable file
·42 lines (38 loc) · 1.42 KB
/
parser.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
#!/usr/bin/env python2
def file_exist(file_path):
import os.path
if os.path.exists(file_path) and os.path.isfile(file_path):
return True
return False
def get_4b_magic_number(file_path):
try:
binaryFile = open(file_path, 'rb')
magicNumber = binaryFile.read(4)
except(IOError), e:
print "%s: unable to open/read.\n%s" % (file_path, e)
else:
return magicNumber
return False
def parse(file_path, image_folder):
if file_exist(file_path):
magicNumber = get_4b_magic_number(file_path)
if magicNumber is not False:
if magicNumber == "8BPS": #PSD file
from psdtools import main
elif magicNumber == "%PDF": #PDF file
from pdfreader import main
return main.run(file_path, image_folder)
else:
print "%s: not a psd nor a pdf (unknown Magic Number)" % file_path
else:
print "%s: file not found." % file_path
if __name__ == '__main__':
import sys
if len(sys.argv) == 3:
json = parse(sys.argv[1], sys.argv[2])
""" We write the json into a file called metadata.json """
target = open("metadata.json", 'w+') # a will append, w will over-write
target.write(json)
target.close()
else:
print "usage: %s pdf_or_psd_file_path generated_images_path/ (eg: python %s book.pdf/.psd './images/')" % (sys.argv[0], sys.argv[0])