From b7ad3ca04f5dec9bae4060477848c1dad1a49d86 Mon Sep 17 00:00:00 2001 From: Kamo Petrosyan Date: Thu, 20 Feb 2020 09:41:17 +0300 Subject: [PATCH] backend --- README.rst | 6 +++ pysitemap/backends/sqlite_todo.py | 83 +++++++++++++++++++++++++++++++ version.py | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 pysitemap/backends/sqlite_todo.py diff --git a/README.rst b/README.rst index 146f689..be2581b 100644 --- a/README.rst +++ b/README.rst @@ -54,6 +54,12 @@ TODO changelog --------- +v. 0.9.2 +'''''''' + +- todo queue and done list backends +- sqlite backend for todo queue and done lists + v. 0.9.1 '''''''' diff --git a/pysitemap/backends/sqlite_todo.py b/pysitemap/backends/sqlite_todo.py new file mode 100644 index 0000000..689c4e6 --- /dev/null +++ b/pysitemap/backends/sqlite_todo.py @@ -0,0 +1,83 @@ +import logging +import sqlite3 + + +class SQLiteTodo(object): + def __init__(self, db_name): + self.connection = sqlite3.connect(db_name) + self.__init_tables() + + def __init_tables(self): + cursor = self.connection.cursor() + cursor.execute("DROP TABLE IF EXISTS todo_queue;") + cursor.execute(""" + CREATE TABLE todo_queue ( + url text(1000) primary key + ); + """) + self.connection.commit() + cursor.close() + + def add(self, url): + cursor = self.connection.cursor() + try: + cursor.execute("""insert into todo_queue values (?);""", (url,)) + except Exception as e: + logging.info(e) + finally: + self.connection.commit() + cursor.close() + + def remove(self, url): + cursor = self.connection.cursor() + try: + cursor.execute("""delete from todo_queue where url = ?;""", (url,)) + except Exception as e: + logging.info(e) + finally: + self.connection.commit() + cursor.close() + + def __contains__(self, item): + cursor = self.connection.cursor() + result = False + try: + cursor.execute("""select 1 from todo_queue where url = ?""", (item, )) + row = cursor.fetchone() + if len(row): + result = True + except Exception as e: + logging.info(e) + finally: + cursor.close() + return result + + def __iter__(self): + cursor = self.connection.cursor() + result = [] + try: + cursor.execute("""select url from todo_queue""") + rows = cursor.fetchall() + result = [row[0] for row in rows] + except Exception as e: + logging.info(e) + finally: + cursor.close() + return iter(result) + + def __next__(self): + for url in self: + yield url + + def __len__(self): + cursor = self.connection.cursor() + result = [] + try: + cursor.execute("""select count(*) as cnt from todo_queue""") + row = cursor.fetchone() + result = row[0] + except Exception as e: + logging.info(e) + finally: + cursor.close() + return result \ No newline at end of file diff --git a/version.py b/version.py index 7e91176..4d11208 100644 --- a/version.py +++ b/version.py @@ -1 +1 @@ -VERSION = '0.9.1' \ No newline at end of file +VERSION = '0.9.2' \ No newline at end of file