473,508 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find the context of importer

I'm using Python in a scripting environment. The host application would
pass in some objects so that the script can act on it. But there are a
number of things I like to add to every script to make it a decent
environment, for example, setting up exception hook to show error
properly. I tried to factorize this into a module called script_util.
For example:

------------------------------------------------------------------------
script:

# host application would pass in objects like window, document, etc.

import script_util

.... do something with window, document, etc, ...

------------------------------------------------------------------------
script_util.py:

import sys

def myExceptionHandler(...):
importer.window.alert(message)

sys.excepthook = myExceptionHandler
sys.stdout = myOutputPipe
------------------------------------------------------------------------
However in order for script_util to work, it needs to have access to
the importer's context to get access to the host objects. Is there
anyway for it to find out? I guess I can do something like:

------------------------------------------------------------------------
import script_util
script_util.init(globals())
------------------------------------------------------------------------

But I like to make something brain dead easy with just the import
statement if possible.

Thank you,

wy

May 31 '06 #1
1 1220
I find the answer to my own question. The inspect module have what I
need.

Here is the sample code:

a.py
------------------------------------------------------------------------
import b
print 'hello world from module a'
------------------------------------------------------------------------
b.py
------------------------------------------------------------------------
import inspect

def globals_of_importer():
""" Return the global namespace of the module that imports this
module. """

# scan the stack using the inspect module
stack = inspect.stack()

# current frame is on the top of the stack
top_rec = stack[0]
current_filename = top_rec[1]

# look for the frame those filename different from current frame
for frame_rec in stack:
filename = frame_rec[1]
if filename != current_filename:
break
else:
return None

# return the globals from the frame object
frame = frame_rec[0]
members = inspect.getmembers(frame)
for name, value in members:
if name == 'f_globals':
return value
else:
return None

print globals_of_importer()
------------------------------------------------------------------------

wy

Jun 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2128
by: Jaime Wyant | last post by:
Will someone explain this to me? >>> "test".find("") 0 Why is the empty string found at position 0? Thanks! jw
20
3373
by: 2pac | last post by:
in this scenario foo1 ---calls---> foo2 is it possible for me to print out - when the control is within foo2 - the caller of foo2 the information is obviously there in the stack - am wondering...
2
1002
by: Stubbly Wubbly | last post by:
Why am I getting this error? Any ideas/suggestions on how to track it down? I have a project for which I put an "auto notification" in the globabl.asax's Application_Error event to email myself...
7
973
by: MuZZy | last post by:
Hi, I need to be able to find out if the assembly is currently used by an asp.net app or a windows app. Assembly contains a class which has an object factory, but this object factory should...
4
14952
by: cdrom205 | last post by:
static void MDString ( unsigned char *input) { MD5_CTX context; unsigned char digest; unsigned int len = sizeof(input);//strlen (const char*) md5.MD5Init (&context); md5.MD5Update...
0
1103
by: rkmr.em | last post by:
Hi Are there any hotmail / aim / yahoo addressbook importer in python? thanks mark
0
1942
by: Hasin Hayder | last post by:
Today I developed this tool to import wordpress blog rolls as XML document. You know when you export data from wordpress.com that doesn't include the blog rolls data. So if you want to keep a...
0
777
by: Kay Schluehr | last post by:
When trying to import a module from a filesystem directory there are several possibilities. Most obviously one can use an import statement but if this is not sufficient one has also API functions....
0
1471
dmjpro
by: dmjpro | last post by:
When i do .... HashMap props = new HashMap(); props.put(Context.INITIAL_CONTEXT_FACTORY,"FactoryClass"); Context ctx = new InitialContext(props); Here it is obvious that the proper...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.