From 5ed847de8bf0e377a1e9c21c2407dbc115080409 Mon Sep 17 00:00:00 2001 From: nkrapp Date: Fri, 21 Oct 2022 15:16:34 +0200 Subject: [PATCH] added user table and import function --- lib/db.py | 12 ++++++++++++ lib/obs.py | 14 ++++++++++++++ lib/users.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 lib/users.py diff --git a/lib/db.py b/lib/db.py index 53d5d3a..80fa486 100644 --- a/lib/db.py +++ b/lib/db.py @@ -154,6 +154,18 @@ class DB: "create index request_number_idx2 on requests(number)", "UPDATE scheme SET version=12", ) + schemes[13] = ( + """ + CREATE TABLE users ( + id SERIAL PRIMARY KEY, + userid VARCHAR(255) NOT NULL, + login VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL, + realname VARCHAR(255) NOT NULL + ) + """, + "UPDATE scheme SET version=13", + ) schema_version = self.schema_version() if (schema_version + 1) not in schemes: diff --git a/lib/obs.py b/lib/obs.py index 77c7c7a..750ac87 100644 --- a/lib/obs.py +++ b/lib/obs.py @@ -8,6 +8,7 @@ from urllib.error import HTTPError import osc.core from lib.request import Request +from lib.users import Users # Add a retry wrapper for some of the HTTP actions. @@ -84,6 +85,14 @@ class OBS: return None return root + def _users(self, userid, **params): + try: + root = self._xml(f"/person/{userid}", **params) + except HTTPError: + logging.error(f"UserID {userid} not found") + return None + return root + def _link(self, project, package, rev): try: root = self._xml(f"source/{project}/{package}/_link", rev=rev) @@ -122,6 +131,11 @@ class OBS: if root is not None: return Request().parse(root) + def users(self, userid): + root = self._users(userid) + if root is not None: + return Users().parse(root, userid) + def files(self, project, package, revision): root = self._xml(f"source/{project}/{package}", rev=revision, expand=1) return [ diff --git a/lib/users.py b/lib/users.py new file mode 100644 index 0000000..3573fdb --- /dev/null +++ b/lib/users.py @@ -0,0 +1,38 @@ +class Users: + def parse(self, xml, userid): + self.userid = userid + self.login = xml.get("login") + self.realname = xml.find("login").get("realname") + self.email = xml.find("login").get("email") + + return self + + def __str__(self): + return f"User {self.userid} {self.login}: {self.realname} {self.email}" + + def __repr__(self): + return f"[{self.__str__()}]" + + def import_into_db(self, db): + cur = db.cursor() + cur.execute( + """INSERT INTO users (userid, login, realname, email) + VALUES (%s,%s,%s,%s) RETURNING id""", + ( + self.userid, + self.login, + self.realname, + self.email, + ), + ) + cur.close() + + def user_lookup(self, db, userid): + cur= db.cursor() + cur.execute("SELECT * FROM users where userid=%s", (userid,)) + row = cur.fetchone() + if not row: + cur.close() + return None + return row + \ No newline at end of file