aboutsummaryrefslogtreecommitdiff
path: root/ardsmm/__init__.py
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-05-20 01:39:46 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-05-20 01:39:46 -0400
commitcf7506e368924df66c221eafe03f719a40729afe (patch)
treeaa007d87a0ff5a48c1c8445ccad918784eafaa69 /ardsmm/__init__.py
downloadardsmm-cf7506e368924df66c221eafe03f719a40729afe.tar.gz
Initial commit
Diffstat (limited to 'ardsmm/__init__.py')
-rw-r--r--ardsmm/__init__.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/ardsmm/__init__.py b/ardsmm/__init__.py
new file mode 100644
index 0000000..cb0b90e
--- /dev/null
+++ b/ardsmm/__init__.py
@@ -0,0 +1,72 @@
+import json
+import sys
+from typing import Any
+from ardsmm._version import __version__, __version_tuple__
+
+
+class ArmaConfigMod:
+ data: dict[Any, Any]
+ MOD_SCHEMA = {
+ "modId": str,
+ "name": str,
+ "version": str,
+ }
+
+ def __init__(self, mod_dict):
+ if isinstance(mod_dict, str):
+ if mod_dict.endswith(","):
+ mod_dict = mod_dict[:-1]
+ self.data = json.loads(mod_dict)
+ else:
+ self.data = mod_dict
+ self.check()
+
+ def check(self):
+ for key, expected_type in self.MOD_SCHEMA.items():
+ if key not in self.data.keys():
+ raise KeyError(f"Mod key '{key}' is missing")
+ elif expected_type is not type(self.data[key]):
+ raise TypeError(f"Mod '{key}' value should be {expected_type}, but got {type(self.data[key])}")
+
+
+class ArmaConfig:
+ mods: list[Any]
+ data: dict[Any, Any]
+ file: str
+ DEFAULT_INDENT = 4
+
+ def __init__(self, configfile):
+ self.file = configfile
+ self.data = {}
+ self.mods = []
+ self.read()
+
+ def read(self):
+ with open(self.file, "r") as fp:
+ self.data = json.load(fp)
+
+ if not self.data.get("game"):
+ self.data["game"] = {}
+ if not self.data["game"].get("mods"):
+ self.data["game"]["mods"]: []
+
+ for mod in self.data["game"]["mods"]:
+ self.mods.append(ArmaConfigMod(mod))
+
+ def to_string(self, indent=DEFAULT_INDENT):
+ return json.dumps(self.data, indent=indent)
+
+ def append_mod(self, s):
+ mod = ArmaConfigMod(s)
+
+ if mod.data["name"] in [x.data["name"] for x in self.mods]:
+ print(f"[Skip ] {mod.data['name']} exists", file=sys.stderr)
+ return
+ print(f"[Append] {mod.data['name']}", file=sys.stderr)
+ self.mods.append(mod)
+
+ def update(self):
+ self.data["game"]["mods"] = sorted(
+ [x.data for x in self.mods],
+ key=lambda y: y["name"]
+ )