DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 31st March 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,080
Thanked 198 Times in 156 Posts
Default Python: Use the value of a variable for calling another variable

My code (simplified):

Code:
import _winreg as winreg

SetRegistry(winreg.HKEY_LOCAL_MACHINE, key, values)

def SetRegistry(reg, key, values):
  reghandle = winreg.ConnectRegistry(None, reg)
This works, but I would like to pass HKEY_LOCAL_MACHINE as a string because I want to make a backup of changed registry settings, but how do I pass this on to ConnectRegistry? This for example won't work for obvious reasons (changes in red):

Code:
import _winreg as winreg

SetRegistry('HKEY_LOCAL_MACHINE', key, values)

def SetRegistry(reg, key, values):
  reghandle = winreg.ConnectRegistry(None, winreg.reg)
Unfortunately, I can't use __str__(), since it's just an int:

Code:
>>> import _winreg
>>> print _winreg.HKEY_LOCAL_MACHINE
2147483650
>>> type(_winreg.HKEY_LOCAL_MACHINE)
<type 'long'>
Probably missing something simple ... :-/
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #2   (View Single Post)  
Old 31st March 2009
roddierod's Avatar
roddierod roddierod is offline
Real Name: Rod Person
VPN Cryptographer
 
Join Date: Apr 2008
Location: Pittsburgh, Pa
Posts: 381
Thanked 13 Times in 13 Posts
Default

I've ask in the past on Python list if you can have Variable Variables in Python like PHP and such, and the only answers I ever got were work arounds so I don't think you can do what you want.

I can't see why you'd want to pass a string in this case.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick
Reply With Quote
  #3   (View Single Post)  
Old 31st March 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,080
Thanked 198 Times in 156 Posts
Default

Quote:
I can't see why you'd want to pass a string in this case.
This script sets some registry keys, these keys need to be changed back to the original values after a while, so I write a backup file with a list of keys and their values, the hive name needs to be included obviously. I guess I can pickle, but I want the file to be plain text so it can be examined&modified if needed.

I can use
if reg == 2147483650:
str = 'HKEY_LOCAL_MACHINE'

But does this work on every system config, and on every Windows version (XP, Vista, 2003, 2008)?
Other than that, it is _ugly_, passing two values (str and reg.HKLM/HKCU) would be better...
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #4   (View Single Post)  
Old 31st March 2009
roddierod's Avatar
roddierod roddierod is offline
Real Name: Rod Person
VPN Cryptographer
 
Join Date: Apr 2008
Location: Pittsburgh, Pa
Posts: 381
Thanked 13 Times in 13 Posts
Default

Ok, I see. Trying to write code to use across windows version now there is a headache. Personally I try and stay away from the registry. And wasn't Microsoft suppose to be moving away from the registry with Vista on. Or did that not happen?

Anyway, I'd probably write two function StringFromRegKey and RegKeyFromString which would basically be your if statement.

If the registry key values are imported from a MS dll then the will probably stay the same across version. They are pretty good about not changing the lower level stuff like that.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick
Reply With Quote
  #5   (View Single Post)  
Old 31st March 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Well, there might be a better way Carpetsmoker but this comes to mind:

since you obviously have access to the winreg object within the function, and just need to know the key and other arguments: why not pass the key as a string and use eval() to access the corresponding attribute/method through it. Since it appears to be the data structure you want to use to access it by, lol.

example:

Code:
>>> class Klass:
...     name = "I am called Klass"
... 
>>> k=Klass()
>>> def test(regkey, key, value):
...   print( eval("k.%s" % str(regkey)) )
... 
>>> test('name', 'and', 'others')
I am called Klass
>>>
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.

Last edited by TerryP; 31st March 2009 at 10:27 PM.
Reply With Quote
  #6   (View Single Post)  
Old 31st March 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,080
Thanked 198 Times in 156 Posts
Default

Using eval() might be an option ... I'll check it out at work tomorrow.

Quote:
Originally Posted by roddierod
Ok, I see. Trying to write code to use across windows version now there is a headache.
Trying to write *any* code for Windows is a headache IMO ...

Quote:
Originally Posted by roddierod
Personally I try and stay away from the registry.
How else change system settings from a script?

Quote:
Originally Posted by roddierod
And wasn't Microsoft suppose to be moving away from the registry with Vista on. Or did that not happen?
Yes, it was planned, just like WinFS and a whole stack of stuff ... Until they scrapped all that and started pretty much over. The code got so complicated and bloated no one could make head or tails from it.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #7   (View Single Post)  
Old 31st March 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Quote:
Originally Posted by Carpetsmoker View Post
Trying to write *any* code for Windows is a headache IMO ...
In my experience generally yes, unless you can look at the Microsoft C API for Windows and smile.

The Qt C++ toolkit (and possibly WxWidgets) make things much less painful but all the fun stuff still requires the systems API or a library set to aggravate linking and distribution. Python is by far the least irksome language to do things with on Windows, that is also suitable for cross platform development. (Python == VERY good for writing portable code).
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #8   (View Single Post)  
Old 1st April 2009
roddierod's Avatar
roddierod roddierod is offline
Real Name: Rod Person
VPN Cryptographer
 
Join Date: Apr 2008
Location: Pittsburgh, Pa
Posts: 381
Thanked 13 Times in 13 Posts
Default

Quote:
Originally Posted by Carpetsmoker View Post
How else change system settings from a script?
It might not be the microsoft way, but I write to ini/config files. It's saved me a lot of headaches over the years. I try and use BSD type methods as much as I can while doing windows programming. I'm goof that way

@TerryP
I like the eval, I keep that in mind from now on.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick
Reply With Quote
  #9   (View Single Post)  
Old 1st April 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,080
Thanked 198 Times in 156 Posts
Default

Quote:
Originally Posted by roddierod View Post
It might not be the microsoft way, but I write to ini/config files. It's saved me a lot of headaches over the years. I try and use BSD type methods as much as I can while doing windows programming. I'm goof that way
No, that's not what I meant, I use flat files for storing the configuration of my program too, using the registry for this never even crossed my mind.

But I want to change system settings, such as IE Proxy, explorer settings, automatic updates, resolution, etc.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
Old 1st April 2009
roddierod's Avatar
roddierod roddierod is offline
Real Name: Rod Person
VPN Cryptographer
 
Join Date: Apr 2008
Location: Pittsburgh, Pa
Posts: 381
Thanked 13 Times in 13 Posts
Default

Oh, well guess your stuck with the registry then. Application I write for windows don't have anything to do with that sort of stuff, thankfully.
__________________
"The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words." -Philip K. Dick
Reply With Quote
Old 1st April 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,080
Thanked 198 Times in 156 Posts
Default

eval() works pretty good:

Code:
import _winreg as winreg

SetRegistry(winreg.HKEY_LOCAL_MACHINE, key, values)

def SetRegistry(reg, key, values):
  regobj = eval('winreg.' + reg)
  reghandle = winreg.ConnectRegistry(None, regobj)
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
Old 1st April 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

If the language doesn't have an eval-like method, it better be able to automatically compile and load libraries!!!


It's like one of the most useful features a language can have, whether or not you use it a lot.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Old 5th April 2009
mwatkins mwatkins is offline
Flying Circus Master
 
Join Date: Mar 2009
Location: Vancouver
Posts: 23
Thanked 1 Time in 1 Post
Default

The problem with "eval" is that it is not secure at all. If someone or something else gets to the registry, they can insert code into a key which your code may read and execute, code which may darn well be malicious.

For example the following file "foo" could just as easily be the C:\ntldr:

Code:
eval("open('foo','w').write('p0wned!')")
You might check (Python 2.6 or greater) literal_eval from the ast module:

Code:
literal_eval(node_or_string)
    Safely evaluate an expression node or a string containing a Python
    expression.  The string or node provided may only consist of the following
    Python literal structures: strings, numbers, tuples, lists, dicts, booleans,
    and None.
aka - I take that to mean no function calls or methods. Check it out further - I've not used this myself, but here's the above example:

Code:
>>> import ast
>>> ast.literal_eval("open('foo','w').write('p0wned!')")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.1/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/local/lib/python3.1/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string
If literal_eval doesn't meet your needs, google "python safe eval" for other approaches.

(And yes, I'm using an unreleased version of Python. Not advisable for general use. I'm working on moving all our code to Python 3.x, generally liking it.)

Last edited by mwatkins; 5th April 2009 at 05:39 PM. Reason: Added example
Reply With Quote
Old 5th April 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Hmm, haven't heard about ast.literal_eval() yet, thanks. I'm most used to the restricted eval (reval) in Safe.pm - and advise a .40 S&W to the cranium before doing an [r]?eval "" on user supplied code in any language.

eval is nicer for internal trickery.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
apache with python deadeyes FreeBSD Ports and Packages 1 27th August 2009 01:31 PM
CSH 'mch' variable? JMJ_coder General software and network 1 12th February 2009 11:24 PM
wip/kdebindings4-python errors Cloud NetBSD Package System (pkgsrc) 7 17th January 2009 06:52 PM
py-yaml - Python 2.5 and 2.6 iGloo FreeBSD Ports and Packages 0 20th December 2008 08:36 PM
Using Date variable? cwhitmore FreeBSD General 9 13th August 2008 07:16 AM


All times are GMT. The time now is 01:47 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick