<s> """<STR_LIT>""" <EOL> from rdflib . term import Literal <EOL> from rdflib . serializer import Serializer <EOL> from rdflib . py3compat import b <EOL> import warnings <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class NTSerializer ( Serializer ) : <EOL> """<STR_LIT>""" <EOL> def serialize ( self , stream , base = None , encoding = None , ** args ) : <EOL> if base is not None : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> if encoding is not None : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> encoding = self . encoding <EOL> for triple in self . store : <EOL> stream . write ( _nt_row ( triple ) . encode ( encoding , "<STR_LIT:replace>" ) ) <EOL> stream . write ( b ( "<STR_LIT:\n>" ) ) <EOL> def _nt_row ( triple ) : <EOL> if isinstance ( triple [ <NUM_LIT:2> ] , Literal ) : <EOL> return u"<STR_LIT>" % ( <EOL> triple [ <NUM_LIT:0> ] . n3 ( ) , <EOL> triple [ <NUM_LIT:1> ] . n3 ( ) , <EOL> _xmlcharref_encode ( _quoteLiteral ( triple [ <NUM_LIT:2> ] ) ) ) <EOL> else : <EOL> return u"<STR_LIT>" % ( triple [ <NUM_LIT:0> ] . n3 ( ) , <EOL> triple [ <NUM_LIT:1> ] . n3 ( ) , <EOL> _xmlcharref_encode ( triple [ <NUM_LIT:2> ] . n3 ( ) ) ) <EOL> def _quoteLiteral ( l ) : <EOL> '''<STR_LIT>''' <EOL> encoded = _quote_encode ( l ) <EOL> if l . language : <EOL> if l . datatype : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return '<STR_LIT>' % ( encoded , l . language ) <EOL> elif l . datatype : <EOL> return '<STR_LIT>' % ( encoded , l . datatype ) <EOL> else : <EOL> return '<STR_LIT:%s>' % encoded <EOL> def _quote_encode ( l ) : <EOL> return '<STR_LIT>' % l . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) <EOL> def _xmlcharref_encode ( unicode_data , encoding = "<STR_LIT:ascii>" ) : <EOL> """<STR_LIT>""" <EOL> res = "<STR_LIT>" <EOL> for char in unicode_data : <EOL> try : <EOL> char . encode ( encoding , '<STR_LIT:strict>' ) <EOL> except UnicodeError : <EOL> if ord ( char ) <= <NUM_LIT> : <EOL> res += '<STR_LIT>' % ord ( char ) <EOL> else : <EOL> res += '<STR_LIT>' % ord ( char ) <EOL> else : <EOL> res += char <EOL> return res </s>
