Created
April 15, 2020 09:59
-
-
Save rondreas/e1609bcab13393a2f4d982b9e2b2c1e2 to your computer and use it in GitHub Desktop.
Fire and forget script to parse and restore a kits default configuration.
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
# python | |
# -*- coding: utf-8 -*- | |
""" | |
Fire and forget script to parse and restore a kits default configuration. | |
>>> @restore_defaults.py myKit | |
""" | |
import os | |
import xml.etree.ElementTree as ET | |
import lx | |
__author__ = "AndreasR" | |
def extract_raw_values(config): | |
""" Scan a config file and extract all raw values as key value tuples. | |
:param config: path to .cfg | |
:returns: list of tuples key,value pairs | |
""" | |
result = [] | |
try: # Attempt to parse the config and read all raw values, | |
root = ET.parse(config).getroot() | |
raw_values = root.findall('*//hash[@type="RawValue"]') | |
result = [(e.attrib.get('key'), e.text) for e in raw_values] | |
except Exception as e: | |
print("Failed to parse config: {}".format(config)) | |
print(e) | |
return result | |
def get_defaults(kit): | |
""" Get default values defined in a kit. """ | |
file_svc = lx.service.File() | |
kit_root = file_svc.ToLocalAlias("kit_{}:".format(kit)) | |
# Store paths to all .cfg files for kit, | |
configs = list() | |
for root, dirs, files in os.walk(kit_root): | |
for file in files: | |
if file.lower().endswith(".cfg"): | |
configs.append(os.path.join(root, file)) | |
defaults = list() | |
for cfg in configs: | |
defaults += extract_raw_values(cfg) | |
return defaults | |
def main(): | |
""" """ | |
kit_name, = lx.args() | |
defaults = get_defaults(kit_name) | |
for username, value in defaults: | |
lx.eval("user.value name:{} value:{}".format(username, value)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment