def test_get_visual_attr(pylonsapp): c = ContextObj() assert None is helpers.get_visual_attr(c, 'fakse') # emulate the c.visual behaviour c.visual = AttributeDict({}) assert None is helpers.get_visual_attr(c, 'some_var') c.visual.some_var = 'foobar' assert 'foobar' == helpers.get_visual_attr(c, 'some_var')
def fake_request(pylons_config, server_name='mediacore.example', language='en', method='GET', post_vars=None): app_globals = pylons_config['pylons.app_globals'] pylons.app_globals._push_object(app_globals) if post_vars and method.upper() != 'POST': raise ValueError('You must not specify post_vars for request method %r' % method) wsgi_environ = create_wsgi_environ('http://%s' % server_name, method.upper(), request_body=post_vars) request = Request(wsgi_environ, charset='utf-8') request.language = language request.settings = app_globals.settings pylons.request._push_object(request) response = Response(content_type='application/xml', charset='utf-8') pylons.response._push_object(response) session = SessionObject(wsgi_environ) pylons.session._push_object(session) routes_url = URLGenerator(pylons_config['routes.map'], wsgi_environ) pylons.url._push_object(routes_url) tmpl_context = ContextObj() tmpl_context.paginators = Bunch() pylons.tmpl_context._push_object(tmpl_context) # some parts of Pylons (e.g. Pylons.controllers.core.WSGIController) # use the '.c' alias instead. pylons.c = pylons.tmpl_context paste_registry = Registry() paste_registry.prepare() engines = create_tw_engine_manager(app_globals) host_framework = PylonsHostFramework(engines=engines) paste_registry.register(tw.framework, host_framework) mediacore_i18n_path = os.path.join(os.path.dirname(mediacore.__file__), 'i18n') translator = Translator(language, dict(mediacore=mediacore_i18n_path)) # not sure why but sometimes pylons.translator is not a StackedObjectProxy # but just a regular Translator. if not hasattr(pylons.translator, '_push_object'): pylons.translator = StackedObjectProxy() paste_registry.replace(pylons.translator, translator) wsgi_environ.update({ 'pylons.pylons': pylons, 'paste.registry': paste_registry, }) return request
def create_request(path, environ=None): """Helper used in test cases to quickly setup a request obj. ``path`` The path will become PATH_INFO ``environ`` Additional environment Returns an instance of the `webob.Request` object. """ # setup the environ if environ is None: environ = {} environ.update(default_environ) # create a "blank" WebOb Request object # using Pylon's Request which is a webob Request plus # some compatibility methods req = Request.blank(path, environ) # setup a Registry reg = environ.setdefault('paste.registry', Registry()) reg.prepare() # setup pylons.request to point to our Registry reg.register(pylons.request, req) # setup tmpl context tmpl_context._push_object(ContextObj()) url._push_object(URLGenerator(default_map, environ)) return req
def setup_app_env(self, environ, start_response): """Setup and register all the Pylons objects with the registry After creating all the global objects for use in the request, :meth:`~PylonsApp.register_globals` is called to register them in the environment. """ if self.log_debug: log.debug("Setting up Pylons stacked object globals") # Setup the basic pylons global objects req_options = self.request_options req = Request(environ, charset=req_options['charset'], unicode_errors=req_options['errors'], decode_param_names=req_options['decode_param_names']) req.language = req_options['language'] req.config = self.config req.link, req.route_dict = environ['wsgiorg.routing_args'] response = Response(content_type=self.response_options['content_type'], charset=self.response_options['charset']) response.headers.update(self.response_options['headers']) # Store a copy of the request/response in environ for faster access pylons_obj = PylonsContext() pylons_obj.config = self.config pylons_obj.request = req pylons_obj.response = response pylons_obj.app_globals = self.globals pylons_obj.h = self.helpers if 'routes.url' in environ: pylons_obj.url = environ['routes.url'] environ['pylons.pylons'] = pylons_obj environ['pylons.environ_config'] = self.environ_config # Setup the translator object lang = self.config['lang'] pylons_obj.translator = _get_translator(lang, pylons_config=self.config) if self.config['pylons.strict_tmpl_context']: tmpl_context = ContextObj() else: tmpl_context = AttribSafeContextObj() pylons_obj.tmpl_context = req.tmpl_context = tmpl_context if self._session_key in environ: pylons_obj.session = req.session = environ[self._session_key] if self._cache_key in environ: pylons_obj.cache = environ[self._cache_key] # Load the globals with the registry if around if 'paste.registry' in environ: self.register_globals(environ)
def setUp(self): tmpl_options = {} tmpl_options['genshi.search_path'] = ['tests'] self._ctx = ContextObj() tmpl_context._push_object(self._ctx) self._buffet = pylons.templating.Buffet( default_engine='genshi',tmpl_options=tmpl_options ) pylons.buffet._push_object(self._buffet) setup_session_dir()
def setUp(self): tmpl_options = {} tmpl_options['genshi.search_path'] = ['tests'] self._ctx = ContextObj() tmpl_context._push_object(self._ctx) warnings.simplefilter("ignore") pylons.config.push_process_config(default_config) warnings.resetwarnings() setup_session_dir()
def init_stack(config=None): if not config: config = pylons.test.pylonsapp.config url._push_object(URLGenerator(config['routes.map'], environ)) pylons.app_globals._push_object(config['pylons.app_globals']) pylons.config._push_object(config) pylons.tmpl_context._push_object(ContextObj()) # Initialize a translator for tests that utilize i18n translator = _get_translator(pylons.config.get('lang')) pylons.translator._push_object(translator)
def setUp(self): c = ContextObj() py_obj = PylonsContext() py_obj.tmpl_context = c py_obj.request = py_obj.response = None self.environ = { 'pylons.routes_dict': dict(action='index'), 'paste.config': dict(global_conf=dict(debug=True)), 'pylons.pylons': py_obj } pylons.tmpl_context._push_object(c)
def setUp(self): if not os.path.exists(session_dir): os.makedirs(session_dir) # Setting TG2 up: c = ContextObj() py_obj = PylonsContext() py_obj.c = c py_obj.request = py_obj.response = None environ = {'pylons.routes_dict': dict(action='index'), 'pylons.pylons': py_obj} pylons.tmpl_context._push_object(c) self.app = make_app(DefaultLessTGController, environ, with_errors=True)
def init_fake_request(self, server_name='mediacore.example', language='en', method='GET', post_vars=None): app_globals = self.pylons_config['pylons.app_globals'] pylons.app_globals._push_object(app_globals) if post_vars and method.upper() != 'POST': raise ValueError('You must not specify post_vars for request method %r' % method) wsgi_environ = create_wsgi_environ('http://%s' % server_name, method.upper(), request_body=post_vars) request = Request(wsgi_environ, charset='utf-8') request.language = language request.settings = app_globals.settings pylons.request._push_object(request) response = Response(content_type='application/xml', charset='utf-8') pylons.response._push_object(response) session = SessionObject(wsgi_environ) pylons.session._push_object(session) routes_url = URLGenerator(self.pylons_config['routes.map'], wsgi_environ) pylons.url._push_object(routes_url) tmpl_context = ContextObj() tmpl_context.paginators = Bunch() pylons.tmpl_context._push_object(tmpl_context) # some parts of Pylons (e.g. Pylons.controllers.core.WSGIController) # use the '.c' alias instead. pylons.c = pylons.tmpl_context paste_registry = Registry() paste_registry.prepare() engines = create_tw_engine_manager(app_globals) host_framework = PylonsHostFramework(engines=engines) paste_registry.register(tw.framework, host_framework) wsgi_environ.update({ 'pylons.pylons': pylons, 'paste.registry': paste_registry, }) return request
def pylons_compatibility_tween(request): """ While migrating from pylons to pyramid we need to call some pylons code from pyramid. For example while rendering an old template that uses the 'c' or 'h' objects. This tween sets up the needed pylons globals. """ try: config = rhodecode.CONFIG environ = request.environ session = request.session session_key = (config['pylons.environ_config'].get( 'session', 'beaker.session')) # Setup pylons globals. pylons.config._push_object(config) pylons.request._push_object(request) pylons.session._push_object(session) environ[session_key] = session pylons.url._push_object(URLGenerator(config['routes.map'], environ)) # TODO: Maybe we should use the language from pyramid. translator = _get_translator(config.get('lang')) pylons.translator._push_object(translator) # Get the rhodecode auth user object and make it available. auth_user = get_auth_user(environ) request.user = auth_user environ['rc_auth_user'] = auth_user # Setup the pylons context object ('c') context = ContextObj() context.rhodecode_user = auth_user attach_context_attributes(context) pylons.tmpl_context._push_object(context) return handler(request) finally: # Dispose current database session and rollback uncommitted # transactions. meta.Session.remove()
def _init_stack(config=None, environ=None): if not config: config = pylons.test.pylonsapp.config if not environ: environ = {} pylons.url._push_object(URLGenerator(config['routes.map'], environ or {})) pylons.app_globals._push_object(config['pylons.app_globals']) pylons.config._push_object(config) pylons.tmpl_context._push_object(ContextObj()) # Initialize a translator for tests that utilize i18n translator = _get_translator(pylons.config.get('lang')) pylons.translator._push_object(translator) pylons.session._push_object(SessionObject(environ or {})) pylons.request._push_object(webob.Request.blank('', environ=environ))
def setUp(self): # Creating the session dir: if not os.path.exists(session_dir): os.makedirs(session_dir) # Setting TG2 up: c = ContextObj() py_obj = PylonsContext() py_obj.c = c py_obj.request = py_obj.response = None environ = {'pylons.routes_dict': dict(action='index'), 'pylons.pylons': py_obj} pylons.tmpl_context._push_object(c) # Finally, the app: self.app = make_app(self.controller, environ)
def setUp(self): # Creating the session dir: if not os.path.exists(session_dir): os.makedirs(session_dir) # Setting Pylons up: c = ContextObj() py_obj = PylonsContext() py_obj.c = c py_obj.request = py_obj.response = None self.environ = { 'pylons.routes_dict': dict(action='index'), 'paste.config': dict(global_conf=dict(debug=True)), 'pylons.pylons': py_obj, } pylons.c._push_object(c) # Finally, the app: self.app = make_app(self.controller, self.environ)
class ModelTest(object): config = appconfig('config:test.ini', relative_to=conf_dir) root_path = os.path.join(conf_dir, 'r2') paths = { 'root': root_path, 'controllers': os.path.join(root_path, 'controllers'), 'templates': tmpl_dirs, 'static_files': os.path.join(root_path, 'public') } registry = Registry() registry.prepare() globals = Globals(config.global_conf, config.local_conf, paths) registry.register(pylons.g, globals) registry.register(pylons.translator, NullTranslations()) context_obj=ContextObj() registry.register(pylons.c, context_obj) r2.config.cache = globals.cache
def __call__(self, environ, start_response): registry = environ['paste.registry'] py_obj = PylonsContext() environ_config = environ.setdefault('pylons.environ_config', {}) if self.setup_cache: py_obj.cache = environ['beaker.cache'] registry.register(pylons.cache, environ['beaker.cache']) environ_config['cache'] = 'beaker.cache' if self.setup_session: py_obj.session = environ['beaker.session'] registry.register(pylons.session, environ['beaker.session']) environ_config['session'] = 'beaker.session' if self.setup_g: py_obj.g = py_obj.app_globals = self.g registry.register(pylons.g, self.g) registry.register(pylons.app_globals, self.g) translator = gettext.NullTranslations() py_obj.translator = translator registry.register(pylons.translator, translator) # Update the environ req = Request( environ, charset=request_defaults['charset'], unicode_errors=request_defaults['errors'], decode_param_names=request_defaults['decode_param_names']) req.language = request_defaults['language'] response = Response(content_type=response_defaults['content_type'], charset=response_defaults['charset']) response.headers.update(response_defaults['headers']) environ.update(self.environ) py_obj.config = pylons.config._current_obj() py_obj.request = req py_obj.response = response py_obj.c = ContextObj() environ['pylons.pylons'] = py_obj registry.register(pylons.request, req) registry.register(pylons.response, response) if 'routes.url' in environ: registry.register(pylons.url, environ['routes.url']) return self.app(environ, start_response)
def setup_app_env(self, environ, start_response): """Setup and register all the Pylons objects with the registry""" if self.log_debug: log.debug("Setting up Pylons stacked object globals") registry = environ['paste.registry'] registry.register(WSGIRequest.defaults, self.request_options) registry.register(WSGIResponse.defaults, self.response_options) req = WSGIRequest(environ) # Setup the basic pylons global objects registry.register(pylons.request, req) registry.register(pylons.response, WSGIResponse()) registry.register(pylons.buffet, self.buffet) registry.register(pylons.g, self.globals) registry.register(pylons.config, self.config) registry.register(pylons.h, self.helpers or \ pylons.legacy.load_h(self.package_name)) # Setup the translator global object registry.register(pylons.translator, gettext.NullTranslations()) lang = self.config.get('lang') if lang: set_lang(lang) if self.config['pylons.strict_c']: registry.register(pylons.c, ContextObj()) else: registry.register(pylons.c, AttribSafeContextObj()) econf = environ['pylons.environ_config'] if econf.get('session'): registry.register(pylons.session, environ[econf['session']]) if econf.get('cache'): registry.register(pylons.cache, environ[econf['cache']])
def main(argv): log = logging.getLogger() logging.basicConfig(level=logging.INFO) conf = appconfig('config:development.ini', relative_to='.') config = None if not pylons.test.pylonsapp: config = load_environment(conf.global_conf, conf.local_conf) # Create the tables if they don't already exist #metadata.create_all(bind=Session.bind) c = ContextObj() py_obj = PylonsContext() py_obj.tmpl_context = c pylons.tmpl_context._push_object(c) #corpushistory = model.meta.Session.query(model.Corpusversion).all() corpusversion = model.meta.Session.query(model.Corpusversion).order_by(model.Corpusversion.updated).first() c.iso_time = corpusversion.updated.strftime("%Y-%m-%d") c.version_number = "{0}.{1}".format(corpusversion.version, corpusversion.revision) # template_entries_seg mylookup = TemplateLookup(directories=config['pylons.paths']['templates']) template_header = open(os.path.join(config['pylons.paths']['templates'][0], 'base', 'graf-header.hdr')).read() template_entries = open(os.path.join(config['pylons.paths']['templates'][0], 'base', 'graf-entries.txt')).read() template_entries_seg = open(os.path.join(config['pylons.paths']['templates'][0], 'base', 'graf-entries.xml')).read() template_annotations = open(os.path.join(config['pylons.paths']['templates'][0], 'base', 'graf-annotations.xml')).read() #template_annotations_seg = open(os.path.join(config['pylons.paths']['templates'][0], 'base', 'graf-annotations-seg.xml')).read() metadata_file = codecs.open(os.path.join(config['pylons.paths']['static_files'], 'downloads', "xml", "sources.csv"), "w", "utf-8") metadata_file.write("ID\tTYPE\tLANGUAGES\tIS_READY\tTITLE\tCOMPONENT\n") #http://www.cidles.eu/quanthistling/book/minor1987/hto/spa?format=xml for b in quanthistling.dictdata.books.list + quanthistling.dictdata.toolboxfiles.list: #if b['bibtex_key'] != "leach1969": # continue c.book = model.meta.Session.query(model.Book).filter_by(bibtex_key=b['bibtex_key']).first() if c.book: # escape characters for XML c.bookinfo = escape(c.book.bookinfo()) c.book_title = escape(c.book.title) c.book_author = escape(c.book.author) # collect book data languages = [ l.language_iso.langcode for dictdata in c.book.dictdata for l in dictdata.src_languages + dictdata.tgt_languages if l.language_iso] components = [ dictdata.component.name for dictdata in c.book.dictdata ] metadata_file.write(u"{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n".format(c.book.bibtex_key, "dictionary", ",".join(languages), c.book.is_ready, c.book.bookinfo(), ",".join(components))) print "Exporting XML data for %s..." % b['bibtex_key'] #temppath = tempfile.mkdtemp() temppath = os.path.join(config['pylons.paths']['static_files'], 'downloads', 'xml', b['bibtex_key']) if not os.path.exists(temppath): os.mkdir(temppath) else: files = glob.glob(os.path.join(temppath, "*")) for f in files: os.remove(f) for c.dictdata in c.book.dictdata: c.url_for = url_for c.base_url = "http://www.quanthistling.info/data" #c.relative_url = url_for(controller='book', action='dictdata', bibtexkey=c.book.bibtex_key, startpage=c.dictdata.startpage, endpage=c.dictdata.endpage, format='html') #c.heading = c.book.bookinfo() c.basename = "dict-%s-%i-%i" % (b['bibtex_key'], c.dictdata.startpage, c.dictdata.endpage) print " getting entries..." c.entries = model.meta.Session.query(model.Entry).filter(model.Entry.dictdata_id==c.dictdata.id).order_by("startpage", "pos_on_page").all() print " getting annotations..." annotations = model.meta.Session.query(model.Annotation).join(model.Entry, model.Annotation.entry_id==model.Entry.id).filter(model.Entry.dictdata_id==c.dictdata.id).order_by("startpage", "pos_on_page").all() c.annotations = collections.defaultdict(dict) for a in annotations: if not c.annotations[a.entry_id]: c.annotations[a.entry_id] = collections.defaultdict(list) c.annotations[a.entry_id][(a.start, a.end)].append(a) print " getting counts..." c.count_heads = model.meta.Session.query(model.Annotation).join(model.Entry, model.Annotation.entry_id==model.Entry.id).filter(model.Entry.dictdata_id==c.dictdata.id).filter(model.Annotation.value==u"head").count() c.count_translations = model.meta.Session.query(model.Annotation).join(model.Entry, model.Annotation.entry_id==model.Entry.id).filter(model.Entry.dictdata_id==c.dictdata.id).filter(model.Annotation.value==u"translation").count() c.count_pos = model.meta.Session.query(model.Annotation).join(model.Entry, model.Annotation.entry_id==model.Entry.id).filter(model.Entry.dictdata_id==c.dictdata.id).filter(model.Annotation.value==u"pos").count() c.count_examples_src = model.meta.Session.query(model.Annotation).join(model.Entry, model.Annotation.entry_id==model.Entry.id).filter(model.Entry.dictdata_id==c.dictdata.id).filter(model.Annotation.value==u"example-src").count() c.count_examples_tgt = model.meta.Session.query(model.Annotation).join(model.Entry, model.Annotation.entry_id==model.Entry.id).filter(model.Entry.dictdata_id==c.dictdata.id).filter(model.Annotation.value==u"example-tgt").count() c.count_manually_corrected = model.meta.Session.query(model.Entry).filter(model.Entry.dictdata_id==c.dictdata.id).filter(model.Entry.has_manual_annotations==True).count() #xml = render('/derived/book/dictdata.xml') #xml = literal(template.render_unicode(c)) print " header..." # write header xml = Template(template_header, lookup=mylookup).render_unicode(c=c) oFile = open(os.path.join(temppath, "%s.hdr" % c.basename),'wb') oFile.write(xml.encode("utf-8")) oFile.close() print " base data..." # write base data file xml = Template(template_entries, lookup=mylookup).render_unicode(c=c) oFile = open(os.path.join(temppath, "%s.txt" % c.basename),'wb') oFile.write(xml.encode("utf-8")) oFile.close() print " entries..." # write entry file xml = Template(template_entries_seg, lookup=mylookup).render_unicode(c=c) oFile = open(os.path.join(temppath, "%s-entries.xml" % c.basename),'wb') oFile.write(xml.encode("utf-8")) oFile.close() print " formatting annotations..." c.annotationtypes = [ "pagelayout", "formatting" ] c.annotationname = "formatting" xml = Template(template_annotations, lookup=mylookup).render_unicode(c=c) oFile = open(os.path.join(temppath, "%s-formatting.xml" % c.basename),'wb') oFile.write(xml.encode("utf-8")) oFile.close() print " dictinterpretation annotations..." c.annotationtypes = [ "dictinterpretation", "orthographicinterpretation", "errata" ] c.annotationname = "dictinterpretation" xml = Template(template_annotations, lookup=mylookup).render_unicode(c=c) oFile = open(os.path.join(temppath, "%s-dictinterpretation.xml" % c.basename),'wb') oFile.write(xml.encode("utf-8")) oFile.close() # create archive myzip = zipfile.ZipFile(os.path.join(config['pylons.paths']['static_files'], 'downloads', 'xml', '%s.zip' % b['bibtex_key']), 'w', zipfile.ZIP_DEFLATED) for file in glob.glob(os.path.join(temppath, "*.*")): myzip.write(file, os.path.basename(file)) myzip.close() #shutil.rmtree(temppath) metadata_file.close() myzip = zipfile.ZipFile(os.path.join(config['pylons.paths']['static_files'], 'downloads', 'xml', 'data.zip'), 'w', zipfile.ZIP_DEFLATED) graf_dirs = [d for d in glob.glob(os.path.join(config['pylons.paths']['static_files'], 'downloads', 'xml', "*")) if os.path.isdir(d)] for d in graf_dirs: bibtex_key = d[d.rfind(os.sep)+1:] for f in glob.glob(os.path.join(d, "*.*")): myzip.write(f, os.path.join(bibtex_key, os.path.basename(f))) f = os.path.join(config['pylons.paths']['static_files'], 'downloads', 'xml', 'sources.csv') myzip.write(f, os.path.basename(f)) myzip.close() pylons.tmpl_context._pop_object()
def setUp(self): self.environ = {'pylons.routes_dict':dict(action='index'), 'paste.config':dict(global_conf=dict(debug=True))} pylons.c._push_object(ContextObj())