initial
This commit is contained in:
91
alacritty_config.py
Normal file
91
alacritty_config.py
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
ALACRITTY_CONFIG = os.path.expanduser("~/.config/alacritty/alacritty.yml")
|
||||
|
||||
|
||||
class Action:
|
||||
mutating = False
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
|
||||
def traverse(self, yml):
|
||||
for p in self.path:
|
||||
yml = yml[p]
|
||||
return yml
|
||||
|
||||
|
||||
class ShowAction(Action):
|
||||
def act_on(self, yml):
|
||||
yml = self.traverse(yml)
|
||||
if type(yml) is dict:
|
||||
print(yaml.dump(yml))
|
||||
else:
|
||||
print(yml)
|
||||
|
||||
|
||||
class EditAction(Action):
|
||||
mutating = True
|
||||
def __init__(self, path):
|
||||
self.path = path[:-1]
|
||||
self.prop = path[-1]
|
||||
|
||||
|
||||
class AddAction(EditAction):
|
||||
def __init__(self, path, val):
|
||||
super().__init__(path)
|
||||
self.val = yaml.safe_load(val)
|
||||
|
||||
def act_on(self, yml):
|
||||
yml = self.traverse(yml)
|
||||
yml[self.prop] = self.val
|
||||
|
||||
|
||||
class DeleteAction(EditAction):
|
||||
def act_on(self, yml):
|
||||
yml = self.traverse(yml)
|
||||
del yml[self.prop]
|
||||
|
||||
|
||||
def parse_path(p):
|
||||
if p.isdigit():
|
||||
return int(p)
|
||||
return p
|
||||
|
||||
|
||||
def parse_action():
|
||||
args = sys.argv
|
||||
|
||||
if len(args) == 1:
|
||||
return ShowAction([])
|
||||
|
||||
path = [parse_path(a) for a in args[1].split('.')]
|
||||
if len(args) == 2:
|
||||
return ShowAction(path)
|
||||
|
||||
if len(args) == 3 and args[2] == 'delete':
|
||||
return DeleteAction(path)
|
||||
|
||||
return AddAction(path, args[2])
|
||||
|
||||
|
||||
def main():
|
||||
action = parse_action()
|
||||
|
||||
try:
|
||||
with open(ALACRITTY_CONFIG) as f:
|
||||
y = yaml.safe_load(f.read())
|
||||
action.act_on(y)
|
||||
if action.mutating:
|
||||
with open(ALACRITTY_CONFIG, "w+") as f:
|
||||
f.write(yaml.dump(y))
|
||||
except KeyError as e:
|
||||
print('Key "{}" was not found in alacritty config!'.format(e))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user