Hi Tim:<br><br>So what you are saying is that it would be perfectly acceptable to have a "for loop" for each Win32_* query and that there is no good way to generalise that behaviour. <br><br>eg. <br><br>class Process()<br>
...<br>class Product()<br>
...<br>
class Event()<br>
...<br>
class Service()<br>
.... <br>
<br>
if __name__ == '__main__':<br>
setup_all (True)<br>
c = wmi.WMI ()<br>
for process in c.Win32_Process (['ProcessId', 'Caption']):<br>
d = dict ((p, getattr (process, p)) for p in process.properties)<br>
Process (**d)<br>
<br>
for product in c.Win32_Products (['ProductId', 'Caption']):<br>
d = dict ((p, getattr (process, p)) for p in product.properties)<br>
Product (**d)<br>
<br>
for event in c.Win32_NTEvent (['EventId', 'Caption']):<br>
d = dict ((p, getattr (event, p)) for p in event.properties)<br>
Event (**d)<br>
<br>
for service in c.Win32_Service (['ServiceId', 'Caption']):<br>
d = dict ((p, getattr (service, p)) for p in service.properties)<br>
Service (**d)<br>
<br><br> ....<br> etc etc.<br>
session.flush ()<br>
session.close ()<br><br>Thanks for your patience.<br>
<br>
Cheers<br>
Mark<br>
<br><div><span class="gmail_quote">On 11/03/2008, <b class="gmail_sendername">Tim Golden</b> <<a href="mailto:mail@timgolden.me.uk">mail@timgolden.me.uk</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
mark.a.brand wrote:<br> > sorry - client as in customer<br> <br> <br>Ah. I see. (Didn't think of that :)<br> <br><br> > i will try and explain better, but its not quite clear in my mind what is<br> > achievable ...<br>
><br> > so to build up these database tables, i have to have some queries happening<br> > of the form :<br> ><br> > for list in Win32_Process()<br> > add instance to table-process<br> ><br> > for list in Win32_Service()<br>
> add instance to table-service<br> ><br> > ...<br> > etc etc for each Win32_* query i wanted to implement.<br> ><br> > what I am looking for is an object based / pythonic way to replace all those<br> > "for loops".<br>
<br> <br>Certainly possible at several levels. There are a number<br> of ORM packages around, but the general favourite is probably<br> sqlalchemy [1] and its spin-off Elixir [2]. That said, there's<br> nothing to stop you working straight at the DBAPI [3] level<br>
and calling .executemany against the tables you want.<br> <br> Certainly you could have your table structure exactly mimic<br> the "column" structure of the corresponding WMI class and<br> push things along a bit that way. It's fairly trivial to<br>
turn the WMI attribute values into a dictionary which could<br> then be passed on to something like Elixir's row-creation.<br> <br> <vague hand-wavy code><br> import os, sys<br> from elixir import *<br> import wmi<br>
<br> DB_FILENAME = os.path.abspath ("wmi.db")<br> metadata.bind = "sqlite:///%s" % DB_FILENAME<br> <br> class Process (Entity):<br> <br> ProcessId = Field (Integer, primary_key=True)<br> Caption = Field (Unicode (100))<br>
<br> if __name__ == '__main__':<br> setup_all (True)<br> c = wmi.WMI ()<br> for process in c.Win32_Process (['ProcessId', 'Caption']):<br> d = dict ((p, getattr (process, p)) for p in process.properties)<br>
Process (**d)<br> <br> session.flush ()<br> session.close ()<br> <br> </code><br> <br> Obviously, this is a mere stub but it does at least work<br> and might be a useful basis. There is definite scope for<br>
factoring things out but you might do better just to have<br> a function which took -- or inferred -- the relevant class<br> and table names. YMMV.<br> <br><br> > thanks for you assistance tim.<br> <br> <br>Glad to be of service<br>
<br> :)<br> <br> TJG<br> <br> [1] <a href="http://sqlalchemy.org">http://sqlalchemy.org</a><br> [2] <a href="http://elixir.ematia.de/trac/wiki">http://elixir.ematia.de/trac/wiki</a><br> [3] <a href="http://www.python.org/dev/peps/pep-0249/">http://www.python.org/dev/peps/pep-0249/</a><br>
<br><br> _______________________________________________<br> python-win32 mailing list<br> <a href="mailto:python-win32@python.org">python-win32@python.org</a><br> <a href="http://mail.python.org/mailman/listinfo/python-win32">http://mail.python.org/mailman/listinfo/python-win32</a><br>
</blockquote></div><br>