Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.0.19 #35

Merged
merged 21 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 3 additions & 2 deletions MiAZ/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ class MiAZBackend(GObject.GObject):
__gtype_name__ = 'MiAZBackend'
conf = {}

def __init__(self) -> None:
def __init__(self, app) -> None:
GObject.GObject.__init__(self)
self.app = app
self.log = get_logger('MiAZBackend')
GObject.signal_new('repository-updated',
MiAZBackend,
Expand Down Expand Up @@ -113,4 +114,4 @@ def repo_load(self, path):

def repo_check(self, *args):
self.log.debug("Source repository updated")
self.emit('repository-updated')
self.emit('repository-updated')
62 changes: 50 additions & 12 deletions MiAZ/backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

from gi.repository import GObject

from MiAZ.backend.env import ENV
from MiAZ.backend.log import get_logger
from MiAZ.backend.models import MiAZModel, MiAZItem, File, Group, Person, Country, Purpose, Concept, SentBy, SentTo, Project, Repository

class MiAZConfig(GObject.GObject):
""" MiAZ Config class"""
used = None
default = None
cache = {}

def __init__(self, backend, log, config_for, used=None, available=None, default=None, model=MiAZModel, must_copy=True, foreign=False):
super().__init__()
Expand Down Expand Up @@ -78,12 +78,24 @@ def get_config_foreign(self):
return self.foreign

def load(self, filepath:str) -> dict:
# ~ self.log.debug("Loading from %s", filepath)
try:
items = self.util.json_load(filepath)
except Exception as error:
items = None
return items
config_changed = self.cache[filepath]['changed']
except:
config_changed = True

if config_changed:
# ~ self.log.debug("Loading from %s", filepath)
try:
items = self.util.json_load(filepath)
self.cache[filepath] = {}
self.cache[filepath]['changed'] = False
self.cache[filepath]['items'] = items
# ~ self.log.debug("In-memory config data updated for '%s'", filepath)
except Exception as error:
items = None
return items
else:
return self.cache[filepath]['items']

def load_available(self) -> dict:
return self.load(self.available)
Expand All @@ -98,6 +110,12 @@ def save(self, filepath: str = '', items: dict = {}) -> bool:
self.emit('available-updated')
elif filepath == self.used:
self.emit('used-updated')
try:
self.cache[filepath]['changed'] = True
except:
self.cache[filepath] = {}
self.cache[filepath]['changed'] = True
self.log.debug("Cache update for '%s'", filepath)
return saved

def save_available(self, items: dict = {}) -> bool:
Expand Down Expand Up @@ -201,7 +219,9 @@ def remove(self, filepath: str, key: str):
class MiAZConfigApp(MiAZConfig):
def __init__(self, backend):
self.backend = backend
self.app = backend.app
self.util = self.backend.util
ENV = self.app.get_env()
GObject.GObject.__init__(self)
GObject.signal_new('repo-settings-updated-app',
MiAZConfigApp,
Expand Down Expand Up @@ -230,6 +250,8 @@ def save(self, filepath: str = '', items: dict = {}) -> bool:

class MiAZConfigRepositories(MiAZConfig):
def __init__(self, backend):
app = backend.app
ENV = app.get_env()
dir_conf = ENV['LPATH']['ETC']
super().__init__(
backend = backend,
Expand All @@ -245,13 +267,15 @@ def __init__(self, backend):

class MiAZConfigCountries(MiAZConfig):
def __init__(self, backend, dir_conf):
app = backend.app
ENV = app.get_env()
super().__init__(
backend = backend,
log=get_logger('MiAZ.Settings.Countries'),
config_for = 'Countries',
available = os.path.join(dir_conf, 'countries-available.json'),
used = os.path.join(dir_conf, 'countries-used.json'),
default = os.path.join(ENV['GPATH']['RESOURCES'],
default = os.path.join(ENV['GPATH']['CONF'],
'MiAZ-countries.json'),
model = Country,
must_copy = False,
Expand All @@ -260,34 +284,40 @@ def __init__(self, backend, dir_conf):

class MiAZConfigGroups(MiAZConfig):
def __init__(self, backend, dir_conf):
app = backend.app
ENV = app.get_env()
super().__init__(
backend = backend,
log=get_logger('MiAZ.Settings.Groups'),
config_for = 'Groups',
used = os.path.join(dir_conf, 'groups-used.json'),
available = os.path.join(dir_conf, 'groups-available.json'),
default = os.path.join(ENV['GPATH']['RESOURCES'],
default = os.path.join(ENV['GPATH']['CONF'],
'MiAZ-groups.json'),
model = Group,
must_copy = True
)

class MiAZConfigPurposes(MiAZConfig):
def __init__(self, backend, dir_conf):
app = backend.app
ENV = app.get_env()
super().__init__(
backend = backend,
log=get_logger('MiAZ.Settings.Purposes'),
config_for = 'Purposes',
used = os.path.join(dir_conf, 'purposes-used.json'),
available = os.path.join(dir_conf, 'purposes-available.json'),
default = os.path.join(ENV['GPATH']['RESOURCES'],
default = os.path.join(ENV['GPATH']['CONF'],
'MiAZ-purposes.json'),
model = Purpose,
must_copy = True
)

class MiAZConfigConcepts(MiAZConfig):
def __init__(self, backend, dir_conf):
app = backend.app
ENV = app.get_env()
super().__init__(
backend = backend,
log=get_logger('MiAZ.Settings.Concepts'),
Expand All @@ -301,48 +331,56 @@ def __init__(self, backend, dir_conf):

class MiAZConfigPeople(MiAZConfig):
def __init__(self, backend, dir_conf):
app = backend.app
ENV = app.get_env()
super().__init__(
backend = backend,
log=get_logger('MiAZ.Settings.People'),
config_for = 'Person',
used = os.path.join(dir_conf, 'people-used.json'),
available = os.path.join(dir_conf, 'people-available.json'),
default = os.path.join(ENV['GPATH']['RESOURCES'],
default = os.path.join(ENV['GPATH']['CONF'],
'MiAZ-people.json'),
model = Person,
must_copy = True
)

class MiAZConfigSentBy(MiAZConfig):
def __init__(self, backend, dir_conf):
app = backend.app
ENV = app.get_env()
super().__init__(
backend = backend,
log=get_logger('MiAZ.Settings.SentBy'),
config_for = 'SentBy',
used = os.path.join(dir_conf, 'sentby-used.json'),
available = os.path.join(dir_conf, 'people-available.json'),
default = os.path.join(ENV['GPATH']['RESOURCES'],
default = os.path.join(ENV['GPATH']['CONF'],
'MiAZ-people.json'),
model = SentBy,
must_copy = False
)

class MiAZConfigSentTo(MiAZConfig):
def __init__(self, backend, dir_conf):
app = backend.app
ENV = app.get_env()
super().__init__(
backend = backend,
log=get_logger('MiAZ.Settings.SentTo'),
config_for = 'SentTo',
used = os.path.join(dir_conf, 'sentto-used.json'),
available = os.path.join(dir_conf, 'people-available.json'),
default = os.path.join(ENV['GPATH']['RESOURCES'],
default = os.path.join(ENV['GPATH']['CONF'],
'MiAZ-people.json'),
model = SentTo,
must_copy = False
)

class MiAZConfigProjects(MiAZConfig):
def __init__(self, backend, dir_conf):
app = backend.app
ENV = app.get_env()
super().__init__(
backend = backend,
log=get_logger('MiAZ.Settings.Project'),
Expand Down
99 changes: 0 additions & 99 deletions MiAZ/backend/env.py

This file was deleted.

15 changes: 7 additions & 8 deletions MiAZ/backend/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

import queue
import logging

from MiAZ.backend.env import ENV
from rich.logging import RichHandler

levels = {
10: 'DEBUG',
Expand All @@ -25,14 +24,14 @@ def get_logger(name):
"""Returns a new logger with personalized.
@param name: logger name
"""
logging.basicConfig(level=logging.INFO, format="%(levelname)7s | %(lineno)4d |%(name)-25s | %(asctime)s | %(message)s")
logging.basicConfig(level='NOTSET', format="%(message)s", handlers=[RichHandler()])
log = logging.getLogger(name)
log.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(levelname)7s | %(lineno)4d |%(name)-25s | %(asctime)s | %(message)s")
fh = logging.FileHandler(ENV['FILE']['LOG'])
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
log.addHandler(fh)
# ~ formatter = logging.Formatter("%(levelname)7s | %(lineno)4d |%(name)-25s | %(asctime)s | %(message)s")
# ~ fh = logging.FileHandler(ENV['FILE']['LOG'])
# ~ fh.setFormatter(formatter)
# ~ fh.setLevel(logging.DEBUG)
# ~ log.addHandler(fh)

return log
Loading
Loading