Please write in Python 2.7. Write a program that takes a value from a user and s
ID: 3848135 • Letter: P
Question
Please write in Python 2.7. Write a program that takes a value from a user and stores it in the registry. You can use any key name that you like but also store the current time as another value inside of your new key. Finally, get a directory listing of your current working directory and store that value. You may need to use REG_MULTI_SZ for that value. I'm having alot of trouble and would really appreciate some assistance.
(A solution other than the ones posted on Chegg would be greatly appreciated as the existing solutions posted don't work... Please try running the script before posting a solution to make sure it really works..)
Explanation / Answer
import _winreg as winreg
class Registry(object):
def __init__(self, RegistryState=False):
self.m_backup = {}
self.m_res_state = RegistryState
def GetKey(self, hkey, skey, access, create_if_doesnt_exist=True):
CreatedKey = False
RegistryKey = None
try:
RegistryKey = winreg.OpenKey(hkey, skey, 0, access)
except WindowsError:
try:
if create_if_doesnt_exist:
RegistryKey = winreg.CreateKey(hkey, skey)
if RegistryKey not in self.m_backup:
self.m_backup[RegistryKey] = ({}, (hkey, skey))
else:
RegistryKey = None
except WindowsError:
if RegistryKey:
self.CloseKey(RegistryKey)
raise Exception('Registry does not exist and could not be created.')
return RegistryKey
def CloseKey(self, RegistryKey):
closed = False
if RegistryKey:
try:
winreg.CloseKey(RegistryKey)
closed = True
except:
closed = False
return closed
def GetRegistryValue(self, hkey, skey, name):
value = None
RegistryKey = self.GetKey(hkey, skey, winreg.KEY_READ, False)
if RegistryKey:
try:
value, _ = winreg.QueryValueEx(RegistryKey, name)
except WindowsError:
value = None
finally:
self.CloseKey(RegistryKey)
return value
def SetRegistryValue(self, hkey, skey, name, type, value):
RegistryKey = self.GetKey(hkey, skey, winreg.KEY_WRITE, True)
backed_up = False
was_set = False
if RegistryKey:
if self.m_restore_state:
if RegistryKey not in self.m_backup:
self.m_backup[RegistryKey] = ({}, None)
existing_value = self.GetRegistryValue(hkey, skey, name)
if existing_value:
self.m_backup[RegistryKey][0][name] = (existing_value, type, False)
else:
self.m_backup[RegistryKey][0][name] = (None, None, True)
backed_up = True
try:
winreg.SetValueEx(RegistryKey, name, 0, type, value)
was_set = True
except WindowsError:
was_set = False
finally:
if not backed_up:
self.CloseKey(RegistryKey)
return was_set
def RegistryState(self):
if self.m_restore_state:
for RegistryKey, data in self.m_backup.iteritems():
backup_dict, key_info = data
try:
for name, backup_data in backup_dict.iteritems():
value, type, was_created = backup_data
if was_created:
print RegistryKey, name
winreg.DeleteValue(RegistryKey, name)
else:
winreg.SetValueEx(RegistryKey, name, 0, type, value)
if key_info:
hkey, skey = key_info
winreg.DeleteKey(hkey, skey)
except:
raise Exception('Could not restore value')
self.CloseKey(RegistryKey)
def __del__(self):
if self.m_restore_state:
self.RegistryState()