mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-19 18:47:55 +08:00
refactor: 重构项目结构,方便后续打包
This commit is contained in:
@@ -5,14 +5,14 @@ from shutil import copy
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from src.file_loader.xaml_load import XamlParser
|
||||
from src.git import get_latest_file_content
|
||||
from src.auto_localization.xaml_load import XamlParser
|
||||
from src.auto_localization.git import get_latest_file_content
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
logging.debug(os.path.abspath('.env'))
|
||||
load_dotenv(dotenv_path='.env')
|
||||
if os.path.exists('../.env'):
|
||||
load_dotenv(dotenv_path='../.env')
|
||||
if os.path.exists('../../.env'):
|
||||
load_dotenv(dotenv_path='../../.env')
|
||||
root_path = os.getenv("LOCALIZATION_PATH")
|
||||
assert root_path, "LOCALIZATION_PATH is not set"
|
||||
zh_cn_path = os.path.join(root_path, "zh-cn.xaml")
|
||||
@@ -29,4 +29,4 @@ def get_latest_file_content(file_path='./cli.py', encoding='utf-8'):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(get_latest_file_content('./cli.py'))
|
||||
print(get_latest_file_content('cli.py'))
|
||||
@@ -8,7 +8,7 @@ from lxml import etree
|
||||
from xmldiff import main
|
||||
from xmldiff.actions import UpdateTextIn, InsertComment, UpdateTextAfter, DeleteNode
|
||||
|
||||
from src.translator.translate import ChatTranslator
|
||||
from src.auto_localization.translate import ChatTranslator
|
||||
|
||||
SPACE = '{http://www.w3.org/XML/1998/namespace}space'
|
||||
|
||||
@@ -1,143 +0,0 @@
|
||||
import unittest
|
||||
|
||||
from xaml_load import XamlParser, judge_encoding, parse_lang_str
|
||||
|
||||
|
||||
class TestXamlParser(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.zh_sample_path = '../../sample/backup/zh-cn.xaml'
|
||||
self.zh_new_sample_path = '../../sample/backup/zh-cn_new.xaml'
|
||||
self.en_sample_path = '../../sample/backup/en-us.xaml'
|
||||
self.en_force_sample_path = '../../sample/backup/en-us_force.xaml'
|
||||
self.en_compare_sample_path = '../../sample/backup/en-us_compare.xaml'
|
||||
self.en_update_sample_path = '../../sample/backup/en-us_update.xaml'
|
||||
with open(self.zh_sample_path, 'r', encoding='utf-8') as f:
|
||||
self.zh_sample_content = f.read()
|
||||
with open(self.zh_new_sample_path, 'r', encoding='utf-8') as f:
|
||||
self.zh_new_sample_content = f.read()
|
||||
with open(self.en_sample_path, 'r', encoding='utf-8') as f:
|
||||
self.en_sample_content = f.read()
|
||||
with open(self.en_force_sample_path, 'r', encoding='utf-8') as f:
|
||||
self.en_force_sample_content = f.read()
|
||||
with open(self.en_compare_sample_path, 'r', encoding='utf-8') as f:
|
||||
self.en_compare_sample_content = f.read()
|
||||
with open(self.en_update_sample_path, 'r', encoding='utf-8') as f:
|
||||
self.en_update_sample_content = f.read()
|
||||
self.zh_path = '../../sample/zh-cn.xaml'
|
||||
self.zh_new_path = '../../sample/zh-cn_new.xaml'
|
||||
self.en_force_path = '../../sample/en-us_force.xaml'
|
||||
self.en_compare_path = '../../sample/en-us_compare.xaml'
|
||||
self.en_update_path = '../../sample/en-us_update.xaml'
|
||||
self.zh_tw_path = '../../sample/zh-tw.xaml'
|
||||
self.ja_path = '../../sample/ja-jp.xaml'
|
||||
self.ko_path = '../../sample/ko-kr.xaml'
|
||||
with open(self.zh_path, 'w', encoding='utf-8') as f:
|
||||
f.write(self.zh_sample_content)
|
||||
with open(self.zh_new_path, 'w', encoding='utf-8') as f:
|
||||
f.write(self.zh_new_sample_content)
|
||||
with open(self.en_force_path, 'w', encoding='utf-8') as f:
|
||||
f.write(self.en_sample_content)
|
||||
with open(self.en_compare_path, 'w', encoding='utf-8') as f:
|
||||
f.write(self.en_sample_content)
|
||||
with open(self.en_update_path, 'w', encoding='utf-8') as f:
|
||||
f.write(self.en_sample_content)
|
||||
|
||||
def test_judge_encoding(self):
|
||||
expected_encoding = 'UTF-8'
|
||||
result_encoding = judge_encoding(self.zh_path)
|
||||
self.assertEqual(expected_encoding, result_encoding)
|
||||
|
||||
def test_parse_lang_str(self):
|
||||
expected_zh_cn_language = "Chinese (Simplified)"
|
||||
expected_zh_tw_language = "Chinese (Traditional)"
|
||||
expected_en_language = "English"
|
||||
expected_ja_language = "Japanese"
|
||||
expected_ko_language = "Korean"
|
||||
self.assertEqual(expected_zh_cn_language, parse_lang_str(self.zh_path))
|
||||
self.assertEqual(expected_zh_tw_language, parse_lang_str(self.zh_tw_path))
|
||||
self.assertEqual(expected_en_language, parse_lang_str(self.en_force_path))
|
||||
self.assertEqual(expected_en_language, parse_lang_str(self.en_compare_path))
|
||||
self.assertEqual(expected_en_language, parse_lang_str(self.en_update_path))
|
||||
self.assertEqual(expected_ja_language, parse_lang_str(self.ja_path))
|
||||
self.assertEqual(expected_ko_language, parse_lang_str(self.ko_path))
|
||||
with self.assertRaises(ValueError):
|
||||
parse_lang_str("./test/bla")
|
||||
|
||||
def test_init(self):
|
||||
zh_parser = XamlParser(parse_type=0, file=self.zh_path)
|
||||
with self.assertRaises(AssertionError):
|
||||
XamlParser(parse_type=1, xaml_string=self.zh_sample_content)
|
||||
zh_string_parser = XamlParser(parse_type=1, language='Chinese', xaml_string=self.zh_sample_content)
|
||||
self.assertIsInstance(zh_parser, XamlParser)
|
||||
self.assertIsInstance(zh_string_parser, XamlParser)
|
||||
self.assertFalse(zh_string_parser.write_xaml())
|
||||
with self.assertRaises(ValueError):
|
||||
XamlParser(parse_type=2)
|
||||
with self.assertRaises(AssertionError):
|
||||
XamlParser(parse_type=0, file="./test/blah.xaml")
|
||||
s = zh_parser.tostring
|
||||
self.assertTrue(s.count('
') == 0)
|
||||
|
||||
def test_xpath(self):
|
||||
zh_parser = XamlParser(parse_type=0, file=self.zh_sample_path)
|
||||
# 检验空结果
|
||||
with self.assertRaises(AssertionError):
|
||||
next(zh_parser.xpath('.//test'))
|
||||
self.assertIsNone(next(zh_parser.xpath('.//test', accept_empty=True)))
|
||||
|
||||
# 检验多结果
|
||||
with self.assertRaises(AssertionError):
|
||||
next(zh_parser.xpath('.//s:String[@x:Key]'))
|
||||
self.assertLess(1, len(list(zh_parser.xpath('.//s:String[@x:Key]', only_one=False))))
|
||||
|
||||
# 检验单结果
|
||||
res = list(zh_parser.xpath('.//s:String[@x:Key="Settings"]'))
|
||||
self.assertEqual(1, len(res))
|
||||
self.assertEqual("设置", res[0].text)
|
||||
|
||||
def test_translate_compare_structure(self):
|
||||
zh_parser = XamlParser(parse_type=0, file=self.zh_sample_path)
|
||||
en_parser = XamlParser(parse_type=0, file=self.en_sample_path)
|
||||
en_compare_parser = XamlParser(parse_type=0, file=self.en_compare_sample_path)
|
||||
self.assertFalse(zh_parser.compare_structure(en_parser))
|
||||
self.assertTrue(zh_parser.compare_structure(en_compare_parser))
|
||||
|
||||
def test_translate_force(self):
|
||||
zh_parser = XamlParser(parse_type=0, file=self.zh_path)
|
||||
zh_parser.translate_force(self.en_force_path, skip_translate=True)
|
||||
with open(self.en_force_path, 'r', encoding='utf-8') as f:
|
||||
translated_content = f.read()
|
||||
compare_parser = XamlParser(parse_type=0, file=self.en_force_path)
|
||||
self.assertEqual(translated_content, self.en_force_sample_content)
|
||||
self.assertTrue(zh_parser.compare_structure(compare_parser))
|
||||
|
||||
def test_translate_compare(self):
|
||||
zh_parser = XamlParser(parse_type=0, file=self.zh_path)
|
||||
en_parser = XamlParser(parse_type=0, file=self.en_compare_path)
|
||||
|
||||
en_parser.translate_compare(zh_parser, skip_translate=True)
|
||||
|
||||
with open(self.en_compare_path, 'r', encoding='utf-8') as f:
|
||||
translated_content = f.read()
|
||||
compare_parser = XamlParser(parse_type=0, file=self.en_compare_path)
|
||||
self.assertEqual(translated_content, self.en_compare_sample_content)
|
||||
self.assertTrue(zh_parser.compare_structure(compare_parser))
|
||||
|
||||
def test_update_translate(self):
|
||||
zh_parser = XamlParser(parse_type=0, file=self.zh_path)
|
||||
zh_new_parser = XamlParser(parse_type=0, file=self.zh_new_path)
|
||||
en_parser = XamlParser(parse_type=0, file=self.en_update_path)
|
||||
en_new_parser = XamlParser(parse_type=0, file=self.en_compare_path)
|
||||
|
||||
en_parser.update_translate(zh_parser, zh_new_parser, skip_translate=True)
|
||||
en_new_parser.update_translate(zh_parser, zh_new_parser, skip_translate=True)
|
||||
with open(self.en_update_path, 'r', encoding='utf-8') as f:
|
||||
translated_content = f.read()
|
||||
compare_parser = XamlParser(parse_type=0, file=self.en_update_path)
|
||||
self.assertEqual(translated_content, self.en_update_sample_content)
|
||||
self.assertTrue(zh_new_parser.compare_structure(compare_parser))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,93 +0,0 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from translate import ChatTranslator
|
||||
|
||||
|
||||
class TestChatTranslator(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.translator = ChatTranslator()
|
||||
|
||||
@patch('openai.ChatCompletion.create')
|
||||
def test_translate_default_sentence(self, mock_create):
|
||||
mock_create.return_value = {
|
||||
'choices': [
|
||||
{
|
||||
'message': {
|
||||
'content': """{"message":200,"content":"Tips: $\\n$$\\n$ 1. Please use this function on the interface with the 'Start Action' button; $\\n$$\\n$ 2. Using a friend's assistance can turn off 'Auto Formation' and manually select operators before starting; $\\n$$\\n$ 3. To conduct a simulation of the paradox, turn off 'Auto Formation', select the skills, and then start on the 'Start Simulation' button interface; $\\n$$\\n$ 4. For 'Annihilation', multiple tasks are built-in under the 'resource/copilot' folder. Please manually form a squad and start on the 'Start Deployment' interface (can be used with 'number of loops'); $\\n$$\\n$ 5. Video recognition is now supported. Please drag the strategy video file and then start. The video resolution should be 16:9, without black borders, simulator borders, or other interfering elements."}"""
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = self.translator.translate()
|
||||
result_split_list = result.split("\n")
|
||||
length = len(result_split_list)
|
||||
self.assertFalse('$' in result)
|
||||
assert_result = """Tips: \n\n 1. Please use this function on the interface with the 'Start Action' button; \n\n 2. Using a friend's assistance can turn off 'Auto Formation' and manually select operators before starting; \n\n 3. To conduct a simulation of the paradox, turn off 'Auto Formation', select the skills, and then start on the 'Start Simulation' button interface; \n\n 4. For 'Annihilation', multiple tasks are built-in under the 'resource/copilot' folder. Please manually form a squad and start on the 'Start Deployment' interface (can be used with 'number of loops'); \n\n 5. Video recognition is now supported. Please drag the strategy video file and then start. The video resolution should be 16:9, without black borders, simulator borders, or other interfering elements."""
|
||||
self.assertEqual(length, 11)
|
||||
self.assertEqual(result, assert_result)
|
||||
|
||||
@patch('openai.ChatCompletion.create')
|
||||
def test_translate_custom_sentence(self, mock_create):
|
||||
mock_create.return_value = {
|
||||
'choices': [
|
||||
{
|
||||
'message': {
|
||||
'content': '{"message":200,"content":"Custom translated text"}'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = self.translator.translate(sentence="This is a custom sentence")
|
||||
self.assertEqual(result, "Custom translated text")
|
||||
|
||||
@patch('openai.ChatCompletion.create')
|
||||
def test_translate_unknown_language(self, mock_create):
|
||||
mock_create.return_value = {
|
||||
'choices': [
|
||||
{
|
||||
'message': {
|
||||
'content': '{"message":404,"content":"未知语言"}'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = self.translator.translate(target_language="未知")
|
||||
self.assertIsNone(result)
|
||||
|
||||
@patch('openai.ChatCompletion.create')
|
||||
def test_translate_error(self, mock_create):
|
||||
mock_create.return_value = {
|
||||
'choices': [
|
||||
{
|
||||
'message': {
|
||||
'content': '{"message":500,"content":"内部服务器错误"}'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = self.translator.translate()
|
||||
self.assertIsNone(result)
|
||||
|
||||
@patch('openai.ChatCompletion.create')
|
||||
def test_wrong_input_error(self, mock_create):
|
||||
mock_create.return_value = {
|
||||
'choices': [
|
||||
{
|
||||
'message': {
|
||||
'content': '我不明白你在说什么'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = self.translator.translate()
|
||||
self.assertIsNone(result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user