persoconf/persoconf.py

78 lines
2.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from metafile import Metafile, MalformedMetafileError, NoPathDefinedError
class Persoconf(object):
"""The root of a persoconf"""
def __init__(self, path, metafile="META"):
self.path = path
self.metafile = metafile
self.check_validity()
# --------------------------------------
def check_validity(self):
"""Check if this Persoconf has a valid path"""
if not os.path.isdir(self.path):
if os.path.exists(self.path):
raise FileInsteadOfDirError("Persoconf root '%s' exists but " \
"is a file"
% self.path )
else:
raise PersoconfRootDoesNotExistError("Persoconf root '%s' " \
"does not exist"
% self.path )
return
# --------------------------------------
def list_apps(self, logger=None):
"""List all apps in the persoconf repo. Returns a dictionnary
{ appname -> Metafile ="""
apps_list = {}
for potential_app in os.listdir(self.path):
try:
# Try to initialize a metafile from the path.
appmeta = Metafile(json_path = self.path +"/"+
potential_app +"/"+
self.metafile)
apps_list[appmeta.name] = appmeta
except FileNotFoundError:
if logger is not None:
logger.info("No metafile %s in %s, probably not an app; " \
"ignoring"
% (self.metafile, potential_app) )
continue
except MalformedMetafileError:
if logger is not None:
logger.warning("Malformed metafile %s in app %s ; ignoring"
% (self.metafile, potential_app) )
continue
return apps_list
class FileInsteadOfDirError(Exception):
"""We expected a directory but got a file instead"""
def __init__(self, message):
Exception.__init__(self, message)
class PersoconfRootDoesNotExistError(Exception):
"""The Persoconf root directory was not created"""
def __init__(self, message):
Exception.__init__(self, message)