I want to use a config method ('absolute_asset_spec') in a view handler.
How do I get the config object to invoke it on?
I've seen talk of a Config() method but where is it defined? Searching for it is, shall we say, problematic :)
Is request.registry.settings what you are looking for?
More details here:
Pyramid and .ini configuration
Or you were talking about a method. You want to access a method in every request?
def some_method():
return 'stuff you want'
# in your main function
config.add_request_method(some_method, 'stuff', reify=True)
# in a view
foo = request.stuff
Thanks for the pointer. The answer is:
#view_config(route_name='home', renderer='templates/home.pt')
def home_view(request):
config = Configurator(registry=request.registry)
path = config.absolute_asset_spec('__init__.py')
:
:
Related
I want to create a simple tool but fail to register it correctly. As soon as I add it to any method I get the error:
AttributeError: 'Toolbox' object has no attribute 'authenticate'
I tried
cherrypy.tools.authenticate = cherrypy.Tool('before_handler', authenticate)
and
#cherrypy.tools.register('before_handler')
def authenticate():
The issue I likely have is placing the function in the wrong place. I have a main file launching the server and all apps:
#config stuff
if __name__ == '__main__':
cherrypy.engine.unsubscribe('graceful', cherrypy.log.reopen_files)
logging.config.dictConfig(LOG_CONF)
cherrypy.tree.mount(app1(), '/app1')
cherrypy.tree.mount(app2(), '/app2')
cherrypy.quickstart(app3)
This file is launched by a systemd unit.
If I put the authenticate function in the config area, it doesn't work. If i put it in one of the apps directly and only use it in that app, it doesn't work. Always the same error.
So where do I have to place it to make this work?
Another case of me falling into the python definition order matters trap.
Doesn't work:
class MyApp(object):
#....
#cherrypy.tools.register('on_start_resource')
def authenticate():
#....
Works:
#cherrypy.tools.register('on_start_resource')
def authenticate():
#....
class MyApp(object):
I'm probably missing a larger point of Javascript here, but I wanted to ask the community if the answer is 'NO!'.
Let's say you have an index.js that requires a udp port module:
index.js:
let port1 = require(udp_port.js);
port1.start( { port: 1234, classObj: new myClassObj() } );
udp_port.js:
let dgram = require('dgram');
let msgProcessor; // This is the class obj I'm trying to pass in from index.js
let server = dgram.createSocket('udp4');
exports.start = function(configObj) {
msgProcessor = configObj.classObj; // Can I do this???
}
Any advice would be great at this point, thanks.
In Javascript (Node.js), Is It Possible To Pass an Already Instantiated Class Object into Another Code Module
Yes, it is perfectly possible to pass an instantiated object created in one module to another module. That is done all the time.
Integers and Strings absolutely, but can you pass in a class object? Something you instantiated with the keyword 'new'?
Yes, something instantiated with new can be passed. There are no limitations on what you can pass. Any Javascript type of data. Separate modules loaded into the same nodejs program all run in the same Javascript interpreter. There are no limitations at all on how they can share data with each other.
#James Yep, I had code that I ran before posting but saw mixed results. The code I put in this post was code I cut out of my program. I ended up solving the problem though. What I thought was a limitation of passing variables, was actually a problem related to globals inside a container module (that the instantiated class object was getting passed into).
I was declaring multiple container variables and the global inside was getting reassigned/overwrote with each instantiated class object I passed in.
port1.start(...);
port2.start(...);
had 'msgProcessor' getting assigned twice. Yikes!
I have a custom function which I want to call using builder object
def generate_action(target, source, env):
print "TRIDIP:::::::I am in"
return None
Now I created a builder
env['BUILDERS']['generateAction'] = Builder(action = generate_action)
action = env.generateAction()
As you see, I have not pass any arugument and I don't want to pass any argument for which my custom function generate_action() is not called. I want to call the function without any argument.
You have to call the Builder, specifying the result file you depend on as source. You can't leave the "target=" and "source=" parameters out at the same time, because SCons has to know where in the total build graph this step fits in. Your SConscript should look something like (untested, and from the top of my head):
# env = Environment()
env['BUILDERS']['generateAction'] = Builder(action = generate_action)
# creates your other result file
myResult = env.someOtherBuilder('out.foo', 'in.txt')
# calls your generate_action method
action = env.generateAction('dummy.txt', myResult)
Note, how this will always call your "generateAction" Builder, because the "dummy.txt" is never created, so the target is never up-to-date.
Finally, my guess is that this answer won't really help and lead you into more trouble. When people try to call custom methods at build time they're usually using SCons in a wrong way...most of the time because they don't have the correct mental model to understand how SCons works. You might want to read up on some basics in the UserGuide ( http://scons.org/doc/production/HTML/scons-user.html ) or ask your further questions on our User mailing list at scons-users#scons.org (see also http://scons.org/lists.php ).
How do I supply a configured value to a #view_config-decorated function or class?
E.g.
#view_config(route_name='example', renderer=some_config['template.name'])
class MyClass(BaseView):
...
Or
#view_defaults(route_name='example', renderer=some_config['template.name2'])
class MyClass2(BaseView):
...
Or
#view_config(route_name='example', renderer=some_config['template.name3'])
def method3(request):
...
It's very hard to know where to start, as I'm trying to edit a pyramid plugin, which pulls together its config in an includeme function, so it doesn't have anything obvious that I can include, and it's hard to know what's available to the #view_config decorator.
You can add views using declarative configuration (what you are doing now using #view_config or alternatively using imperative configuration by calling config.add_view() method.
In this case, as you need to access the Pyramid registry and settings file, it is easier to do adding the views imperatively.
In your __init__.py you can do:
settings = config.registry.settings
# You need to call config.add_route("foobar") to map view to URL also
config.add_view('views.MyClass', route_name="foobar", renderer=settings['template.name3'])
Then in your views.py:
class MyClass(BaseView):
pass
#view_config() and add_view() arguments are equal.
I thin kyou can also mix view_config and add_view() arguments for the same view, but I am not sure aobut this. Hope this helps.
How do I do this? If you could please kindly include the code for message map and the function itself, that would be greatly appreciated.
EDIT:
More specifically I am wondering how OnFileSave() links to OnSaveDocument(LPCSTR lpszPathName)
How does OnFileSave get lpszPathName?
You don't need to do anything special to override OnSaveDocument(...) it's already a virtual function in CDocument, so your derived class can just declare virtual BOOL OnSaveDocument(LPCTSTR lpszPathName); in it's header, then implement it in the document. Nothing is needed in the message map. OnSaveDocument will be called by the framework as part of OnFileSave which is a handler in the base class for ID_FILE_SAVE. The lpszPathName refers to m_strPathName when called by OnFileSafe, which is set when opening a file or by calling SetPathName. If it's empty when saving, the user is prompted for a file name.
CDocument::OnFileSave is the message handler for the Save menu command. To handle it yourself put this in your document class message map:
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
and add your function:
void CYOURDOCUMENT::OnFileSave()
{
CDocument::OnFileSave();
}
To see everything it does put a breakpoint in your function and start single-stepping.