-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
268 additions
and
20 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from fluentformatter import FluentFile, FluentFormatter | ||
from fluentformatter import FluentFile, FluentFormatter |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import os | ||
import re | ||
import chardet | ||
from datetime import datetime | ||
|
||
def find_top_level_dir(start_dir): | ||
marker_file = 'SpaceStation14.sln' | ||
current_dir = start_dir | ||
while True: | ||
if marker_file in os.listdir(current_dir): | ||
return current_dir | ||
parent_dir = os.path.dirname(current_dir) | ||
if parent_dir == current_dir: | ||
print(f"Не удалось найти {marker_file} начиная с {start_dir}") | ||
exit(-1) | ||
current_dir = parent_dir | ||
def find_ftl_files(root_dir): | ||
ftl_files = [] | ||
for root, dirs, files in os.walk(root_dir): | ||
for file in files: | ||
if file.endswith('.ftl'): | ||
ftl_files.append(os.path.join(root, file)) | ||
return ftl_files | ||
|
||
def detect_encoding(file_path): | ||
with open(file_path, 'rb') as file: | ||
raw_data = file.read() | ||
return chardet.detect(raw_data)['encoding'] | ||
|
||
def parse_ent_blocks(file_path): | ||
try: | ||
encoding = detect_encoding(file_path) | ||
with open(file_path, 'r', encoding=encoding) as file: | ||
content = file.read() | ||
except UnicodeDecodeError: | ||
print(f"Ошибка при чтении файла {file_path}. Попытка чтения в UTF-8.") | ||
try: | ||
with open(file_path, 'r', encoding='utf-8') as file: | ||
content = file.read() | ||
except UnicodeDecodeError: | ||
print(f"Не удалось прочитать файл {file_path}. Пропускаем.") | ||
return {} | ||
|
||
ent_blocks = {} | ||
current_ent = None | ||
current_block = [] | ||
|
||
for line in content.split('\n'): | ||
if line.startswith('ent-'): | ||
if current_ent: | ||
ent_blocks[current_ent] = '\n'.join(current_block) | ||
current_ent = line.split('=')[0].strip() | ||
current_block = [line] | ||
elif current_ent and (line.strip().startswith('.desc') or line.strip().startswith('.suffix')): | ||
current_block.append(line) | ||
elif line.strip() == '': | ||
if current_ent: | ||
ent_blocks[current_ent] = '\n'.join(current_block) | ||
current_ent = None | ||
current_block = [] | ||
else: | ||
if current_ent: | ||
ent_blocks[current_ent] = '\n'.join(current_block) | ||
current_ent = None | ||
current_block = [] | ||
|
||
if current_ent: | ||
ent_blocks[current_ent] = '\n'.join(current_block) | ||
|
||
return ent_blocks | ||
|
||
def remove_duplicates(root_dir): | ||
ftl_files = find_ftl_files(root_dir) | ||
all_ents = {} | ||
removed_duplicates = [] | ||
|
||
for file_path in ftl_files: | ||
ent_blocks = parse_ent_blocks(file_path) | ||
for ent, block in ent_blocks.items(): | ||
if ent not in all_ents: | ||
all_ents[ent] = (file_path, block) | ||
|
||
for file_path in ftl_files: | ||
try: | ||
encoding = detect_encoding(file_path) | ||
with open(file_path, 'r', encoding=encoding) as file: | ||
content = file.read() | ||
|
||
ent_blocks = parse_ent_blocks(file_path) | ||
for ent, block in ent_blocks.items(): | ||
if all_ents[ent][0] != file_path: | ||
content = content.replace(block, '') | ||
removed_duplicates.append((ent, file_path, block)) | ||
|
||
content = re.sub(r'\n{3,}', '\n\n', content) | ||
|
||
with open(file_path, 'w', encoding=encoding) as file: | ||
file.write(content) | ||
except Exception as e: | ||
print(f"Ошибка при обработке файла {file_path}: {str(e)}") | ||
|
||
# Сохранение лога удаленных дубликатов | ||
log_filename = f"removed_duplicates_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log" | ||
with open(log_filename, 'w', encoding='utf-8') as log_file: | ||
for ent, file_path, block in removed_duplicates: | ||
log_file.write(f"Удален дубликат: {ent}\n") | ||
log_file.write(f"Файл: {file_path}\n") | ||
log_file.write("Содержимое:\n") | ||
log_file.write(block) | ||
log_file.write("\n\n") | ||
|
||
print(f"Обработка завершена. Проверено файлов: {len(ftl_files)}") | ||
print(f"Лог удаленных дубликатов сохранен в файл: {log_filename}") | ||
|
||
if __name__ == "__main__": | ||
script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
main_folder = find_top_level_dir(script_dir) | ||
root_dir = os.path.join(main_folder, "Resources\\Locale\\ru-RU") | ||
remove_duplicates(root_dir) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import os | ||
import logging | ||
from datetime import datetime | ||
|
||
def find_top_level_dir(start_dir): | ||
marker_file = 'SpaceStation14.sln' | ||
current_dir = start_dir | ||
while True: | ||
if marker_file in os.listdir(current_dir): | ||
return current_dir | ||
parent_dir = os.path.dirname(current_dir) | ||
if parent_dir == current_dir: | ||
print(f"Не удалось найти {marker_file} начиная с {start_dir}") | ||
exit(-1) | ||
current_dir = parent_dir | ||
def setup_logging(): | ||
log_filename = f"cleanup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log" | ||
logging.basicConfig(filename=log_filename, level=logging.INFO, | ||
format='%(asctime)s - %(levelname)s - %(message)s') | ||
console = logging.StreamHandler() | ||
console.setLevel(logging.INFO) | ||
logging.getLogger('').addHandler(console) | ||
return log_filename | ||
|
||
def remove_empty_files_and_folders(path): | ||
removed_files = 0 | ||
removed_folders = 0 | ||
|
||
for root, dirs, files in os.walk(path, topdown=False): | ||
# Удаление пустых файлов | ||
for file in files: | ||
file_path = os.path.join(root, file) | ||
if os.path.getsize(file_path) == 0: | ||
try: | ||
os.remove(file_path) | ||
logging.info(f"Удален пустой файл: {file_path}") | ||
removed_files += 1 | ||
except Exception as e: | ||
logging.error(f"Ошибка при удалении файла {file_path}: {str(e)}") | ||
|
||
# Удаление пустых папок | ||
if not os.listdir(root): | ||
try: | ||
os.rmdir(root) | ||
logging.info(f"Удалена пустая папка: {root}") | ||
removed_folders += 1 | ||
except Exception as e: | ||
logging.error(f"Ошибка при удалении папки {root}: {str(e)}") | ||
|
||
return removed_files, removed_folders | ||
|
||
if __name__ == "__main__": | ||
script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
main_folder = find_top_level_dir(script_dir) | ||
root_dir = os.path.join(main_folder, "Resources\\Locale") | ||
log_file = setup_logging() | ||
|
||
logging.info(f"Начало очистки в директории: {root_dir}") | ||
files_removed, folders_removed = remove_empty_files_and_folders(root_dir) | ||
logging.info(f"Очистка завершена. Удалено файлов: {files_removed}, удалено папок: {folders_removed}") | ||
print(f"Лог операций сохранен в файл: {log_file}") |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from fluent.syntax import ast | ||
from fluent.syntax import ast | ||
from fluentast import FluentAstAbstract | ||
|
||
|
||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import typing | ||
import typing | ||
import logging | ||
|
||
from pydash import py_ | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import lokalise | ||
import lokalise | ||
import typing | ||
from lokalisemodels import LokaliseKey | ||
from pydash import py_ | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import typing | ||
import typing | ||
import os | ||
from pydash import py_ | ||
from project import Project | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import pathlib | ||
import pathlib | ||
import os | ||
import glob | ||
from file import FluentFile | ||
|
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@echo off | ||
|
||
call pip install -r requirements.txt | ||
call python ./yamlextractor.py | ||
call python ./keyfinder.py | ||
call python ./clean_duplicates.py | ||
call python ./clean_empty.py |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env sh | ||
|
||
# make sure to start from script dir | ||
if [ "$(dirname $0)" != "." ]; then | ||
cd "$(dirname $0)" | ||
fi | ||
|
||
pip install -r requirements.txt | ||
python3 ./yamlextractor.py | ||
python3 ./keyfinder.py | ||
python3 ./clean_duplicates.py | ||
python3 ./clean_empty.py |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import logging | ||
import logging | ||
import typing | ||
|
||
from fluent.syntax import FluentParser, FluentSerializer | ||
|
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
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