82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import json
|
|
|
|
class Metafile():
|
|
"""Representation of a metafile (default name META), containing (as its
|
|
name implies) metadata."""
|
|
|
|
|
|
def __init__(self, json_path=None, name=None):
|
|
|
|
self.name = name
|
|
|
|
# json_path given => we load it ; otherwise fill with default
|
|
if json_path is not None:
|
|
self.load_json(json_path) # raise FileNotFoundError
|
|
else:
|
|
self.load_default()
|
|
|
|
def load_default(self):
|
|
self.json_path = None
|
|
self.depends = {}
|
|
self.files = {}
|
|
# (Note that name is already written in __init__)
|
|
|
|
def load_json(self, json_path):
|
|
self.json_path = json_path
|
|
|
|
tmp = json.load( open(self.json_path) ) # Can raise a FileNotFoundError
|
|
|
|
try:
|
|
self.name = tmp["name"]
|
|
self.depends = tmp["depends"]
|
|
self.files = tmp["files"]
|
|
except KeyError:
|
|
raise MalformedMetafileError("Missing attribute (name, depends or files)")
|
|
|
|
def save(self):
|
|
if self.json_path is None:
|
|
raise NoPathDefinedError("json_path is not defined")
|
|
|
|
json.dump( { "name": self.name, "depends": self.depends, "files": self.files }, open(self.json_path, 'w') )
|
|
|
|
|
|
def get_files_dst2src(self):
|
|
dstdic = {}
|
|
|
|
try:
|
|
for (fname, finfo) in self.files.items():
|
|
dstdic[ finfo["dest"] ] = fname
|
|
except KeyError:
|
|
raise MalformedMetafileError("Missing 'dest' attribute in files infos")
|
|
|
|
return dstdic
|
|
|
|
def add_file(self, name, dest):
|
|
self.files[name] = { "dest": dest }
|
|
|
|
def del_file(self, name):
|
|
del self.files[name]
|
|
|
|
|
|
|
|
# Exceptions
|
|
########################################
|
|
class MalformedMetafileError(Exception):
|
|
"""A custom exception to alert that the metafile is malformed"""
|
|
|
|
def __init__(self, message):
|
|
Exception.__init__(self, message)
|
|
|
|
|
|
class NoPathDefinedError(Exception):
|
|
"""A custom exception to alert that the json_path properties of the
|
|
Metafile is not defined, therefore we cannot write the data."""
|
|
|
|
def __init__(self, message):
|
|
Exception.__init__(self, message)
|
|
|
|
|
|
|