From 59d95ec3e9bff266f2783b19645a5829551dd5ca Mon Sep 17 00:00:00 2001 From: arkhan Date: Sun, 8 Jan 2023 00:38:05 -0500 Subject: [PATCH] [FIX] Use resp.get() to prevent KeyError #9 --- odooly.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/odooly.py b/odooly.py index 582c8cf..3d97b7c 100644 --- a/odooly.py +++ b/odooly.py @@ -4,6 +4,7 @@ Author: Florent Xicluna """ +import _ast import atexit import csv import functools @@ -16,19 +17,19 @@ import time import traceback -import _ast PY2 = sys.version_info[0] == 2 if not PY2: # Python 3 from configparser import ConfigParser from threading import current_thread from urllib.request import Request, urlopen - from xmlrpc.client import Fault, ServerProxy, MININT, MAXINT + from xmlrpc.client import MAXINT, MININT, Fault, ServerProxy else: # Python 2 - from ConfigParser import SafeConfigParser as ConfigParser from threading import currentThread as current_thread + + from ConfigParser import SafeConfigParser as ConfigParser from urllib2 import Request, urlopen - from xmlrpclib import Fault, ServerProxy, MININT, MAXINT + from xmlrpclib import MAXINT, MININT, Fault, ServerProxy try: import requests @@ -166,7 +167,6 @@ def _dict_to_list(self, rowdict): for cell in rowlst ] - else: # Python 3 basestring = str int_types = int @@ -441,7 +441,6 @@ def http_post(url, data, headers={"Content-Type": "application/json"}): resp = requests.post(url, data=data, headers=headers) return resp.json() - else: def http_post(url, data, headers={"Content-Type": "application/json"}): @@ -455,12 +454,12 @@ def dispatch_jsonrpc(url, service_name, method, args): "jsonrpc": "2.0", "method": "call", "params": {"service": service_name, "method": method, "args": args}, - "id": "%04x%010x" % (os.getpid(), (int(time.time() * 1e6) % 2 ** 40)), + "id": "%04x%010x" % (os.getpid(), (int(time.time() * 1e6) % 2**40)), } resp = http_post(url, json.dumps(data).encode("ascii")) if resp.get("error"): raise ServerError(resp["error"]) - return resp["result"] + return resp.get("result") class partial(functools.partial): @@ -544,13 +543,13 @@ def wrapper(self, *args): class Env(object): """An environment wraps data for Odoo models and records: - - :attr:`db_name`, the current database; - - :attr:`uid`, the current user id; - - :attr:`context`, the current context dictionary. + - :attr:`db_name`, the current database; + - :attr:`uid`, the current user id; + - :attr:`context`, the current context dictionary. - To retrieve an instance of ``some.model``: + To retrieve an instance of ``some.model``: - >>> env["some.model"] + >>> env["some.model"] """ name = uid = user = None @@ -1953,7 +1952,12 @@ def _set_external_id(self, xml_id): if self.env["ir.model.data"].search(domain): raise ValueError("ID %r collides with another entry" % xml_id) self.env["ir.model.data"].create( - {"model": self._name, "res_id": self.id, "module": mod, "name": name,} + { + "model": self._name, + "res_id": self.id, + "module": mod, + "name": name, + } ) def __getattr__(self, attr):