<s> """<STR_LIT>""" <EOL> import sys <EOL> import posixpath <EOL> from os import path <EOL> from six . moves import cPickle as pickle <EOL> from jinja2 import Environment , FileSystemLoader <EOL> from docutils . core import publish_parts <EOL> from sphinx . application import Sphinx <EOL> from sphinx . locale import _ <EOL> from sphinx . util . osutil import ensuredir <EOL> from sphinx . util . jsonimpl import dumps as dump_json <EOL> from sphinx . util . pycompat import htmlescape <EOL> from sphinx . websupport import errors <EOL> from sphinx . websupport . search import BaseSearch , SEARCH_ADAPTERS <EOL> from sphinx . websupport . storage import StorageBackend <EOL> class WebSupport ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> srcdir = None , <EOL> builddir = '<STR_LIT>' , <EOL> datadir = None , <EOL> staticdir = None , <EOL> doctreedir = None , <EOL> search = None , <EOL> storage = None , <EOL> status = sys . stdout , <EOL> warning = sys . stderr , <EOL> moderation_callback = None , <EOL> allow_anonymous_comments = True , <EOL> docroot = '<STR_LIT>' , <EOL> staticroot = '<STR_LIT>' , <EOL> ) : <EOL> self . srcdir = srcdir <EOL> self . builddir = builddir <EOL> self . outdir = path . join ( builddir , '<STR_LIT:data>' ) <EOL> self . datadir = datadir or self . outdir <EOL> self . staticdir = staticdir or path . join ( self . builddir , '<STR_LIT>' ) <EOL> self . doctreedir = staticdir or path . join ( self . builddir , '<STR_LIT>' ) <EOL> self . staticroot = staticroot . strip ( '<STR_LIT:/>' ) <EOL> self . docroot = docroot . strip ( '<STR_LIT:/>' ) <EOL> self . status = status <EOL> self . warning = warning <EOL> self . moderation_callback = moderation_callback <EOL> self . allow_anonymous_comments = allow_anonymous_comments <EOL> self . _init_templating ( ) <EOL> self . _init_search ( search ) <EOL> self . _init_storage ( storage ) <EOL> self . _globalcontext = None <EOL> self . _make_base_comment_options ( ) <EOL> def _init_storage ( self , storage ) : <EOL> if isinstance ( storage , StorageBackend ) : <EOL> self . storage = storage <EOL> else : <EOL> from sphinx . websupport . storage . sqlalchemystorage import SQLAlchemyStorage <EOL> if not storage : <EOL> db_path = path . join ( self . datadir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ensuredir ( path . dirname ( db_path ) ) <EOL> storage = '<STR_LIT>' + db_path <EOL> self . storage = SQLAlchemyStorage ( storage ) <EOL> def _init_templating ( self ) : <EOL> import sphinx <EOL> template_path = path . join ( sphinx . package_dir , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> loader = FileSystemLoader ( template_path ) <EOL> self . template_env = Environment ( loader = loader ) <EOL> def _init_search ( self , search ) : <EOL> if isinstance ( search , BaseSearch ) : <EOL> self . search = search <EOL> else : <EOL> mod , cls = SEARCH_ADAPTERS [ search or '<STR_LIT:null>' ] <EOL> mod = '<STR_LIT>' + mod <EOL> SearchClass = getattr ( __import__ ( mod , None , None , [ cls ] ) , cls ) <EOL> search_path = path . join ( self . datadir , '<STR_LIT>' ) <EOL> self . search = SearchClass ( search_path ) <EOL> self . results_template = self . template_env . get_template ( '<STR_LIT>' ) <EOL> def build ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . srcdir : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> app = Sphinx ( self . srcdir , self . srcdir , self . outdir , self . doctreedir , <EOL> '<STR_LIT>' , status = self . status , warning = self . warning ) <EOL> app . builder . set_webinfo ( self . staticdir , self . staticroot , <EOL> self . search , self . storage ) <EOL> self . storage . pre_build ( ) <EOL> app . build ( ) <EOL> self . storage . post_build ( ) <EOL> def get_globalcontext ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _globalcontext : <EOL> infilename = path . join ( self . datadir , '<STR_LIT>' ) <EOL> f = open ( infilename , '<STR_LIT:rb>' ) <EOL> try : <EOL> self . _globalcontext = pickle . load ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> return self . _globalcontext <EOL> def get_document ( self , docname , username = '<STR_LIT>' , moderator = False ) : <EOL> """<STR_LIT>""" <EOL> docpath = path . join ( self . datadir , '<STR_LIT>' , docname ) <EOL> if path . isdir ( docpath ) : <EOL> infilename = docpath + '<STR_LIT>' <EOL> if not docname : <EOL> docname = '<STR_LIT:index>' <EOL> else : <EOL> docname += '<STR_LIT>' <EOL> else : <EOL> infilename = docpath + '<STR_LIT>' <EOL> try : <EOL> f = open ( infilename , '<STR_LIT:rb>' ) <EOL> except IOError : <EOL> raise errors . DocumentNotFoundError ( <EOL> '<STR_LIT>' % docname ) <EOL> try : <EOL> document = pickle . load ( f ) <EOL> finally : <EOL> f . close ( ) <EOL> comment_opts = self . _make_comment_options ( username , moderator ) <EOL> comment_meta = self . _make_metadata ( <EOL> self . storage . get_metadata ( docname , moderator ) ) <EOL> document [ '<STR_LIT>' ] = comment_opts + comment_meta + document [ '<STR_LIT>' ] <EOL> return document <EOL> def get_search_results ( self , q ) : <EOL> """<STR_LIT>""" <EOL> results = self . search . query ( q ) <EOL> ctx = { <EOL> '<STR_LIT:q>' : q , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : results , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:_>' : _ , <EOL> } <EOL> document = { <EOL> '<STR_LIT:body>' : self . results_template . render ( ctx ) , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return document <EOL> def get_data ( self , node_id , username = None , moderator = False ) : <EOL> """<STR_LIT>""" <EOL> return self . storage . get_data ( node_id , username , moderator ) <EOL> def delete_comment ( self , comment_id , username = '<STR_LIT>' , moderator = False ) : <EOL> """<STR_LIT>""" <EOL> return self . storage . delete_comment ( comment_id , username , moderator ) <EOL> def add_comment ( self , text , node_id = '<STR_LIT>' , parent_id = '<STR_LIT>' , displayed = True , <EOL> username = None , time = None , proposal = None , <EOL> moderator = False ) : <EOL> """<STR_LIT>""" <EOL> if username is None : <EOL> if self . allow_anonymous_comments : <EOL> username = '<STR_LIT>' <EOL> else : <EOL> raise errors . UserNotAuthorizedError ( ) <EOL> parsed = self . _parse_comment_text ( text ) <EOL> comment = self . storage . add_comment ( parsed , displayed , username , <EOL> time , proposal , node_id , <EOL> parent_id , moderator ) <EOL> comment [ '<STR_LIT>' ] = text <EOL> if not displayed and self . moderation_callback : <EOL> self . moderation_callback ( comment ) <EOL> return comment <EOL> def process_vote ( self , comment_id , username , value ) : <EOL> """<STR_LIT>""" <EOL> value = int ( value ) <EOL> if not - <NUM_LIT:1> <= value <= <NUM_LIT:1> : <EOL> raise ValueError ( '<STR_LIT>' % value ) <EOL> self . storage . process_vote ( comment_id , username , value ) <EOL> def update_username ( self , old_username , new_username ) : <EOL> """<STR_LIT>""" <EOL> self . storage . update_username ( old_username , new_username ) <EOL> def accept_comment ( self , comment_id , moderator = False ) : <EOL> """<STR_LIT>""" <EOL> if not moderator : <EOL> raise errors . UserNotAuthorizedError ( ) <EOL> self . storage . accept_comment ( comment_id ) <EOL> def _make_base_comment_options ( self ) : <EOL> """<STR_LIT>""" <EOL> self . base_comment_opts = { } <EOL> if self . docroot != '<STR_LIT>' : <EOL> comment_urls = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> for key , value in comment_urls : <EOL> self . base_comment_opts [ key ] = '<STR_LIT:/>' + posixpath . join ( self . docroot , value ) <EOL> if self . staticroot != '<STR_LIT>' : <EOL> static_urls = [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] <EOL> for key , value in static_urls : <EOL> self . base_comment_opts [ key ] = '<STR_LIT:/>' + posixpath . join ( self . staticroot , '<STR_LIT>' , value ) <EOL> def _make_comment_options ( self , username , moderator ) : <EOL> """<STR_LIT>""" <EOL> rv = self . base_comment_opts . copy ( ) <EOL> if username : <EOL> rv . update ( { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:username>' : username , <EOL> '<STR_LIT>' : moderator , <EOL> } ) <EOL> return '''<STR_LIT>''' % dump_json ( rv ) <EOL> def _make_metadata ( self , data ) : <EOL> return '''<STR_LIT>''' % dump_json ( data ) <EOL> def _parse_comment_text ( self , text ) : <EOL> settings = { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> try : <EOL> ret = publish_parts ( text , writer_name = '<STR_LIT:html>' , <EOL> settings_overrides = settings ) [ '<STR_LIT>' ] <EOL> except Exception : <EOL> ret = htmlescape ( text ) <EOL> return ret </s>
