Files
Exiled-Exchange-2/dataParser/vendor/client/descriptionParser/descriptionFile.py
T
Kvan7 835e43a166 v0.1.4 (#166)
* Fix #132

* Features/python script update (#143)

This closes the following issues:

- #67 
- #68 
- #69 
- #70 
- #110 
- #111 
- #112 
- #139

* [Parse Error] - Dualstring bow #60

* Adds starting for localization

* tests yay, this is terrible Add junit tests :( #74

* revert tests cause they were really terrible

* bump gitignore so i dont lose files

* Features/pseudo-pseudo (#93)

* uhhhhhhhhhhhh, maybe pseudo will get support before GGG

* psesudo only ele

* remove git ignored files

* fix ignore

* working tree

* update parser #67

* bump

* more stuff

* maybe working new desc parsing

* Description parser

* Maybe?

* update

* add ko and cmn-Hant

* Add items and pseudo

* push to main

* force utf-16

* moving data

* uh, miss typed a utf 8 for a utf 16

* revert cause that was worse

* Features/moreLocalizationFixes (#148)

* change data files

* ru client strings

* Revert to search by translated name

REVERT THIS LATER ONCE COPY ADVANCED DESC IS BACK

* ru ko and cmn-Hant parse correctly now

* Change Exalted orb to Perfect Jewelers orb

May change again, but just found out about sidekick and since their program was made before creating this, I'm not going to add conflict of logos.

* change from jewelers to div, didn't like how it looked in ui

* Update README

* Update README.md

* Update README.md

* Revert "change from jewelers to div, didn't like how it looked in ui"

This reverts commit b11d33353d.

* Update README.md

* Update localization for PoE2 #66

Functional. Missing some base types in some languages but will make separate issue

* move .ds_store to ds_store (just unhide)

* Builtin browser opens while disabled in settings #103

* [Not Recognized Modifier] - +2 Charm Slots #137

* Remove calls to poeprices for now

* Version Bump to v0.1.4

* Move store and rename

* file move error

* lint + update app_i18n

* [PoE2] Item gets pinned in-game with CTRL+D (auto-hide on) #124

* add fix to pull-json

* final lint fix
2024-12-24 12:45:31 -06:00

104 lines
3.7 KiB
Python

import json
import logging
import os
from descriptionParser.description import Description
logger = logging.getLogger(__name__)
def get_script_dir():
"""Returns the directory where the script is located."""
return os.path.dirname(os.path.realpath(__file__))
class DescriptionFile:
def __init__(self, filePath, encoding="utf-16", lang="English"):
self.filePath = filePath
self.encoding = encoding
self.lang = lang
logger.debug(
f"Initializing DescriptionFile with path: {filePath}, encoding: {encoding}, lang: {lang}"
)
fileLines = self.load(self.filePath)
blocks = self.split_into_description_blocks(fileLines)
self.descriptions = [Description(block, lang=self.lang) for block in blocks]
self.descriptions_lookup = {d.id: d for d in self.descriptions}
logger.debug(f"Loaded {len(self.descriptions)} descriptions.")
logger.debug(f"Loading overwrites for {self.lang}")
overwrites = self.load_overwrites()
if self.lang in overwrites:
self.overwrites = overwrites[self.lang]
for desc in self.descriptions:
if desc.id in self.overwrites:
desc.data = self.overwrites[desc.id]
manual_additions = self.load_manual_additions()
if self.lang in manual_additions:
self.manual_additions = manual_additions[self.lang]
for id, matcher in self.manual_additions.items():
self.descriptions.append(
Description([], lang=self.lang, manual={"id": id, "data": matcher})
)
def __str__(self):
return f"DescriptionFile(descriptions={self.descriptions})"
def load(self, filePath: str) -> list[str]:
logger.debug(f"Loading file: {filePath} with encoding: {self.encoding}")
with open(filePath, "r", encoding=self.encoding) as f:
lines = f.readlines()
logger.debug(f"Loaded {len(lines)} lines from the file.")
# trim up until the first description block
while not lines[0].startswith("description"):
logger.debug("Removing line as it does not start with 'description'.")
lines.pop(0)
return lines
def split_into_description_blocks(self, lines: list[str]) -> list[list[str]]:
logger.debug("Splitting lines into description blocks.")
blocks = [[]]
for i in range(len(lines)):
if lines[i].startswith("description"):
if len(blocks) > 0 and len(blocks[-1]) > 0 and blocks[-1][-1] == "\n":
# remove last empty line
blocks[-1].pop()
logger.debug(
f"Removed last empty line from block {len(blocks) - 1}."
)
blocks.append([])
blocks[-1].append(lines[i])
if len(blocks[-1]) > 0 and blocks[-1][-1] == "\n":
# remove last empty line
blocks[-1].pop()
logger.debug("Removed last empty line from the final block.")
if len(blocks[0]) == 0:
blocks.pop(0)
logger.debug(f"Created {len(blocks)} blocks of descriptions.")
return blocks
def load_overwrites(self):
overwrites_file_path = f"{get_script_dir()}/overwrites.json"
with open(overwrites_file_path, "r", encoding="utf-8") as overwrites_file:
return json.loads(overwrites_file.read())
def load_manual_additions(self):
manual_additions_file_path = f"{get_script_dir()}/manual-additions.json"
with open(
manual_additions_file_path, "r", encoding="utf-8"
) as manual_additions_file:
return json.loads(manual_additions_file.read())