Skip to content

Commit

Permalink
Merge pull request #46 from t00m/0.0.28
Browse files Browse the repository at this point in the history
0.0.28
  • Loading branch information
t00m authored May 27, 2024
2 parents 8c707dd + c4ed4b8 commit d98ec87
Show file tree
Hide file tree
Showing 22 changed files with 221 additions and 208 deletions.
15 changes: 8 additions & 7 deletions MiAZ/backend/pluginsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class MiAZPluginType(IntEnum):
SYSTEM = 1
USER = 2

# ~ def __str__(self):
# ~ if self.value == MiAZPluginType.USER:
# ~ return _("User Plugin")
# ~ elif self.value == MiAZPluginType.SYSTEM:
# ~ return _("System Plugin")
def __str__(self):
if self.value == MiAZPluginType.USER:
return _("User Plugin")
elif self.value == MiAZPluginType.SYSTEM:
return _("System Plugin")


class MiAZPluginManager(GObject.GObject):
Expand Down Expand Up @@ -119,7 +119,7 @@ def load_plugin(self, plugin: Peas.PluginInfo) -> bool:
try:
self.engine.load_plugin(plugin)
if plugin.is_loaded():
# ~ self.log.debug("Plugin %s (%s) loaded", plugin.get_name(), ptype)
self.log.debug("Plugin %s (%s) loaded", plugin.get_name(), ptype)
return True
else:
self.log.error("Plugin %s (%s) couldn't be loaded", plugin.get_name(), ptype)
Expand All @@ -131,8 +131,9 @@ def load_plugin(self, plugin: Peas.PluginInfo) -> bool:

def unload_plugin(self, plugin: Peas.PluginInfo):
try:
ptype = self.get_plugin_type(plugin)
self.engine.unload_plugin(plugin)
self.log.debug("Plugin unloaded")
self.log.debug("Plugin %s (%s) unloaded", plugin.get_name(), ptype)
except Exception as error:
self.log.error(error)

Expand Down
7 changes: 4 additions & 3 deletions MiAZ/backend/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def __init__(self, backend):
conf = self.backend.get_config()
self.config = conf['Project']
self.util = self.backend.get_service('util')
repository = self.backend.get_service('repo')
repo_dir_conf = repository.get('dir_conf')
self.repository = self.backend.get_service('repo')
repo_dir_conf = self.repository.get('dir_conf')
self.cnfprj = os.path.join(repo_dir_conf, 'projects.json')
self.projects = {}
if not os.path.exists(self.cnfprj):
Expand All @@ -36,10 +36,11 @@ def __init__(self, backend):
self.check()

def check(self):
repo_dir = self.repository.get('dir_docs')
to_delete = []
for project in self.projects:
for doc in self.docs_in_project(project):
docpath = self.util.filename_path(doc)
docpath = os.path.join(repo_dir, doc)
if not os.path.exists(docpath):
to_delete.append((doc, project))
for doc, project in to_delete:
Expand Down
3 changes: 2 additions & 1 deletion MiAZ/backend/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ def load(self, path):
self.config['Project'] = MiAZConfigProjects(self.backend, repo_dir_conf)
self.config['Plugin'] = MiAZConfigUserPlugins(self.backend, repo_dir_conf)
self.backend.add_service('Projects', MiAZProject(self.backend))
watcher = self.backend.add_service('watcher', MiAZWatcher('source', path))
watcher = MiAZWatcher('source', path)
watcher.set_active(active=True)
self.backend.add_service('watcher', watcher)
self.log.debug("Config repo loaded from: %s", repo_dir_conf)
self.emit('repository-switched')

Expand Down
4 changes: 3 additions & 1 deletion MiAZ/backend/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, backend):
self.backend = backend
self.log = get_logger('MiAZStats')
self.util = self.backend.get_service('util')
self.repository = self.backend.get_service('repo')

def _build(self, *args):
self.stats = {}
Expand All @@ -52,7 +53,8 @@ def _build(self, *args):
self.stats[_(Purpose.__title__)] = {}
self.stats[_(SentTo.__title__)] = {}

for document in self.util.get_files():
repo_dir = self.repository.get('dir_docs')
for document in self.util.get_files(repo_dir):
fields = self.util.get_fields(document)

# Date
Expand Down
180 changes: 83 additions & 97 deletions MiAZ/backend/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, backend):
self.app = backend.app
self.backend = backend
self.conf = self.backend.get_config()
self.repository = self.backend.get_service('repo')
# ~ self.repository = self.backend.get_service('repo')

def directory_open(self, dirpath: str):
os.system("xdg-open '%s'" % dirpath)
Expand Down Expand Up @@ -95,9 +95,9 @@ def json_save(self, filepath: str, adict: {}) -> {}:
with open(filepath, 'w') as fout:
json.dump(adict, fout, sort_keys=True, indent=4)

def field_used(self, item_type, value):
def field_used(self, repo_dir, item_type, value):
used = False
for doc in self.get_files():
for doc in self.get_files(repo_dir):
fields = self.get_fields(doc)
fn = Field[item_type]
if fields[fn] == value:
Expand All @@ -108,10 +108,8 @@ def field_used(self, item_type, value):

def get_temp_dir(self):
ENV = self.app.get_env()
repo_dir = self.repository.get('dir_docs')
ts = datetime.now().strftime('%Y%m%d_%H%M%S')
name = self.valid_key(repo_dir)
return os.path.join(ENV['LPATH']['TMP'], "%s_%s" % (ts, name))
return os.path.join(ENV['LPATH']['TMP'], "%s_%s" % (ts, 'miaz-export'))

def get_temp_file(self, dir_tmp, suffix='.txt'):
return tempfile.mkstemp(dir=dir_tmp, suffix=suffix)
Expand All @@ -124,15 +122,10 @@ def get_fields(self, filename: str) -> []:
filename = filename[:dot]
return filename.split('-')

def get_files(self, root_dir: str = '') -> []:
"""Get all files from a given directory.
If no directory is given, it will return files from repository
documents directory.
"""
def get_files(self, dirpath: str) -> []:
"""Get all files from a given directory."""
# ~ FIXME: validate root_dir
if len(root_dir) == 0:
repo_dir = self.repository.get('dir_docs')
return glob.glob(os.path.join(repo_dir, '*'))
return glob.glob(os.path.join(dirpath, '*'))

def get_files_recursively(self, root_dir: str) -> []:
"""Get documents from a given directory recursively
Expand Down Expand Up @@ -162,9 +155,7 @@ def get_files_recursively(self, root_dir: str) -> []:
documents.add(thisfile)
return documents

def filename_get_creation_date(self, doc: str) -> datetime:
repo_dir = self.repository.get('dir_docs')
filepath = os.path.join(repo_dir, doc)
def filename_get_creation_date(self, filepath: str) -> datetime:
lastmod = os.stat(filepath).st_mtime
return datetime.fromtimestamp(lastmod)

Expand Down Expand Up @@ -208,11 +199,8 @@ def valid_key(self, key: str) -> str:
key = str(key).strip().replace(' ', '_')
return re.sub(r'(?u)[^-\w.]', '', key)

def filename_rename(self, doc_source, doc_target) -> bool:
def filename_rename(self, source, target) -> bool:
rename = False
repo_dir = self.repository.get('dir_docs')
source = os.path.join(repo_dir, doc_source)
target = os.path.join(repo_dir, doc_target)
if source != target:
if not os.path.exists(target):
try:
Expand All @@ -226,7 +214,7 @@ def filename_rename(self, doc_source, doc_target) -> bool:
self.log.error(error)
else:
self.log.debug("Document NOT renamed:")
self.log.error("\tTarget '%s' already exist", doc_target)
self.log.error("\tTarget '%s' already exist", target)
# ~ else:
# ~ self.log.error("Source and Target are the same. Skip rename")
return rename
Expand All @@ -245,13 +233,11 @@ def filename_import(self, source: str, target: str):
Normally, only the source filename would be necessary, but
as it is renamed according MiAZ rules, target is also needed.
"""
target = self.repository.get('dir_docs')
# ~ target = self.repository.get('dir_docs')
self.filename_copy(source, target)
self.emit('filename-added', target)

def filename_export(self, doc: str, target: str):
repo_dir = self.repository.get('dir_docs')
source = os.path.join(repo_dir, doc)
def filename_export(self, source: str, target: str):
self.filename_copy(source, target)

def filename_copy(self, source, target, overwrite=True):
Expand Down Expand Up @@ -284,88 +270,88 @@ def filename_date_human_simple(self, value: str = '') -> str:
date_dsc = None
return date_dsc

def filename_display(self, doc):
filepath = self.filename_path(doc)
def filename_display(self, filepath):
# ~ filepath = self.filename_path(doc)
if sys.platform in ['linux', 'linux2']:
os.system("xdg-open \"%s\"" % filepath)
elif sys.platform in ['win32', 'cygwin', 'msys']:
os.startfile(filepath)

def filename_path(self, doc):
repo_dir = self.repository.get('dir_docs')
return os.path.join(repo_dir, doc)
# ~ def filename_path(self, doc):
# ~ repo_dir = self.repository.get('dir_docs')
# ~ return os.path.join(repo_dir, doc)

def filename_validate(self, doc:str) -> bool:
if len(doc.split('-')) == 7:
return True
return False

def filename_validate_complex(self, filepath: str) -> tuple:
filename = os.path.basename(filepath)
reasons = "OK"
valid = True
reasons = []

# Check fields partitioning
partitioning = False
fields = filename.split('-')
if len(fields) != 7:
source = filename
target = self.filename_normalize(filename)
if source != target:
self.filename_rename(source, target)
else:
self.log.debug("Target normalized filename is the same than source")
name, ext = self.filename_details(filename)
fields = name.split('-')

# Check extension
item_type = Extension
gtype = Date.__gtype_name__
dot = filename.rfind('.')
if dot > 0:
name = filename[:dot]
ext = filename[dot+1:]
message = "File extension '%s' is valid" % ext
rc = True
else:
name = filename
ext = ''
rc = False
valid &= False
message = "File extension missing. Please, check this document!"
reasons.append((rc, gtype, ext, message))

# Validate fields
for item_type in [Date, Country, Group, SentBy, Purpose, SentTo]:
gtype = item_type.__gtype_name__
fn = Field[item_type] # Field number
fname = item_type.__gtype_name__
title = _(item_type.__title__)
key = fields[fn]
value = None
if len(key) == 0:
valid &= False
rc = False
message = _('<i>%s</i> field is empty') % title
else:
if item_type != Date:
available = self.conf[fname].exists_available(key)
used = self.conf[fname].exists_used(key)
if available and used:
rc = True
items = self.conf[fname].load_used()
value = self.conf[fname].get(key)
if len(value) > 0:
message = _('%s %s (%s) is available and ready to use') % (fname, key, value)
else:
message = _('%s %s is available and ready to use') % (fname, key)
else:
valid &= False
rc = False
message = _('%s %s available? %s. Used? %s') % (title, key, available, used)
reasons.append((rc, gtype, value, message))
return valid, reasons
# ~ def filename_validate_complex(self, filepath: str) -> tuple:
# ~ filename = os.path.basename(filepath)
# ~ reasons = "OK"
# ~ valid = True
# ~ reasons = []

# ~ # Check fields partitioning
# ~ partitioning = False
# ~ fields = filename.split('-')
# ~ if len(fields) != 7:
# ~ source = filename
# ~ target = self.filename_normalize(filename)
# ~ if source != target:
# ~ self.filename_rename(source, target)
# ~ else:
# ~ self.log.debug("Target normalized filename is the same than source")
# ~ name, ext = self.filename_details(filename)
# ~ fields = name.split('-')

# ~ # Check extension
# ~ item_type = Extension
# ~ gtype = Date.__gtype_name__
# ~ dot = filename.rfind('.')
# ~ if dot > 0:
# ~ name = filename[:dot]
# ~ ext = filename[dot+1:]
# ~ message = "File extension '%s' is valid" % ext
# ~ rc = True
# ~ else:
# ~ name = filename
# ~ ext = ''
# ~ rc = False
# ~ valid &= False
# ~ message = "File extension missing. Please, check this document!"
# ~ reasons.append((rc, gtype, ext, message))

# ~ # Validate fields
# ~ for item_type in [Date, Country, Group, SentBy, Purpose, SentTo]:
# ~ gtype = item_type.__gtype_name__
# ~ fn = Field[item_type] # Field number
# ~ fname = item_type.__gtype_name__
# ~ title = _(item_type.__title__)
# ~ key = fields[fn]
# ~ value = None
# ~ if len(key) == 0:
# ~ valid &= False
# ~ rc = False
# ~ message = _('<i>%s</i> field is empty') % title
# ~ else:
# ~ if item_type != Date:
# ~ available = self.conf[fname].exists_available(key)
# ~ used = self.conf[fname].exists_used(key)
# ~ if available and used:
# ~ rc = True
# ~ items = self.conf[fname].load_used()
# ~ value = self.conf[fname].get(key)
# ~ if len(value) > 0:
# ~ message = _('%s %s (%s) is available and ready to use') % (fname, key, value)
# ~ else:
# ~ message = _('%s %s is available and ready to use') % (fname, key)
# ~ else:
# ~ valid &= False
# ~ rc = False
# ~ message = _('%s %s available? %s. Used? %s') % (title, key, available, used)
# ~ reasons.append((rc, gtype, value, message))
# ~ return valid, reasons

def since_date_this_year(self, adate: datetime) -> datetime:
year = adate.year
Expand Down
Loading

0 comments on commit d98ec87

Please sign in to comment.