From 02aee997556c14bdbf4ef3ba6582f0d822f18c55 Mon Sep 17 00:00:00 2001 From: Rickard Westerlund Date: Wed, 18 Feb 2015 22:04:24 +0100 Subject: [PATCH 1/3] Initial work on Python 3 compatibility. Not tested with Python 2.7. --- pysteam/_shortcut_generator.py | 3 ++- pysteam/_shortcut_parser.py | 10 +++++----- pysteam/shortcut.py | 11 +++++++++-- pysteam/steam.py | 15 ++++++++++----- pysteam/user.py | 7 ++++--- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/pysteam/_shortcut_generator.py b/pysteam/_shortcut_generator.py index 1c65b55..da9ff5a 100644 --- a/pysteam/_shortcut_generator.py +++ b/pysteam/_shortcut_generator.py @@ -21,7 +21,8 @@ def to_string(self,shortcuts): string = x00 + 'shortcuts' + x00 + self.generate_array_string(shortcuts) + x08 + x08 + x0a # rstrip is to remove the eol character that is automatically added. # According to vim the files I got from steam don't have the eol character - return unicode(string).rstrip() + try: return unicode(string).rstrip() + except NameError: return string.rstrip() def generate_array_string(self,shortcuts): string = "" diff --git a/pysteam/_shortcut_parser.py b/pysteam/_shortcut_parser.py index c13d4e1..3250fb5 100644 --- a/pysteam/_shortcut_parser.py +++ b/pysteam/_shortcut_parser.py @@ -11,7 +11,7 @@ import os import re -import shortcut +from pysteam import shortcut class ShortcutParser(object): @@ -22,7 +22,7 @@ def parse(self, path): return self.match_base(file_contents) def match_base(self,string): - match = re.match(ur"\u0000shortcuts\u0000(.*)\u0008\u0008$",string, re.IGNORECASE) + match = re.match(u"\u0000shortcuts\u0000(.*)\u0008\u0008$",string, re.IGNORECASE) if match: return self.match_array_string(match.groups()[0]) else: @@ -37,7 +37,7 @@ def match_array_string(self,string): # ignoring it for now shortcuts = [] while True: - match = re.match(ur"(.*)\u0000[0-9]+\u0000(\u0001AppName.*)\u0008",string, re.IGNORECASE) + match = re.match(u"(.*)\u0000[0-9]+\u0000(\u0001AppName.*)\u0008",string, re.IGNORECASE) if match: groups = match.groups() string = groups[0] @@ -51,7 +51,7 @@ def match_shortcut_string(self,string): # for the shortcut string (Appname, Exe, StartDir, etc), as oppposed # to matching for general Key-Value pairs. This could possibly create a # lot of work for me later, but for now it will get the job done - match = re.match(ur"\u0001AppName\u0000(.*)\u0000\u0001Exe\u0000(.*)\u0000\u0001StartDir\u0000(.*)\u0000\u0001icon\u0000(.*)\u0000\u0000tags\u0000(.*)\u0008",string, re.IGNORECASE) + match = re.match(u"\u0001AppName\u0000(.*)\u0000\u0001Exe\u0000(.*)\u0000\u0001StartDir\u0000(.*)\u0000\u0001icon\u0000(.*)\u0000\u0000tags\u0000(.*)\u0008",string, re.IGNORECASE) if match: # The 'groups' that are returned by the match should be the data # contained in the file. Now just make a Shortcut out of that data @@ -68,7 +68,7 @@ def match_shortcut_string(self,string): def match_tags_string(self,string): tags = [] while True: - match = re.match(ur"(.*)\u0001[0-9]+\u0000(.*?)\u0000",string) + match = re.match(u"(.*)\u0001[0-9]+\u0000(.*?)\u0000",string) if match: groups = match.groups() string = groups[0] diff --git a/pysteam/shortcut.py b/pysteam/shortcut.py index 730dfed..09683e3 100644 --- a/pysteam/shortcut.py +++ b/pysteam/shortcut.py @@ -10,8 +10,8 @@ import sys import os -import game -from _crc_algorithms import Crc +from pysteam import game +from pysteam._crc_algorithms import Crc class Shortcut(game.Game): @@ -24,6 +24,13 @@ def __init__(self, name, exe, startdir, icon="", tags=None): self.icon = icon self.tags = tags + def __eq__(self, other): + return (self.name == other.name and + self.exe == other.exe and + self.startdir == other.startdir and + self.icon == other.icon and + self.tags == other.tags) + def appid(self): algorithm = Crc(width = 32, poly = 0x04C11DB7, reflect_in = True, xor_in = 0xffffffff, reflect_out = True, xor_out = 0xffffffff) crc_input = ''.join([self.exe,self.name]) diff --git a/pysteam/steam.py b/pysteam/steam.py index 494b5fc..45ac99b 100644 --- a/pysteam/steam.py +++ b/pysteam/steam.py @@ -12,7 +12,7 @@ import sys import os -import user +from pysteam import user def _is_mac(): return sys.platform == 'darwin' @@ -26,7 +26,8 @@ def _is_linux(): def _windows_steam_location(): if not _is_windows(): return - import _winreg as registry + try: import _winreg as registry + except: import winreg as registry key = registry.CreateKey(registry.HKEY_CURRENT_USER,"Software\Valve\Steam") return registry.QueryValueEx(key,"SteamPath")[0] @@ -83,6 +84,10 @@ def local_users(self): # Any users on the machine will have an entry inside of the userdata # folder. As such, the easiest way to find a list of all users on the # machine is to just list the folders inside userdata - userdirs = filter(self._is_user_directory, os.listdir(self.userdata_location())) - # Exploits the fact that the directory is named the same as the user id - return map(lambda userdir: user.User(self, int(userdir)), userdirs) + users = [] + userdata_dir = self.userdata_location() + for entry in os.listdir(userdata_dir): + if os.path.isdir(os.path.join(userdata_dir,entry)): + u = user.User(self, int(entry)) + users.append(u) + return users diff --git a/pysteam/user.py b/pysteam/user.py index cabdb30..8807d97 100644 --- a/pysteam/user.py +++ b/pysteam/user.py @@ -7,11 +7,12 @@ Copyright (c) 2013 Scott Rice. All rights reserved. """ +from __future__ import print_function import sys import os -from _shortcut_parser import ShortcutParser -from _shortcut_generator import ShortcutGenerator +from pysteam._shortcut_parser import ShortcutParser +from pysteam._shortcut_generator import ShortcutGenerator # Information about SteamIDs and conversion between them found here: # https://developer.valvesoftware.com/wiki/SteamID @@ -66,7 +67,7 @@ def _load_shortcuts(self): parsed_shortcuts = [] if parsed_shortcuts == None: # TODO: Raise a decent error - print "Parsing error on file: %s" % file + print("Parsing error on file: %s" % file) parsed_shortcuts = [] return parsed_shortcuts From 1e2eaeda1eb29cbffdc3abe3eeb7d81a6a45cbc8 Mon Sep 17 00:00:00 2001 From: Rickard Westerlund Date: Wed, 25 Feb 2015 21:26:02 +0100 Subject: [PATCH 2/3] Restored _is_user_directory usage. Restored previously overwritten code. Verified tests work with Python 3. --- pysteam/steam.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pysteam/steam.py b/pysteam/steam.py index 45ac99b..56cbab0 100644 --- a/pysteam/steam.py +++ b/pysteam/steam.py @@ -84,10 +84,6 @@ def local_users(self): # Any users on the machine will have an entry inside of the userdata # folder. As such, the easiest way to find a list of all users on the # machine is to just list the folders inside userdata - users = [] - userdata_dir = self.userdata_location() - for entry in os.listdir(userdata_dir): - if os.path.isdir(os.path.join(userdata_dir,entry)): - u = user.User(self, int(entry)) - users.append(u) - return users + userdirs = filter(self._is_user_directory, os.listdir(self.userdata_location())) + # Exploits the fact that the directory is named the same as the user id + return list(map(lambda userdir: user.User(self, int(userdir)), userdirs)) From e65cc37972c49364f332338c67924be2b7a929f0 Mon Sep 17 00:00:00 2001 From: Rickard Westerlund Date: Sat, 25 Jul 2015 10:08:19 +0200 Subject: [PATCH 3/3] Python 2.7 compatibility fix for the CRC code. --- pysteam/_crc_algorithms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pysteam/_crc_algorithms.py b/pysteam/_crc_algorithms.py index 46280ea..1266e36 100644 --- a/pysteam/_crc_algorithms.py +++ b/pysteam/_crc_algorithms.py @@ -132,7 +132,7 @@ def bit_by_bit(self, in_data): value at the end. """ # If the input data is a string, convert to bytes. - if isinstance(in_data, str): + if isinstance(in_data, str) or isinstance(in_data, unicode): in_data = [ord(c) for c in in_data] register = self.NonDirectInit @@ -165,7 +165,7 @@ def bit_by_bit_fast(self, in_data): wich are appended to the input message in the bit-by-bit algorithm. """ # If the input data is a string, convert to bytes. - if isinstance(in_data, str): + if isinstance(in_data, str) or isinstance(in_data, unicode): in_data = [ord(c) for c in in_data] register = self.DirectInit @@ -219,7 +219,7 @@ def table_driven(self, in_data): The Standard table_driven CRC algorithm. """ # If the input data is a string, convert to bytes. - if isinstance(in_data, str): + if isinstance(in_data, str) or isinstance(in_data, unicode): in_data = [ord(c) for c in in_data] tbl = self.gen_table()