1
0
mirror of https://github.com/lutris/lutris synced 2024-07-05 16:38:42 +00:00

Allow creation of WineRegistryKeys by definition or by path

This commit is contained in:
Mathieu Comandon 2016-10-26 20:45:28 -07:00
parent 02b7c817ac
commit a619602130
2 changed files with 36 additions and 9 deletions

View File

@ -166,21 +166,34 @@ class WineRegistry(object):
class WineRegistryKey(object):
def __init__(self, key_def=None):
self.raw_name = key_def[:key_def.index(']') + 1]
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)

View File

@ -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+')