<s> """<STR_LIT>""" <EOL> from collections import defaultdict <EOL> from rdflib . plugins . serializers . turtle import TurtleSerializer , _GEN_QNAME_FOR_DT , VERB <EOL> from rdflib . term import BNode , Literal <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class TrigSerializer ( TurtleSerializer ) : <EOL> short_name = "<STR_LIT>" <EOL> indentString = <NUM_LIT:4> * u'<STR_LIT:U+0020>' <EOL> def __init__ ( self , store ) : <EOL> if store . context_aware : <EOL> self . contexts = list ( store . contexts ( ) ) <EOL> self . default_context = store . default_context . identifier <EOL> if store . default_context : <EOL> self . contexts . append ( store . default_context ) <EOL> else : <EOL> self . contexts = [ store ] <EOL> self . default_context = None <EOL> super ( TrigSerializer , self ) . __init__ ( store ) <EOL> def preprocess ( self ) : <EOL> for context in self . contexts : <EOL> self . store = context <EOL> self . getQName ( context . identifier ) <EOL> self . _references = defaultdict ( int ) <EOL> self . _subjects = { } <EOL> for triple in context : <EOL> self . preprocessTriple ( triple ) <EOL> self . _contexts [ context ] = ( self . orderSubjects ( ) , self . _subjects , self . _references ) <EOL> def reset ( self ) : <EOL> super ( TrigSerializer , self ) . reset ( ) <EOL> self . _contexts = { } <EOL> def serialize ( self , stream , base = None , encoding = None , <EOL> spacious = None , ** args ) : <EOL> self . reset ( ) <EOL> self . stream = stream <EOL> self . base = base <EOL> if spacious is not None : <EOL> self . _spacious = spacious <EOL> self . preprocess ( ) <EOL> self . startDocument ( ) <EOL> firstTime = True <EOL> for store , ( ordered_subjects , subjects , ref ) in self . _contexts . items ( ) : <EOL> self . _references = ref <EOL> self . _serialized = { } <EOL> self . store = store <EOL> self . _subjects = subjects <EOL> if self . default_context and store . identifier == self . default_context : <EOL> self . write ( self . indent ( ) + '<STR_LIT>' ) <EOL> else : <EOL> if isinstance ( store . identifier , BNode ) : <EOL> iri = store . identifier . n3 ( ) <EOL> else : <EOL> iri = self . getQName ( store . identifier ) <EOL> if iri is None : <EOL> iri = store . identifier . n3 ( ) <EOL> self . write ( self . indent ( ) + '<STR_LIT>' % iri ) <EOL> self . depth += <NUM_LIT:1> <EOL> for subject in ordered_subjects : <EOL> if self . isDone ( subject ) : <EOL> continue <EOL> if firstTime : <EOL> firstTime = False <EOL> if self . statement ( subject ) and not firstTime : <EOL> self . write ( '<STR_LIT:\n>' ) <EOL> self . depth -= <NUM_LIT:1> <EOL> self . write ( '<STR_LIT>' ) <EOL> self . endDocument ( ) <EOL> stream . write ( u"<STR_LIT:\n>" . encode ( '<STR_LIT:ascii>' ) ) </s>
