2018-12-06 01:29:53 -05:00
|
|
|
from pathlib import Path
|
|
|
|
from lib.menu import ChecklistMenu
|
|
|
|
|
|
|
|
def toggle(obj, prop):
|
|
|
|
return lambda b: setattr(obj, prop, b)
|
|
|
|
|
|
|
|
class Package:
|
|
|
|
def __init__(self, obj):
|
2018-12-08 23:20:16 -05:00
|
|
|
# Defaults
|
|
|
|
self.source = 'core'
|
|
|
|
self.alias = None
|
|
|
|
|
|
|
|
# Immutable attributes
|
2018-12-06 01:29:53 -05:00
|
|
|
if isinstance(obj, dict):
|
|
|
|
self.name, props = next(iter(obj.items()))
|
|
|
|
self.source = 'core'
|
|
|
|
for key, value in props.items():
|
2018-12-08 23:20:16 -05:00
|
|
|
setattr(self, key, value if value != 'none' else None)
|
2018-12-06 01:29:53 -05:00
|
|
|
else:
|
|
|
|
self.name = obj
|
2018-12-08 23:20:16 -05:00
|
|
|
|
|
|
|
# Transform and infer attributes
|
2018-12-06 01:29:53 -05:00
|
|
|
self.update_from_files()
|
|
|
|
|
|
|
|
# Install State
|
|
|
|
self.enabled = True
|
|
|
|
self.install = True
|
2018-12-08 19:02:08 -05:00
|
|
|
self.export_config = True
|
2018-12-06 01:29:53 -05:00
|
|
|
self.run_script = True
|
2018-12-08 19:02:08 -05:00
|
|
|
self.export_units = True
|
2018-12-08 23:20:16 -05:00
|
|
|
self.enable_units = True
|
2018-12-06 01:29:53 -05:00
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-12-08 23:20:16 -05:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return getattr(self, key)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
return setattr(self, key, value)
|
|
|
|
|
|
|
|
def getattrs(self):
|
|
|
|
excepted = [
|
|
|
|
'name', 'enabled', 'install',
|
|
|
|
'export_config', 'run_script',
|
|
|
|
'export_units', 'enable_units'
|
|
|
|
]
|
2018-12-06 01:29:53 -05:00
|
|
|
|
2018-12-08 23:20:16 -05:00
|
|
|
return [(k, v) for k, v in vars(self).items() if v != None and k not in excepted]
|
|
|
|
|
|
|
|
def transform_default(self, prop, default, basepath):
|
|
|
|
default_path = Path(default)
|
|
|
|
if hasattr(self, prop):
|
|
|
|
if isinstance(self[prop], list):
|
|
|
|
self[prop] = list(map(lambda p: Path(p), self[prop]))
|
|
|
|
elif self[prop] == 'none':
|
|
|
|
self[prop] = None
|
|
|
|
elif self[prop] != None:
|
2018-12-08 23:46:13 -05:00
|
|
|
self[prop] = [Path(self[prop])]
|
2018-12-08 23:20:16 -05:00
|
|
|
elif (Path(basepath) / default_path).is_dir() or (Path(basepath) / default_path).is_file():
|
|
|
|
self[prop] = [default_path]
|
2018-12-06 01:29:53 -05:00
|
|
|
else:
|
2018-12-08 23:20:16 -05:00
|
|
|
self[prop] = None
|
|
|
|
|
|
|
|
def update_from_files(self):
|
|
|
|
self.transform_default('config', self.name, 'lain/.config')
|
|
|
|
self.transform_default('script', self.name + '.fish', 'scripts')
|
|
|
|
self.transform_default('userunit', self.name + '.service', 'lain/.config/systemd/user')
|
2018-12-06 01:29:53 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def config_menu(screen, title, packages):
|
|
|
|
items = []
|
|
|
|
for package in packages:
|
|
|
|
title = package.name + ' - Configure'
|
|
|
|
submenu = ChecklistMenu(screen, title, [
|
|
|
|
('Install', toggle(package, 'install')),
|
2018-12-08 19:02:08 -05:00
|
|
|
('Export Config', toggle(package, 'export_config')),
|
|
|
|
('Run Script', toggle(package, 'run_script')),
|
2018-12-08 23:20:16 -05:00
|
|
|
('Export Units', toggle(package, 'export_units')),
|
|
|
|
('Enable Units', toggle(package, 'enable_units'))
|
2018-12-06 01:29:53 -05:00
|
|
|
], True)
|
|
|
|
items += [(package.name, toggle(package, 'enabled'), submenu.display)]
|
|
|
|
return ChecklistMenu(screen, title, items, True)
|
|
|
|
|
|
|
|
class Category:
|
|
|
|
def __init__(self, name, packages):
|
|
|
|
self.name = name
|
|
|
|
self.packages = [Package(pkg) for pkg in packages]
|
|
|
|
self.enabled = True
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.name + ': ' + str(self.packages)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def config_menu(screen, categories):
|
|
|
|
items = []
|
|
|
|
for category in categories:
|
|
|
|
title = category.name + ' - Configure Packages'
|
|
|
|
submenu = Package.config_menu(screen, title, category.packages)
|
|
|
|
items += [(category.name, toggle(category, 'enabled'), submenu.display)]
|
|
|
|
return ChecklistMenu(screen, 'Configure Categories', items, True)
|
|
|
|
|