# -*- coding: utf-8 -*-
"""

Options class: collect the possible options that govern the parsing possibilities. It also includes a reference and
handling of the extra Graph for warnings, informations, errors.


@summary: RDFa parser (distiller)
@requires: U{RDFLib<http://rdflib.net>}
@requires: U{html5lib<http://code.google.com/p/html5lib/>} for the HTML5 parsing; note possible dependecies on Python's version on the project's web site
@organization: U{World Wide Web Consortium<http://www.w3.org>}
@author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}
@license: This software is available for use under the
U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">}

"""

"""
$Id: Options.py,v 1.4 2008-06-16 09:47:24 ivan Exp $ $Date: 2008-06-16 09:47:24 $
"""

import sys
from rdflib.Graph	import Graph
from rdflib.URIRef	import URIRef
from rdflib.Literal	import Literal
from rdflib.RDFS	import comment as rdfs_comment

class CommentGraph :
	"""Class to handle the 'comment graph', ie, the (RDF) Graph containing the warnings,
	error messages, and informational messages.
	"""
	def __init__(self, warnings = False) :
		"""
		@param warnings: whether a graph should effectively be set up, or whether this
		should just be an empty shell for the various calls to work (without effect)
		"""
		if warnings :
			self.graph = Graph()
		else :
			self.graph = None
		self.accumulated_literals = []
		self.baseURI              = None
		
	def _add_triple(self, msg) :
		obj = Literal(msg)
		if self.baseURI == None :
			self.accumulated_literals.append(obj)
		elif self.graph != None :
			self.graph.add((self.baseURI, rdfs_comment, obj))
			
	def set_base_URI(self, URI) :
		"""Set the base URI for the comment triples.
		
		Note that this method I{must} be called at some point to complete the triples. Without it the triples
		added via L{add_warning<CommentGraph.add_warning>}, L{add_info<CommentGraph.add_info>}, etc, will not be added to the final graph.
		
		@param URI: URIRef for the subject of the comments
		"""
		self.baseURI = URI
		if self.graph != None :
			for lit in self.accumulated_literals :
				self.graph.add((self.baseURI, rdfs_comment, lit))
		self.accumulated_literals = []
				
	def add_warning(self, txt) :
		"""Add a warning. A comment triplet is added to the separate "warning" graph.
		@param txt: the warning text. It will be preceded by the string "==== pyRdfa Warning ==== "
		"""
		self._add_triple("=== pyRdfa warning === " + txt)

	def add_info(self, txt) :
		"""Add an informational comment. A comment triplet is added to the separate "warning" graph.
		@param txt: the information text. It will be preceded by the string "==== pyRdfa information ==== "
		"""
		self._add_triple("=== pyRdfa information === " + txt)

	def add_error(self, txt) :
		"""Add an error comment. A comment triplet is added to the separate "warning" graph.
		@param txt: the information text. It will be preceded by the string "==== pyRdfa information ==== "
		"""
		self._add_triple("=== pyRdfa error === " + txt)
		
	def _add_debug(self, txt) :
		self._add_triple("=== DEBUG === " + txt)
		

class Options :
	"""Settable options. An instance of this class is stored in
	the L{execution context<ExecutionContext>} of the parser.

	@ivar space_preserve: whether plain literals should preserve spaces at output or not
	@type space_preserve: Boolean
	@ivar comment_graph: Graph for the storage of warnings
	@type comment_graph: L{CommentGraph}
	@ivar warnings: whether warnings should be generated or not
	@type warnings: Boolean
	@ivar transformers: extra transformers
	@type transformers: list
	@ivar strict_xhtml: Strict XHTML version: do not use the HTML5Lib parser (if available) as a fallback...
	@type strict_xhtml: Boolean
	"""
	def __init__(self, warnings = False, space_preserve = True, transformers=[], strict_xhtml = False) :
		self.space_preserve 	= space_preserve
		self.transformers   	= transformers
		self.strict_xhtml   	= strict_xhtml
		self.comment_graph  	= CommentGraph(warnings) 
		self.warnings			= warnings
		self.html5_parser_used = False


