-
Notifications
You must be signed in to change notification settings - Fork 1
/
pp_tv.py
executable file
·31 lines (30 loc) · 1.07 KB
/
pp_tv.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
#!/usr/bin/env python
# Parses a tag/value file
# and prints it out formatted.
# usage pp_tv <inputfile> <outfile>
if __name__ == '__main__':
import sys
import codecs
from spdx.writers.tagvalue import write_document, InvalidDocumentError
from spdx.parsers.tagvalue import Parser
from spdx.parsers.loggers import StandardLogger
from spdx.parsers.tagvaluebuilders import Builder
source = sys.argv[1]
target = sys.argv[2]
p = Parser(Builder(), StandardLogger())
p.build()
with open(source, 'r') as f:
data = f.read()
document, error = p.parse(data)
if not error:
print 'Parsing Successful'
with codecs.open(target, mode='w', encoding='utf-8') as out:
try:
write_document(document, out)
except InvalidDocumentError:
print 'Document is Invalid'
messages = []
document.validate(messages)
print '\n'.join(messages)
else:
print 'Errors encountered while parsing'