fixed nested config

This commit is contained in:
Hellow 2024-02-15 01:08:45 +01:00
parent 0c4c41ff84
commit e85681a708
4 changed files with 37 additions and 21 deletions

View File

@ -52,9 +52,7 @@ class TwitterFeed(Feed):
) )
logged_in = True logged_in = True
config.set_field(self.__config_name__, { config.set_field(f"feed.{self.__config_name__}.cookies", dict(self.client.http.client.cookies), update_dict=True)
'cookies': dict(self.client.http.client.cookies)
}, update_dict=True)
def post(self, message: str): def post(self, message: str):

View File

@ -26,15 +26,13 @@ class Routine:
@staticmethod @staticmethod
def init_feed(feed: Type[Feed]) -> Feed: def init_feed(feed: Type[Feed]) -> Feed:
feed_config = config.get_field(feed.__config_name__, {}) feed_config = config.get_field(f"feed.{feed.__config_name__}", {})
try: try:
feed_instance = feed(**feed_config) return feed(**feed_config)
except (TypeError, error.InvalidCredential) as e: except (TypeError, error.InvalidCredential) as e:
print(feed_config) config.set_field(f"feed.{feed.__config_name__}", {feed.prompt_auth()}, update_dict=True)
raise e feed_config = config.get_field(f"feed.{feed.__config_name__}", {})
config.set_field(feed.__name__, feed.prompt_auth(), update_dict=True)
feed_config = config.get_field(feed.__config_name__, {})
try: try:
return feed(**feed_config) return feed(**feed_config)

View File

@ -2,13 +2,15 @@ from . import Routine
from ..feeds import Feed, TwitterFeed from ..feeds import Feed, TwitterFeed
from ..utils import date from ..utils import date
from datetime import datetime
from ics import Calendar from ics import Calendar
class Development(Routine): class Development(Routine):
def run(self): def test_feeds(self):
calendar = self.get_ics() for feed in self.iter_feeds():
feed_instance = self.init_feed(feed)
feed_instance.post(f"Hello World!!! ({datetime.now()})")
for event in calendar.events: def run(self):
print(f"Event: {event.name}") self.test_feeds()
print(self.get_event_string(event))

View File

@ -22,10 +22,11 @@ _config: dict = {
"twitter", "twitter",
], ],
"feed": {
"mastodon": {}, "mastodon": {},
"twitter": {}, "twitter": {},
"lemmy": {}, "lemmy": {},
},
} }
@ -50,16 +51,33 @@ def write():
def get_field(__name: str, default=None): def get_field(__name: str, default=None):
global _config global _config
return _config.get(__name, default)
_parts = __name.split(".")
_field_container = _config
for _part in _parts[:-1]:
_field_container = _field_container.get(_part, {})
return _field_container.get(_parts[-1], default)
def set_field(__name: str, __value, update_dict=False): def set_field(__name: str, __value, update_dict=False):
global _config global _config
_parts = __name.split(".")
_field_container = _config
for _part in _parts[:-1]:
if _part not in _field_container:
_field_container[_part] = {}
_field_container = _field_container[_part]
if update_dict and isinstance(__value, dict) and isinstance(_config.get(__name), dict): if update_dict and isinstance(__value, dict) and isinstance(_config.get(__name), dict):
_config[__name].update(__value) _field_container[_parts[-1]].update(__value)
else: else:
_config[__name] = __value _field_container[_parts[-1]] = __value
write() write()