Last active
January 15, 2024 10:16
-
-
Save corro/9ea04ca7bb01975f289e7f5d72678432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import json | |
import yaml | |
import argparse | |
import collections | |
''' | |
Script to convert the output of the ILIAS status command (setup/cli.php status) | |
into a valid JSON install configuration (install.json) | |
Dependencies: | |
* Python 3 | |
* PyYAML (pip3 install pyyaml) | |
Usage example: | |
$ php setup/cli.php status | ilias-status-to-json | |
IMPORTANT: This script is not part of ILIAS and will not be maintained | |
''' | |
class RecursiveDict(collections.UserDict): | |
''' | |
Class for accessing and manipulating dicts using dot notation. | |
>>> r = RecursiveDict({'foo': {'bar': 'baz'}}) | |
>>> r.get_recursive('foo.bar') | |
'baz' | |
>>> r.set_recursive('foo.bar', 'xxx') | |
>>> r | |
{'foo': {'bar': 'xxx'}} | |
>>> r.del_recursive('foo.bar') | |
>>> r | |
{'foo': {}} | |
''' | |
def get_recursive(self, name): | |
def _get(dictionary, path): | |
key = path.pop(0) | |
if len(path) == 0: | |
return dictionary.get(key) | |
return _get(dictionary.get(key, {}), path) | |
return _get(self.data, name.split('.')) | |
def set_recursive(self, name, value): | |
def _set(dictionary, path, value): | |
key = path.pop(0) | |
if len(path) == 0: | |
dictionary[key] = value | |
return | |
_set(dictionary.get(key, {}), path, value) | |
_set(self.data, name.split('.'), value) | |
def del_recursive(self, name): | |
def _del(dictionary, path): | |
key = path.pop(0) | |
if len(path) == 0: | |
if key in dictionary: | |
del dictionary[key] | |
return | |
_del(dictionary.get(key, {}), path) | |
_del(self.data, name.split('.')) | |
class ILIASConfig(RecursiveDict): | |
''' | |
Class for manipulating ILIAS config dictionaries (based on RecursiveDict). | |
''' | |
def update_value(self, name, old_value, new_value): | |
''' | |
Update the value of an option. | |
>>> config = ILIASConfig({ | |
... 'common': {'client_id': 'foo'}, | |
... 'globalcache': {'service': 'ilStaticCache'} | |
... }) | |
>>> config.update_value('globalcache.service', 'ilStaticCache', | |
... 'static') | |
>>> config | |
{'common': {'client_id': 'foo'}, 'globalcache': {'service': 'static'}} | |
''' | |
value = self.get_recursive(name) | |
if value == old_value: | |
self.set_recursive(name, new_value) | |
def rename_option(self, old_name, new_name): | |
''' | |
Rename an option. | |
>>> config = ILIASConfig({ | |
... 'common': {'client_id': 'foo'}, | |
... 'database': {'name': 'foobar'} | |
... }) | |
>>> config.rename_option('database.name', 'database.database') | |
>>> config | |
{'common': {'client_id': 'foo'}, 'database': {'database': 'foobar'}} | |
>>> config = ILIASConfig({'common': {'client_id': 'foo'}}) | |
>>> config.rename_option('database.name', 'database.database') | |
>>> config | |
{'common': {'client_id': 'foo'}} | |
''' | |
value = self.get_recursive(old_name) | |
self.set_recursive(new_name, value) | |
self.del_recursive(old_name) | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), | |
default=sys.stdin, help='ilias status output (defaults to stdin)') | |
parser.add_argument('-t', dest='run_tests', action='store_true', | |
help='run doctests and exit') | |
args = parser.parse_args() | |
return args | |
def run_tests(): | |
import doctest | |
doctest.testmod(verbose=True) | |
def convert_status(status): | |
config = ILIASConfig(status.get('config', {})) | |
config.update_value('globalcache.service', 'ilStaticCache', 'static') | |
config.update_value('backgroundtasks.max_number_of_concurrent_tasks', 0, 1) | |
config.update_value('backgroundtasks.type', None, 'sync') | |
config.rename_option('http.http_path', 'http.path') | |
config.rename_option('database.name', 'database.database') | |
config.rename_option('database.pass', 'database.password') | |
return dict(config) | |
def main(): | |
args = parse_args() | |
if args.run_tests: | |
run_tests() | |
sys.exit(0) | |
status = yaml.safe_load(args.infile) | |
config = convert_status(status) | |
print(json.dumps(dict(config), indent=4)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment