From 5fe4e3b211e0f73b2245625dff608a6dcb9bfdc9 Mon Sep 17 00:00:00 2001 From: "Schimon Jehudah, Adv." Date: Sun, 15 Dec 2024 20:33:55 +0200 Subject: [PATCH] Fix database creation; Thank you. roughnecks. --- slixfeed/syndication.py | 4 ++-- slixfeed/utilities.py | 25 +++++++++-------------- slixfeed/version.py | 4 ++-- slixfeed/xmpp/chat.py | 8 ++++---- slixfeed/xmpp/client.py | 42 +++++++++++++++++++-------------------- slixfeed/xmpp/commands.py | 13 ++++++------ slixfeed/xmpp/ipc.py | 3 ++- slixfeed/xmpp/publish.py | 12 +++++------ slixfeed/xmpp/status.py | 2 +- 9 files changed, 54 insertions(+), 59 deletions(-) diff --git a/slixfeed/syndication.py b/slixfeed/syndication.py index e5e09ef..92c6e0f 100644 --- a/slixfeed/syndication.py +++ b/slixfeed/syndication.py @@ -52,7 +52,7 @@ class Feed: os.mkdir(dir_cache + '/' + ext) filename = os.path.join( dir_cache, ext, 'slixfeed_' + DateAndTime.timestamp() + '.' + ext) - db_file = os.path.join(dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(dir_data, jid_bare) results = sqlite.get_feeds(db_file) match ext: # case 'html': @@ -1273,7 +1273,7 @@ class FeedTask: # print('Scanning for updates for JID {}'.format(jid_bare)) logger.info('Scanning for updates for JID {}'.format(jid_bare)) while True: - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) urls = sqlite.get_active_feeds_url_sorted_by_last_scanned(db_file) for url in urls: #Message.printer('Scanning updates for URL {} ...'.format(url)) diff --git a/slixfeed/utilities.py b/slixfeed/utilities.py index 4de18e3..ea51db8 100644 --- a/slixfeed/utilities.py +++ b/slixfeed/utilities.py @@ -48,6 +48,7 @@ import os import random import slixfeed.fetch as fetch from slixfeed.log import Logger +import slixfeed.sqlite as sqlite import sys from urllib.parse import ( parse_qs, @@ -138,31 +139,23 @@ class Config: class Database: - def instantiate(jid_bare): + def instantiate(dir_data, jid_bare): """ - Callback function to instantiate action on database. + Instantiate action on database and return its filename location. Parameters ---------- + dir_data : str + Directory. jid_file : str - Filename. - callback : ? - Function name. - message : str, optional - Optional kwarg when a message is a part or - required argument. The default is None. + Jabber ID. Returns ------- - object - Coroutine object. + db_file + Filename. """ - db_dir = Config.get_default_data_directory() - if not os.path.isdir(db_dir): - os.mkdir(db_dir) - if not os.path.isdir(db_dir + "/sqlite"): - os.mkdir(db_dir + "/sqlite") - db_file = os.path.join(db_dir, "sqlite", r"{}.db".format(jid_bare)) + db_file = os.path.join(dir_data, 'sqlite', f'{jid_bare}.db') sqlite.create_tables(db_file) return db_file diff --git a/slixfeed/version.py b/slixfeed/version.py index 9f3504b..e73b3b7 100644 --- a/slixfeed/version.py +++ b/slixfeed/version.py @@ -1,2 +1,2 @@ -__version__ = '0.1.105' -__version_info__ = (0, 1, 105) +__version__ = '0.1.106' +__version_info__ = (0, 1, 106) diff --git a/slixfeed/xmpp/chat.py b/slixfeed/xmpp/chat.py index 5643791..a7cedd0 100644 --- a/slixfeed/xmpp/chat.py +++ b/slixfeed/xmpp/chat.py @@ -33,7 +33,7 @@ from slixfeed.fetch import Http from slixfeed.log import Logger import slixfeed.sqlite as sqlite from slixfeed.syndication import FeedTask -from slixfeed.utilities import Documentation, Html, MD, Task, Url +from slixfeed.utilities import Database, Documentation, Html, MD, Task, Url from slixfeed.xmpp.commands import XmppCommands from slixfeed.xmpp.message import XmppMessage from slixfeed.xmpp.presence import XmppPresence @@ -130,7 +130,7 @@ class XmppChat: return response = None - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) match command_lowercase: case 'help': @@ -635,7 +635,7 @@ class XmppChatAction: """ function_name = sys._getframe().f_code.co_name logger.debug(f'{function_name}: jid: {jid_bare} num: {num}') - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) encrypt_omemo = Config.get_setting_value(self, jid_bare, 'omemo') encrypted = True if encrypt_omemo else False jid = JID(jid_bare) @@ -875,7 +875,7 @@ class XmppChatTask: async def task_message(self, jid_bare): - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) while True: diff --git a/slixfeed/xmpp/client.py b/slixfeed/xmpp/client.py index 772b857..0825a91 100644 --- a/slixfeed/xmpp/client.py +++ b/slixfeed/xmpp/client.py @@ -51,7 +51,7 @@ import slixfeed.fetch as fetch from slixfeed.log import Logger import slixfeed.sqlite as sqlite from slixfeed.syndication import Feed, FeedDiscovery, FeedTask, Opml -from slixfeed.utilities import DateAndTime, Html, String, Task, Toml, Url, Utilities +from slixfeed.utilities import Database, DateAndTime, Html, String, Task, Toml, Url, Utilities from slixfeed.version import __version__ from slixfeed.xmpp.bookmark import XmppBookmark from slixfeed.xmpp.chat import XmppChat, XmppChatTask @@ -429,7 +429,7 @@ class XmppClient(slixmpp.ClientXMPP): for result in await XmppPubsub.get_pubsub_services(self): jid_bare = result['jid'] if jid_bare not in self.settings: - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) Config.add_settings_jid(self, jid_bare, db_file) #await XmppPubsubTask.task_publish(self, jid_bare) #await FeedTask.check_updates(self, jid_bare) @@ -491,7 +491,7 @@ class XmppClient(slixmpp.ClientXMPP): message_log = '{}: jid_full: {}' logger.debug(message_log.format(function_name, jid_full)) jid_bare = message['from'].bare - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) if jid_bare == self.boundjid.bare: @@ -1130,7 +1130,7 @@ class XmppClient(slixmpp.ClientXMPP): ftype='hidden', value=jid_bare) num = 100 - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) results = sqlite.get_entries(db_file, num) subtitle = 'Recent {} updates'.format(num) if results: @@ -1392,7 +1392,7 @@ class XmppClient(slixmpp.ClientXMPP): logger.debug('{}: jid_full: {}' .format(function_name, jid_full)) jid_bare = session['from'].bare - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) form = self['xep_0004'].make_form('form', 'Profile') @@ -1489,7 +1489,7 @@ class XmppClient(slixmpp.ClientXMPP): chat_type = await XmppUtilities.get_chat_type(self, jid_bare) if XmppUtilities.is_access(self, jid, chat_type): jid = session['from'].bare - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) form = self['xep_0004'].make_form('form', 'Filters') form['instructions'] = ('Filters allow you to skip news items ' 'that you may not be interested at. Use ' @@ -1564,7 +1564,7 @@ class XmppClient(slixmpp.ClientXMPP): jid_bare = session['from'].bare # form = self['xep_0004'].make_form('result', 'Done') # form['instructions'] = ('✅️ Filters have been updated') - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) # In this case (as is typical), the payload is a form values = payload['values'] for key in values: @@ -1717,7 +1717,7 @@ class XmppClient(slixmpp.ClientXMPP): form.add_field(var='jid', ftype='hidden', value=jid_bare) - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) num = 100 match values['action']: case 'all': @@ -1777,7 +1777,7 @@ class XmppClient(slixmpp.ClientXMPP): form.add_field(var='jid', ftype='hidden', value=jid) - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) title = sqlite.get_entry_title(db_file, ix) title = title[0] if title else 'Untitled' form['instructions'] = title @@ -1853,7 +1853,7 @@ class XmppClient(slixmpp.ClientXMPP): form.add_field(var='jid', ftype='hidden', value=jid_bare) - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if identifier and sqlite.check_identifier_exist(db_file, identifier): form['title'] = 'Conflict' form['instructions'] = ('Name "{}" already exists. Choose a ' @@ -2056,7 +2056,7 @@ class XmppClient(slixmpp.ClientXMPP): if XmppUtilities.is_operator(self, jid_bare) and 'jid' in values: jid_bare = values['jid'][0] del values['jid'] - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) for key in values: value = 1 if values[key] else 0 await sqlite.set_enabled_status(db_file, key, value) @@ -2081,8 +2081,8 @@ class XmppClient(slixmpp.ClientXMPP): if XmppUtilities.is_operator(self, jid_bare) and 'jid' in values: jid_bare = values['jid'][0] del values['jid'] - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') - subscriptions ='' + db_file = Database.instantiate(self.dir_data, jid_bare) + subscriptions = '' ixs = values['subscriptions'] for ix in ixs: name = sqlite.get_feed_title(db_file, ix) @@ -2314,7 +2314,7 @@ class XmppClient(slixmpp.ClientXMPP): form.add_field(ftype='hidden', value=jid_bare, var='jid') - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) match values['action']: case 'browse': form['instructions'] = 'Editing subscriptions' @@ -2408,7 +2408,7 @@ class XmppClient(slixmpp.ClientXMPP): form.add_field(ftype='hidden', value=jid_bare, var='jid') - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) tag_id = values['tag'] tag_name = sqlite.get_tag_name(db_file, tag_id)[0] form['instructions'] = 'Subscriptions tagged with "{}"'.format(tag_name) @@ -2446,7 +2446,7 @@ class XmppClient(slixmpp.ClientXMPP): form.add_field(ftype='hidden', value=jid_bare, var='jid') - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if 'subscription' in values: urls = values['subscription'] elif 'subscriptions' in values: urls = values['subscriptions'] url_count = len(urls) @@ -2539,7 +2539,7 @@ class XmppClient(slixmpp.ClientXMPP): values = payload['values'] if XmppUtilities.is_operator(self, jid_bare) and 'jid' in values: jid_bare = values['jid'][0] - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) # url = values['url'] # feed_id = sqlite.get_feed_id(db_file, url) # feed_id = feed_id[0] @@ -2940,7 +2940,7 @@ class XmppClient(slixmpp.ClientXMPP): if XmppUtilities.is_operator(self, jid_bare) and 'jid' in values: jid = values['jid'] jid_bare = jid[0] if isinstance(jid, list) else jid - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) result = await fetch.http(self.settings_network, url) count = await Opml.import_from_file(db_file, result) try: @@ -3465,7 +3465,7 @@ class XmppClient(slixmpp.ClientXMPP): if key: jid_bare = key value = values[key] - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) await Config.set_setting_value( @@ -3728,7 +3728,7 @@ class XmppClient(slixmpp.ClientXMPP): jid_bare = session['from'].bare chat_type = await XmppUtilities.get_chat_type(self, jid_bare) if XmppUtilities.is_access(self, jid, chat_type): - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) form = self['xep_0004'].make_form('form', 'Settings') @@ -3845,7 +3845,7 @@ class XmppClient(slixmpp.ClientXMPP): logger.debug('{}: jid_full: {}' .format(function_name, jid_full)) jid_bare = session['from'].bare - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) # In this case (as is typical), the payload is a form diff --git a/slixfeed/xmpp/commands.py b/slixfeed/xmpp/commands.py index 53133f9..78ac601 100644 --- a/slixfeed/xmpp/commands.py +++ b/slixfeed/xmpp/commands.py @@ -9,7 +9,7 @@ import slixfeed.fetch as fetch from slixfeed.log import Logger import slixfeed.sqlite as sqlite from slixfeed.syndication import Feed, FeedDiscovery, Opml -from slixfeed.utilities import DateAndTime, Documentation, String, Url, Utilities +from slixfeed.utilities import Database, DateAndTime, Documentation, String, Url, Utilities from slixfeed.version import __version__ from slixfeed.xmpp.bookmark import XmppBookmark from slixfeed.xmpp.muc import XmppMuc @@ -266,12 +266,12 @@ class XmppCommands: async def restore_default(self, jid_bare, key=None): if key: self.settings[jid_bare][key] = None - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) await sqlite.delete_setting(db_file, key) message = f'Setting {key} has been restored to default value.' else: del self.settings[jid_bare] - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) await sqlite.delete_settings(db_file) message = 'Default settings have been restored.' return message @@ -381,7 +381,7 @@ class XmppCommands: jid = info[0] if '/' not in jid: url = info[1] - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid}.db') + db_file = Database.instantiate(self.dir_data, jid) if len(info) > 2: identifier = info[2] else: @@ -470,7 +470,8 @@ class XmppCommands: async def fetch_http(self, url, db_file, jid_bare): if url.startswith('feed:/') or url.startswith('rss:/'): url = Url.feed_to_http(url) - url = (await Url.replace_hostname(self.dir_config, self.proxies, self.settings_network, url, 'feed')) or url + url = (await Url.replace_hostname( + self.dir_config, self.proxies, self.settings_network, url, 'feed') or url) counter = 0 while True: identifier = String.generate_identifier(url, counter) @@ -703,7 +704,7 @@ class XmppCommands: # response = ( # f'Every update will contain {response} news items.' # ) - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) await Config.set_setting_value( self, jid_bare, db_file, 'quantum', val_new) message = f'Next update will contain {val_new} news items (was: {val_old}).' diff --git a/slixfeed/xmpp/ipc.py b/slixfeed/xmpp/ipc.py index c48d747..c9324d1 100644 --- a/slixfeed/xmpp/ipc.py +++ b/slixfeed/xmpp/ipc.py @@ -12,6 +12,7 @@ socket (i.e. clients[fd]) from the respective client. import asyncio import os from slixfeed.syndication import FeedTask +from slixfeed.utilities import Database from slixfeed.xmpp.chat import XmppChatTask from slixfeed.xmpp.commands import XmppCommands from slixfeed.xmpp.chat import XmppChatAction @@ -84,7 +85,7 @@ class XmppIpcServer: if '~' in data: data_list = data.split('~') jid_bare = data_list[0] - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) command = data_list[1] else: command = data diff --git a/slixfeed/xmpp/publish.py b/slixfeed/xmpp/publish.py index cf4c66c..3cbc90e 100644 --- a/slixfeed/xmpp/publish.py +++ b/slixfeed/xmpp/publish.py @@ -16,7 +16,7 @@ from slixfeed.config import Config from slixfeed.log import Logger import slixfeed.sqlite as sqlite from slixfeed.syndication import Feed -from slixfeed.utilities import String, Url, Utilities +from slixfeed.utilities import Database, String, Url, Utilities from slixfeed.xmpp.iq import XmppIQ import sys @@ -259,7 +259,7 @@ class XmppPubsubAction: async def send_selected_entry(self, jid_bare, node_id, entry_id): function_name = sys._getframe().f_code.co_name logger.debug('{}: jid_bare: {}'.format(function_name, jid_bare)) - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) feed_id = sqlite.get_feed_id_by_entry_index(db_file, entry_id) feed_id = feed_id[0] node_id, node_title, node_subtitle = sqlite.get_feed_properties(db_file, feed_id) @@ -298,7 +298,7 @@ class XmppPubsubAction: """ function_name = sys._getframe().f_code.co_name logger.debug('{}: jid_bare: {}'.format(function_name, jid_bare)) - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) report = {} subscriptions = sqlite.get_active_feeds_url(db_file) for url in subscriptions: @@ -372,7 +372,7 @@ class XmppPubsubTask: async def loop_task(self, jid_bare): - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) while True: @@ -393,7 +393,7 @@ class XmppPubsubTask: def restart_task(self, jid_bare): - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) if jid_bare not in self.task_manager: @@ -411,7 +411,7 @@ class XmppPubsubTask: async def task_publish(self, jid_bare): - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) if jid_bare not in self.settings: Config.add_settings_jid(self, jid_bare, db_file) while True: diff --git a/slixfeed/xmpp/status.py b/slixfeed/xmpp/status.py index 41d04c5..62ed4f3 100644 --- a/slixfeed/xmpp/status.py +++ b/slixfeed/xmpp/status.py @@ -29,7 +29,7 @@ class XmppStatus: status_text = '📜️ Slixfeed RSS News Bot' enabled = Config.get_setting_value(self, jid_bare, 'enabled') if enabled: - db_file = os.path.join(self.dir_data, 'sqlite', f'{jid_bare}.db') + db_file = Database.instantiate(self.dir_data, jid_bare) jid_task = self.pending_tasks[jid_bare] if jid_bare in self.pending_tasks else None if jid_task and len(jid_task): # print('status dnd for ' + jid_bare)