From e9ffd954354a6ce4dce80592f1a8cec898c2f038 Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Thu, 9 Mar 2023 12:17:10 -0500 Subject: [PATCH] implement dict.get() --- core/allnet.py | 32 ++++++++++++++------------------ core/mucha.py | 45 +++++++++++++++++++-------------------------- titles/diva/read.py | 2 +- 3 files changed, 34 insertions(+), 45 deletions(-) diff --git a/core/allnet.py b/core/allnet.py index 4c0c542..c11fe67 100644 --- a/core/allnet.py +++ b/core/allnet.py @@ -346,20 +346,16 @@ class AllnetPowerOnRequest: def __init__(self, req: Dict) -> None: if req is None: raise AllnetRequestException("Request processing failed") - self.game_id: str = req["game_id"] if "game_id" in req else "" - self.ver: str = req["ver"] if "ver" in req else "" - self.serial: str = req["serial"] if "serial" in req else "" - self.ip: str = req["ip"] if "ip" in req else "" - self.firm_ver: str = req["firm_ver"] if "firm_ver" in req else "" - self.boot_ver: str = req["boot_ver"] if "boot_ver" in req else "" - self.encode: str = req["encode"] if "encode" in req else "" - - try: - self.hops = int(req["hops"]) if "hops" in req else 0 - self.format_ver = int(req["format_ver"]) if "format_ver" in req else 2 - self.token = int(req["token"]) if "token" in req else 0 - except ValueError as e: - raise AllnetRequestException(f"Failed to parse int: {e}") + self.game_id: str = req.get("game_id", "") + self.ver: str = req.get("ver", "") + self.serial: str = req.get("serial", "") + self.ip: str = req.get("ip", "") + self.firm_ver: str = req.get("firm_ver", "") + self.boot_ver: str = req.get("boot_ver", "") + self.encode: str = req.get("encode", "") + self.hops = int(req.get("hops", "0")) + self.format_ver = int(req.get("hops", "2")) + self.token = int(req.get("hops", "0")) class AllnetPowerOnResponse3: @@ -413,10 +409,10 @@ class AllnetPowerOnResponse2: class AllnetDownloadOrderRequest: def __init__(self, req: Dict) -> None: - self.game_id = req["game_id"] if "game_id" in req else "" - self.ver = req["ver"] if "ver" in req else "" - self.serial = req["serial"] if "serial" in req else "" - self.encode = req["encode"] if "encode" in req else "" + self.game_id = req.get("game_id", "") + self.ver = req.get("ver", "") + self.serial = req.get("serial", "") + self.encode = req.get("encode", "") class AllnetDownloadOrderResponse: diff --git a/core/mucha.py b/core/mucha.py index 1b73b84..e058b12 100644 --- a/core/mucha.py +++ b/core/mucha.py @@ -132,24 +132,19 @@ class MuchaServlet: class MuchaAuthRequest: def __init__(self, request: Dict) -> None: - self.gameVer = ( - "" if "gameVer" not in request else request["gameVer"] - ) # gameCd + boardType + countryCd + version - self.sendDate = ( - "" if "sendDate" not in request else request["sendDate"] - ) # %Y%m%d - self.serialNum = "" if "serialNum" not in request else request["serialNum"] - self.gameCd = "" if "gameCd" not in request else request["gameCd"] - self.boardType = "" if "boardType" not in request else request["boardType"] - self.boardId = "" if "boardId" not in request else request["boardId"] - self.mac = "" if "mac" not in request else request["mac"] - self.placeId = "" if "placeId" not in request else request["placeId"] - self.storeRouterIp = ( - "" if "storeRouterIp" not in request else request["storeRouterIp"] - ) - self.countryCd = "" if "countryCd" not in request else request["countryCd"] - self.useToken = "" if "useToken" not in request else request["useToken"] - self.allToken = "" if "allToken" not in request else request["allToken"] + # gameCd + boardType + countryCd + version + self.gameVer = request.get("gameVer", "") + self.sendDate = request.get("sendDate", "") # %Y%m%d + self.serialNum = request.get("serialNum", "") + self.gameCd = request.get("gameCd", "") + self.boardType = request.get("boardType", "") + self.boardId = request.get("boardId", "") + self.mac = request.get("mac", "") + self.placeId = request.get("placeId", "") + self.storeRouterIp = request.get("storeRouterIp", "") + self.countryCd = request.get("countryCd", "") + self.useToken = request.get("useToken", "") + self.allToken = request.get("allToken", "") class MuchaAuthResponse: @@ -199,14 +194,12 @@ class MuchaAuthResponse: class MuchaUpdateRequest: def __init__(self, request: Dict) -> None: - self.gameVer = "" if "gameVer" not in request else request["gameVer"] - self.gameCd = "" if "gameCd" not in request else request["gameCd"] - self.serialNum = "" if "serialNum" not in request else request["serialNum"] - self.countryCd = "" if "countryCd" not in request else request["countryCd"] - self.placeId = "" if "placeId" not in request else request["placeId"] - self.storeRouterIp = ( - "" if "storeRouterIp" not in request else request["storeRouterIp"] - ) + self.gameVer = request.get("gameVer", "") + self.gameCd = request.get("gameCd", "") + self.serialNum = request.get("serialNum", "") + self.countryCd = request.get("countryCd", "") + self.placeId = request.get("placeId", "") + self.storeRouterIp = request.get("storeRouterIp", "") class MuchaUpdateResponse: diff --git a/titles/diva/read.py b/titles/diva/read.py index 9c409ef..2eeacdc 100644 --- a/titles/diva/read.py +++ b/titles/diva/read.py @@ -298,6 +298,6 @@ class DivaReader(BaseReader): tree[key] = ( value if len(vector) == 1 - else self.add_branch(tree[key] if key in tree else {}, vector[1:], value) + else self.add_branch(tree.get(key, {}), vector[1:], value) ) return tree