tar / make_spec_db.py.py
CC2311's picture
Upload 2 files
d6673d2 verified
raw
history blame
3.07 kB
# make_spec_db.py
import os, json, csv, argparse, sys
SPEC_DB_DIR = os.environ.get("SPEC_DB_DIR", ".")
DB_FILES = {
1: os.path.join(SPEC_DB_DIR, "spec_db_1.json"),
2: os.path.join(SPEC_DB_DIR, "spec_db_2.json"),
}
def load_db(profile:int)->dict:
path = DB_FILES[profile]
if os.path.exists(path):
with open(path,"r",encoding="utf-8") as f:
try:
return json.load(f)
except Exception:
return {}
return {}
def save_db(profile:int, data:dict):
path = DB_FILES[profile]
tmp = path + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
f.flush(); os.fsync(f.fileno())
os.replace(tmp, path)
print(f"[OK] Saved: {path}")
def set_entry(profile:int, typ:str, buff:str, M:float):
db = load_db(profile)
db.setdefault(typ, {})[buff] = float(M)
save_db(profile, db)
def import_csv(profile:int, csv_path:str):
db = load_db(profile)
with open(csv_path, "r", encoding="utf-8") as f:
rdr = csv.DictReader(f)
for row in rdr:
typ = str(row.get("type","")).strip()
buff = str(row.get("buff","")).strip()
M = float(row.get("M","0") or 0)
if not typ or not buff: continue
db.setdefault(typ, {})[buff] = M
save_db(profile, db)
def import_json(profile:int, json_path:str):
with open(json_path, "r", encoding="utf-8") as f:
payload = json.load(f)
if not isinstance(payload, dict):
print("Input JSON must be { type: { buff: M, ... }, ... }")
sys.exit(1)
db = load_db(profile)
for typ, inner in payload.items():
if not isinstance(inner, dict): continue
for buff, M in inner.items():
try:
db.setdefault(str(typ), {})[str(buff)] = float(M)
except Exception:
pass
save_db(profile, db)
def main():
ap = argparse.ArgumentParser(description="SPEC DB builder (profiles 1/2)")
ap.add_argument("--profile", type=int, choices=[1,2], required=True, help="DB ํ”„๋กœํŒŒ์ผ (1=7.0/PEND_OFF/GILD_OFF, 2=9.0/PEND_ON/GILD_OFF)")
sub = ap.add_subparsers(dest="cmd")
sp_set = sub.add_parser("set", help="๋‹จ์ผ ๊ฐ’ ์„ค์ •")
sp_set.add_argument("--type", required=True)
sp_set.add_argument("--buff", required=True)
sp_set.add_argument("--M", type=float, required=True)
sp_csv = sub.add_parser("import-csv", help="CSV ์ผ๊ด„ ์ž…๋ ฅ (cols: type,buff,M)")
sp_csv.add_argument("--path", required=True)
sp_json = sub.add_parser("import-json", help="JSON ์ผ๊ด„ ์ž…๋ ฅ {type:{buff:M}}")
sp_json.add_argument("--path", required=True)
args = ap.parse_args()
if args.cmd == "set":
set_entry(args.profile, args.type, args.buff, args.M)
elif args.cmd == "import-csv":
import_csv(args.profile, args.path)
elif args.cmd == "import-json":
import_json(args.profile, args.path)
else:
ap.print_help()
if __name__ == "__main__":
main()