Can't set attribute?!

2009-09-01 Thread dolgion ch
Hi there,

i'm new to Python, and i don't understand why following exception
occurs in this code:

class NETRPCGateway(RPCGateway):
"""NETRPC Implementation.
"""

def __init__(self, host, port):
self.host = host
self.port = port
super(NETRPCGateway, self).__init__(self.host, self.port,
'socket')

def listdb(self):
sock = tiny_socket.mysocket()
try:
sock.connect(self.host, self.port)
sock.mysend(('db', 'list'))
res = sock.myreceive()
sock.disconnect()
return res
except Exception, e:
return -1

def login(self, db, user, password):
sock = tiny_socket.mysocket()
self.db = db
self.password = password
self.user = user
try:
sock.connect(self.host, self.port)
sock.mysend(('common', 'login', db, user, password))
res = sock.myreceive()
sock.disconnect()
except Exception, e:
return -1

return res

def _execute(self, obj, method, args=(), noauth=False):
sock = tiny_socket.mysocket()
try:
sock.connect(self.host, self.port)
if not noauth:
print "in noauth"
args = (self.db, self.uid, self.password) + args
sock.mysend((obj, method) + args)
res = sock.myreceive()
sock.disconnect()
return res

except xmlrpclib.Fault, err:
raise RPCException(err.faultCode, err.faultString)

except tiny_socket.Myexception, err:
raise RPCException(err.faultCode, err.faultString)

def execute(self, obj, method, *args):
return self._execute(obj, method, args)

def execute_noauth(self, obj, method, *args):
return self._execute(obj, method, args, noauth=True)

def execute_db(self, method, *args):
sock = tiny_socket.mysocket()
sock.connect(host, port)
sock.mysend(('db', method) + args)
res = sock.myreceive()
sock.disconnect()
return res


class RPCSession(object):
"""This is a wrapper class that provides Pythonic way to handle
RPC (remote procedure call).
It also provides a way to store session data into different kind
of storage.
"""

__slots__ = ['host', 'port', 'protocol', 'storage', 'gateway']

def __init__(self, host, port, protocol='socket', storage={}):
"""Create new instance of RPCSession.

@param host: the openerp-server host
@params port: the openerp-server port
@params protocol: the openerp-server protocol
@param storage: a dict like storage that will be used to store
session data
"""
self.host = host
self.port = port
self.protocol = protocol
self.storage = storage
print "RPCSession ", host, port,protocol

if protocol == 'http':
self.gateway = XMLRPCGateway(host, port, 'http')

elif protocol == 'https':
self.gateway = XMLRPCGateway(host, port, 'https')

elif protocol == 'socket':
self.gateway = NETRPCGateway(host, port)

else:
raise common.message(_("Connection refused!"))

def __getattr__(self, name):
try:
return super(RPCSession, self).__getattribute__(name)
except:
pass

return self.storage.get(name)

def __setattr__(self, name, value):
if name in self.__slots__:
super(RPCSession, self).__setattr__(name, value)
else:
self.storage[name] = value

def __getitem__(self, name):
return self.storage.get(name)

def __setitem__(self, name, value):
self.storage[name] = value

def __delitem__(self, name):
try:
del self.storage[name]
except:
pass

def get(self, name, default=None):
return self.storage.get(name, default)

def get_url(self):
return self.gateway.get_url()

def listdb(self):
return self.gateway.listdb()

def login(self, db, user, password):

if password is None:
return -1

uid = self.gateway.login(db, user or '', password or '')

if uid <= 0:
return -1

self.uid = uid
self.db = db
self.password = password
self.open = True

#self.gateway.db = db
#self.gateway.uid = uid
#self.gateway.password = password

# read the full name of the user
self.user_name = self.execute('object', 'execute',
'res.users', 'read', [uid], ['name'])[0]['name']

# set the context
self.context_reload()

return uid

def logout(self):
try:
self.storage.clear()
except Exception, e:
pass

def is_logged(self):
return self.uid and self.open

def getmac(self):
s = socket.socket(socket.AF

Re: Can't set attribute?!

2009-09-01 Thread dolgion ch
alex23 u were right with ur suspicion about the RPCGateway, i hadn't
seen that there were attributes with the names db, uid, and password
already set. I resolved the issue by simply giving the attributes
different names :P

the problem with the slots wasn't going to occur since i was talking
about setting attributes in the NETRPCGateway class, not the
RPCSession class.

Thanks anyhow, and good to know about this __slots__ thing, i'll keep
it in mind

-- 
http://mail.python.org/mailman/listinfo/python-list