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
config.set_field(self.__config_name__, {
'cookies': dict(self.client.http.client.cookies)
}, update_dict=True)
config.set_field(f"feed.{self.__config_name__}.cookies", dict(self.client.http.client.cookies), update_dict=True)
def post(self, message: str):

View File

@ -26,15 +26,13 @@ class Routine:
@staticmethod
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:
feed_instance = feed(**feed_config)
return feed(**feed_config)
except (TypeError, error.InvalidCredential) as e:
print(feed_config)
raise e
config.set_field(feed.__name__, feed.prompt_auth(), update_dict=True)
feed_config = config.get_field(feed.__config_name__, {})
config.set_field(f"feed.{feed.__config_name__}", {feed.prompt_auth()}, update_dict=True)
feed_config = config.get_field(f"feed.{feed.__config_name__}", {})
try:
return feed(**feed_config)

View File

@ -2,13 +2,15 @@ from . import Routine
from ..feeds import Feed, TwitterFeed
from ..utils import date
from datetime import datetime
from ics import Calendar
class Development(Routine):
def run(self):
calendar = self.get_ics()
def test_feeds(self):
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:
print(f"Event: {event.name}")
print(self.get_event_string(event))
def run(self):
self.test_feeds()

View File

@ -22,10 +22,11 @@ _config: dict = {
"twitter",
],
"mastodon": {},
"twitter": {},
"lemmy": {},
"feed": {
"mastodon": {},
"twitter": {},
"lemmy": {},
},
}
@ -50,16 +51,33 @@ def write():
def get_field(__name: str, default=None):
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):
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):
_config[__name].update(__value)
_field_container[_parts[-1]].update(__value)
else:
_config[__name] = __value
_field_container[_parts[-1]] = __value
write()