Re: "Plugin" architecture - how to do?
On Apr 10, 10:26 pm, c james <[EMAIL PROTECTED]> wrote: > Take a look at Trac. This might give you some ideas. > > http://trac.edgewall.org/wiki/TracDev/ComponentArchitecture Thanks cJames, that's exactly what I'm looking for. -- http://mail.python.org/mailman/listinfo/python-list
"Plugin" architecture - how to do?
I'm making a program that consists of a main engine + plugins. Both are in Python. My question is, how do I go about importing arbitrary code and have it be able to use the engine's functions, classes, etc? First, here's how it's laid out on the filesystem: -mainstarterscript.py - -__init__.py -[whole bunch of files] - -__init__.py -main.py -[whole bunch of files] So you'd execute the whole thing with ./mainstarterscript.py, which imports engine. Engine initializes, then executes plugin somehow. Plugin needs to be able to call functions in engine, and vice versa. Although various IPC methods would probably work, I think there's gotta be an easier and more straightforward way to do it. I use execfile to execute the plugin's __init__.py script. I insert some globals from engine into it so that __init__ can use them. __init__.py also imports plugin/main.py, which uses functions the engine functions, and so must import __init__.py. The problem is, when main imports __init__, the inserted globals aren't there, so it dies. So I've tried to make some globals in __init__ that simply map them to the engine's globals. Here's roughly how: #plugin/__init__.py engine_globals = {} if __name__ == '__builtin__': #run via execfile global engine_globals engine_globals = globals() #I don't really want all the globals, but in my real code I go through them one by one else: #imported from one of the other scripts in plugin dir. pass import main #among other things # end __init__.py # plugins/main.py import __init__ __init__.engine_globals.['some_function'](args) #do other stuff #end main.py #engine/__init__.py def some_function(args): pass sys.argv.append('plugins') execfile('plugins/__init__.py') When main.py imports plungin/__init__, it reinitializes engine_globals to {}, of course. But I can't think of a way not to initialize it to something, thus overwriting the original, because it needs to be in the namespace.. Essentially, I want a global to not reset itself if it's already set. So how do I go about doing this? Am I on the right track? Should I be __import__'ing plugins instead of execfile'ing them? Thanks, Nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Looping issues
On Apr 5, 2:01 pm, [EMAIL PROTECTED] wrote: > What I am trying to do is compare two files to each other. > > If the 2nd file contains the same line the first file contains, I want > to print it. I wrote up the following code: > > correct_settings = open("C:\Python25\Scripts\Output > \correct_settings.txt","r") > current_settings = open("C:\Python25\Scripts\Output\output.txt","r") > > for line in correct_settings: > for val in current_settings: > if val == line: > print line + " found." > > correct_settings.close() > current_settings.close() > > For some reason this only looks at the first line of the > correct_settings.txt file. Any ideas as to how i can loop through each > line of the correct_settings file instead of just looking at the first? Instead of "for line in correct_settings", try "for line in correct_settings.readlines()". -- http://mail.python.org/mailman/listinfo/python-list
Re: Looping issues
On Apr 5, 2:27 pm, Larry Bates <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > What I am trying to do is compare two files to each other. > > > If the 2nd file contains the same line the first file contains, I want > > to print it. I wrote up the following code: > > > correct_settings = open("C:\Python25\Scripts\Output > > \correct_settings.txt","r") > > current_settings = open("C:\Python25\Scripts\Output\output.txt","r") > > > for line in correct_settings: > > for val in current_settings: > > if val == line: > > print line + " found." > > > correct_settings.close() > > current_settings.close() > > > For some reason this only looks at the first line of the > > correct_settings.txt file. Any ideas as to how i can loop through each > > line of the correct_settings file instead of just looking at the first? > > If the files aren't terribly large (not tested): > > correct_lines=open(r"C:\Python25\Scripts\Output" \ > "\correct_settings.txt", "r").readlines() > > current_lines=open(r"C:\Python25\Scripts\Output\output.txt", > "r").readlines() > > for line in current_settings: > if line in correct_lines: > print line + " found" > > This does what you asked for but somehow I don't think it is > what you want. I would suggest that you take a look at difflib. > > Somththing along the lines of: > > import difflib > > correct_lines=open(r"C:\Python25\Scripts\Output" \ > "\correct_settings.txt", "r").readlines() > > current_lines=open(r"C:\Python25\Scripts\Output\output.txt", > "r").readlines() > > delta=difflib.unified_diff(correct_lines, current_lines) > diffs=''.join(delta) > > print diffs > > Will show you the lines that are different and some lines > around it for context. > > -Larry Sorry my solution didn't work. The only other thing I can think of is that something is screwy with the newlines, although this seems silly. I've heard Python has "universal newline" support, meaning \n = \r = \r\n, etc. Try printing the contents of the file in its entirety: print current_settings.read() And see if it prints the entire file, or just the first line. I can't imagine it won't print the whole thing. Next, do print current_settings.read().replace('\\',''). This will make the escape characters visible, so you can see exactly what type of newlines it's printing. If the file is in Unix format and you're on Windows, Python may be assuming the latter and not breaking lines correctly. Post what it prints. Nick -- http://mail.python.org/mailman/listinfo/python-list
Re: "Plugin" architecture - how to do?
On Apr 6, 9:59 am, "Nate Finch" <[EMAIL PROTECTED]> wrote: > On Apr 5, 10:57 am, [EMAIL PROTECTED] wrote: > > > I'm making a program that consists of a main engine + plugins. Both > > are in Python. My question is, how do I go about importing arbitrary > > code and have it be able to use the engine's functions, classes, etc? > > For a truepluginarchitecture, you don't have the main engine calling > methods on theplugin. What you do is have an API on your engine with > methods theplugincan call and events it can hook into. The events > are how the engine communicates state to any plugins, without having > to know who they are or what they do. Your engine has a configuration > that tells it what plugins to load (which the plugins presumably > modified when they installed themselves) or otherwise has some > standard way that the engine can figure out what plugins need to be > loaded. > > Now granted, I don't specifically know how to do this via python.. > but, maybe what I've said will send you in the right direction. > > -Nate Alright that's a good suggestion. But the problem I'm having is that I don't know how to execute the plugin. I think I know how to do the architecture, just that there needs to be some crazy circular importing going on and I was wondering if there was a smarter way to attack the problem. Thanks, Nick -- http://mail.python.org/mailman/listinfo/python-list