def get(name, rc_file='~/.oerplibrc'): """Return the session configuration identified by `name` from the `rc_file` file. >>> import oerplib >>> oerplib.tools.session.get('foo') {'protocol': 'xmlrpc', 'user': '******', 'timeout': 120, 'database': 'db_name', 'passwd': 'admin', 'type': 'OERP', 'port': 8069, 'server': 'localhost'} :raise: :class:`oerplib.error.Error` """ conf = SafeConfigParser() conf.read([os.path.expanduser(rc_file)]) if not conf.has_section(name): raise error.Error( "'{0}' session does not exist".format(name)) return { 'type': conf.get(name, 'type'), 'server': conf.get(name, 'server'), 'protocol': conf.get(name, 'protocol'), 'port': conf.getint(name, 'port'), 'timeout': conf.getint(name, 'timeout'), 'user': conf.get(name, 'user'), 'passwd': conf.get(name, 'passwd'), 'database': conf.get(name, 'database'), }
def load(cls, name, rc_file='~/.oerplibrc'): """.. versionadded:: 0.8 Return a :class:`OERP` session pre-configured and connected with informations identified by `name`: >>> import oerplib >>> oerp = oerplib.OERP.load('foo') Such informations are stored with the :func:`OERP.save <oerplib.OERP.save>` method. """ data = session.get(name, rc_file) if data.get('type') != cls.__name__: raise error.Error( "'{0}' session is not of type '{1}'".format( name, cls.__name__)) oerp = cls( server=data['server'], protocol=data['protocol'], port=data['port'], timeout=data['timeout'], ) oerp.login( user=data['user'], passwd=data['passwd'], database=data['database']) return oerp
def login(self, user='******', passwd='admin', database=None): """Log in as the given `user` with the password `passwd` on the database `database` and return the corresponding user as a browsable record (from the ``res.users`` model). If `database` is not specified, the default one will be used instead. >>> user = oerp.login('admin', 'admin', database='db_name') >>> user.name u'Administrator' :return: the user connected as a browsable record :raise: :class:`oerplib.error.RPCError`, :class:`oerplib.error.Error` """ # Raise an error if no database was given self._database = database or self._database_default if not self._database: raise error.Error("No database specified") # Get the user's ID and generate the corresponding User record try: user_id = self.common.login(self._database, user, passwd) except rpc.error.ConnectorError as exc: raise error.RPCError(exc.message, exc.oerp_traceback) else: if user_id: self._uid = user_id self._password = passwd self._context = self.execute('res.users', 'context_get') self._user = self.browse('res.users', user_id, self._context) return self._user else: #FIXME: Raise an error? raise error.RPCError("Wrong login ID or password")
def remove(cls, name, rc_file='~/.oerplibrc'): """.. versionadded:: 0.8 Remove the session identified by `name` from the `rc_file` file: >>> import oerplib >>> oerplib.OERP.remove('foo') True """ data = session.get(name, rc_file) if data.get('type') != cls.__name__: raise error.Error( "'{0}' session is not of type '{1}'".format( name, cls.__name__)) return session.remove(name, rc_file)
def remove(name, rc_file='~/.oerplibrc'): """Remove the session configuration identified by `name` from the `rc_file` file. >>> import oerplib >>> oerplib.tools.session.remove('foo') :raise: :class:`oerplib.error.Error` """ conf = SafeConfigParser() conf.read([os.path.expanduser(rc_file)]) if not conf.has_section(name): raise error.Error( "'{0}' session does not exist".format(name)) conf.remove_section(name) with open(os.path.expanduser(rc_file), 'wb') as file_: conf.write(file_)
def _check_logged_user(self): """Check if a user is logged. Otherwise, an error is raised.""" if not self._uid or not self._password: raise error.Error(u"User login required.")