Skip to content
Ahmed Hisham Ismail edited this page Aug 12, 2014 · 3 revisions

Parsing RDF file

In order to parse a file you need to construct a Parser instance. The RDF parser for SPDX 1.2 is spdx.parsers.rdf.Parser. It requires a builder and a logger. You can use the standard builder and logger spdx.parsers.rdfbuilders.Builder and spdx.parsers.loggers.StandardLogger.

Example:

from spdx.parsers.rdf import Parser
from spdx.parsers.loggers import StandardLogger
from spdx.parsers.rdfbuilders import Builder
rdfparser = Parser(Builder(), StandardLogger())

The function parse returns a pair which is an spdx.document.Document and a boolean that is True if errors were encountered while parsing.

Example:

with open(file_name) as f:
        doc, error = rdfparser.parse(f)

The file parse_rdf.py contains an example of parsing an RDF file and printing out some values.

Parsing tag value file

In order to parse a file you need to construct a Parser instance. The tag/value parser for SPDX 1.2 is spdx.parsers.tagvalue.Parser. It requires a builder and a logger. You can use the standard builder and logger spdx.parsers.tagvaluebuilders.Builder and spdx.parsers.loggers.StandardLogger.

Example:

from spdx.parsers.tagvalue import Parser
from spdx.parsers.loggers import StandardLogger
from spdx.parsers.tagvaluebuilders import Builder
tvparser = Parser(Builder(), StandardLogger())
tvparser.build()

Note: unlike the RDF parser the method build must be called on the tagvalue parser before parsing. This will likely change in the future.

The function parse returns a pair which is an spdx.document.Document and a boolean that is True if errors were encountered while parsing.

Example:

with open(file_name) as f:
        doc, error = tvparser.parse(f)

The file parse_tv_ex.py provides a demonstration.

Writing tag value file

The function spdx.writers.tagvalue.write_document takes two parameters an spdx.document.Document instance and a file or file like object. Once you have a Document either constructed or returned from a parser you can use it like so:

from spdx.writers.tagvalue import write_document, InvalidDocumentError
with codecs.open(file_name, mode='w', encoding='utf-8') as out:
        try:
            write_document(doc, out)
        except InvalidDocumentError:
            print 'Document is Invalid'
            messages = []
            doc.validate(messages)
            print '\n'.join(messages)

Please note that the function raises an InvalidDocumentError if the document is not valid, Document.validate function returns False. There are three example files that demonstrate the tag/value writer. write_tv_ex.py which creates a document model and then writes it out. pp_tv.py which parses a tag/value file and writes it out, effectively formatting it. rdf_to_tv.py which converts from RDF to tag/value.

Writing rdf file

The function spdx.writers.rdf.write_document takes two parameters an spdx.document.Document instance and a file or file like object. Once you have a Document either constructed or returned from a parser you can use it like so:

from spdx.writers.rdf import write_document, InvalidDocumentError
with open(file_name, mode='w') as out:
        try:
            write_document(doc, out)
        except InvalidDocumentError:
            print 'Document is Invalid'
            messages = []
            doc.validate(messages)
            print '\n'.join(messages)

Clone this wiki locally