mirror of
https://github.com/lutris/lutris
synced 2024-10-04 14:59:37 +00:00
Allow creation of WineRegistryKeys by definition or by path
This commit is contained in:
parent
02b7c817ac
commit
a619602130
|
@ -166,21 +166,34 @@ class WineRegistry(object):
|
|||
|
||||
|
||||
class WineRegistryKey(object):
|
||||
def __init__(self, key_def=None):
|
||||
def __init__(self, key_def=None, path=None):
|
||||
|
||||
self.subkeys = OrderedDict()
|
||||
self.metas = OrderedDict()
|
||||
|
||||
if path:
|
||||
# Key is created by path, it's a new key
|
||||
timestamp = datetime.now().timestamp()
|
||||
self.name = path
|
||||
self.raw_name = "[{}]".format(path.replace('/', '\\\\'))
|
||||
self.raw_timestamp = ' '.join(str(timestamp).split('.'))
|
||||
key_def = "{} {}".format(self.raw_name, self.raw_timestamp)
|
||||
|
||||
windows_timestamp = WindowsFileTime.from_unix_timestamp(timestamp)
|
||||
self.metas["time"] = windows_timestamp.to_hex()
|
||||
else:
|
||||
# Existing key loaded from file
|
||||
self.raw_name = key_def[:key_def.index(']') + 1]
|
||||
self.raw_timestamp = key_def[key_def.index(']') + 2:]
|
||||
self.name = self.raw_name.replace('\\\\', '/').strip("[]")
|
||||
|
||||
# Parse timestamp either as int or float
|
||||
self.raw_timestamp = key_def[key_def.index(']') + 1:]
|
||||
ts_parts = self.raw_timestamp.strip().split()
|
||||
if len(ts_parts) == 1:
|
||||
self.timestamp = int(ts_parts[0])
|
||||
else:
|
||||
self.timestamp = float("{}.{}".format(ts_parts[0], ts_parts[1]))
|
||||
|
||||
self.subkeys = OrderedDict()
|
||||
self.metas = OrderedDict()
|
||||
self.name = self.raw_name.replace('\\\\', '/').strip("[]")
|
||||
|
||||
def __str__(self):
|
||||
return "{0} {1}".format(self.raw_name, self.raw_timestamp)
|
||||
|
||||
|
@ -204,7 +217,7 @@ class WineRegistryKey(object):
|
|||
|
||||
def render(self):
|
||||
"""Return the content of the key in the wine .reg format"""
|
||||
content = self.raw_name + self.raw_timestamp + "\n"
|
||||
content = self.raw_name + ' ' + self.raw_timestamp + "\n"
|
||||
for key, value in self.metas.items():
|
||||
if value is None:
|
||||
content += "#{}\n".format(key)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import os
|
||||
from unittest import TestCase
|
||||
from lutris.util.wineregistry import WineRegistry
|
||||
from lutris.util.wineregistry import WineRegistry, WineRegistryKey
|
||||
|
||||
FIXTURES_PATH = os.path.join(os.path.dirname(__file__), 'fixtures')
|
||||
|
||||
|
@ -62,3 +62,17 @@ class TestWineRegistry(TestCase):
|
|||
system_reg = WineRegistry(registry_path)
|
||||
content = system_reg.render()
|
||||
self.assertEqual(content, original_content)
|
||||
|
||||
|
||||
class TestWineRegistryKey(TestCase):
|
||||
def test_creation_by_key_def_parses(self):
|
||||
key = WineRegistryKey(key_def='[Control Panel\\\\Desktop] 1477412318')
|
||||
self.assertEqual(key.name, 'Control Panel/Desktop')
|
||||
self.assertEqual(key.raw_name, '[Control Panel\\\\Desktop]')
|
||||
self.assertEqual(key.raw_timestamp, '1477412318')
|
||||
|
||||
def test_creation_by_path_parses(self):
|
||||
key = WineRegistryKey(path='Control Panel/Desktop')
|
||||
self.assertEqual(key.name, 'Control Panel/Desktop')
|
||||
self.assertEqual(key.raw_name, '[Control Panel\\\\Desktop]')
|
||||
self.assertRegexpMatches(key.raw_timestamp, r'\d+\s\d+')
|
||||
|
|
Loading…
Reference in a new issue