Skip to content

Commit

Permalink
Use poetry and pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGrosjean committed Sep 22, 2024
1 parent 333e265 commit a1a3555
Show file tree
Hide file tree
Showing 9 changed files with 943 additions and 91 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/flake8_test_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ jobs:
- name: Coverage with pytest
run: |
coverage run --omit "tests/*.py" -m pytest ./tests/ --junitxml=./report.xml -s
coverage report
coverage xml
genbadge tests -i ./report.xml -s -o ./tests.svg
genbadge coverage -i ./coverage.xml -s -o ./coverage.svg
# genbadge tests -i ./report.xml -s -o ./tests.svg
# genbadge coverage -i ./coverage.xml -s -o ./coverage.svg
# - name: Setup node
# uses: actions/setup-node@v1
# - name: Upload badge to GitHub
Expand Down
14 changes: 5 additions & 9 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@
"**/.pytest_cache": true,
"**/test-reports": true,
"**/*.pyc": true,
},
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test_*.py"
},
"python.testing.pytestArgs": [
"tests", "-vv"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
161 changes: 99 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,48 +17,63 @@ Example : If I translate from English to French, the source directory refers to

I recommend you to install a Python environment with poetry, conda, virtualenv or pipenv.

##### Poetry
### Poetry

```
poetry install
```

##### Conda
For example with conda,
[download and install miniconda](https://docs.conda.io/en/latest/miniconda.html)
#### Pre-commit (only need for contributing to the project)

Create a conda environment
<!-- [On Windows] Copy the file `coverage.bat` in the folder `.git/hooks/` and name it `pre-commit` (it is normal there is no `.bat`). -->

Install pre-commit

```shell
pre-commit install
```
conda create -n paradox_loc python=3.7.1

### Conda

If you prefer conda to poetry, [download and install miniconda](https://docs.conda.io/en/latest/miniconda.html)

Create a conda environment

```shell
conda create -n paradox_loc python=3.10
```

Activate the conda environment
```

```shell
activate paradox_loc
```

Install Levenshtein package (only needed for *apply_diff_all*)

```shell
conda install python-levenshtein
```
conda install python-levenshtein==0.12.0
```

OR
```
pip install python-levenshtein==0.12.0

```shell
pip install python-levenshtein
```

Install pandas package (only needed for *extract_existing_translation*)
```

```shell
pip install pandas
```

Install coverage tool (only need for contributing to the project)
```
pip install pytest coverage genbadge[all]
```
Copy the file `coverage.bat` in the folder `.git/hooks/` and name it `pre-commit` (it is normal there is no `.bat`).
#### Pre-commit (only need for contributing to the project)

Install pre-commit (only need for contributing to the project)
```
<!-- [On Windows] Copy the file `coverage.bat` in the folder `.git/hooks/` and name it `pre-commit` (it is normal there is no `.bat`). -->

Install pre-commit

```shell
pip install pre-commit
pre-commit install
```
Expand All @@ -70,12 +85,13 @@ pre-commit install
> Update the localization files of your source languages.
> It also copy in the parent of the target directory an extract of the source localization and all files in target_dir.
```
```shell
python src/update_source.py <source_dir> <target_dir> <source_lang>
```

Example to update the English files of the HoI4 mod Kaiserreich
```

```shell
python src/update_source.py <...>\Steam\steamapps\workshop\content\394360\1521695605 <...>\GitHub\Traduction-FR-Kaiserreich\Traduction-FR-Kaiserreich english
```

Expand All @@ -87,18 +103,19 @@ Get your Paratranz API token in the settings tab of your Paratranz profile.

Get the Paratranz project id by open the project and see the number in the url.

```
```shell
python src/update_paratranz.py <token> <project_id> <loc_dir> <language>
```

Example to update the RICE project (translation from English) on Paratranz

```
```shell
python src/update_paratranz.py <token> 2617 <...>\GitHub\RICE-for-CK3\RICE\localization english
```

To compile the code, run the following command
```

```shell
python compile_update_paratranz.py build
```

Expand All @@ -112,7 +129,7 @@ Get your Paratranz API token in the settings tab of your Paratranz profile.

Get the Paratranz project id by open the project and see the number in the url.

```
```shell
python src/download_paratranz.py <token> <project_id> <raw_dir_path> <utf8_dir_path>
```

Expand All @@ -121,22 +138,26 @@ python src/download_paratranz.py <token> <project_id> <raw_dir_path> <utf8_dir_p
> Add in the destination files the lines which are missing from the source ones
#### For Imperator Rome and sooner
```

```shell
python src/add_missing_lines.py <source_dir> <dest_dir>
```

Example to add in French files missing lines from English files
```

```shell
python src/add_missing_lines.py "<...>\game\localization\english" "<...>\game\localization\french"
```

#### For EUIV, HoI4 or Stellaris
```

```shell
python src/add_missing_lines.py <localisation_dir> <localisation_dir> -source_lang <source_lang> -dest_lang <dest_lang>
```

Example to add in French files missing lines from English files
```

```shell
python src/add_missing_lines.py "<...>\localisation" "<...>\localisation" -source_lang english -dest_lang french
```

Expand All @@ -146,7 +167,8 @@ python src/add_missing_lines.py "<...>\localisation" "<...>\localisation" -sourc
> Apply the differences between an old source version and a current source version to your current destination files.
#### For Imperator Rome and sooner, and new EUIV, HoI4 or Stellaris localisation format (a subdirectory for each language)
```

```shell
python src/apply_diff_all.py <old_localisation_dir> <current_localisation_dir>
```

Expand All @@ -155,20 +177,23 @@ python src/apply_diff_all.py <old_localisation_dir> <current_localisation_dir>
* <current_dest_localisation_dir> contains the localisation of the old destination version

Example to apply in French files the modifications done in English
```

```shell
python src/apply_diff_all.py "<...>\V1\localisation\english" "<...>\V2\localisation\english" -dest_dir "<...>\V1\localisation\french"
```

#### For old EUIV, HoI4 or Stellaris localisation format (all files in the same directory)
```

```shell
python src/apply_diff_all.py <old_localisation_dir> <current_localisation_dir> -source_lang <source_lang> -dest_lang <dest_lang>
```

* <old_localisation_dir> contains the localisation of the old source version
* <current_localisation_dir> contains the localisation of both new source version and old destination version

Example to apply in French files the modifications done in English
```

```shell
python src/apply_diff_all.py "<...>\V1\localisation" "<...>\V2\localisation" -source_lang english -dest_lang french
```

Expand All @@ -195,17 +220,20 @@ It is useful when you have keys like spellcheck_ignore in the source files.
The target is EUIV or sooner whereas the source can be CK2/Vic2 or EUIV and sooner.

#### Usage
```

```shell
python src/extract_existing_translation.py <extract_source_dir> <extract_dest_dir> <target_source_dir> <target_dest_dir> <source_lang> <dest_lang> -source_col_ck2 <source_col_ck2> -dest_col_ck2 <dest_col_ck2>
```

Example to add in French CK3 files some texts from CK2 files.
```

```shell
python src/extract_existing_translation.py "<...>\CK2\localisation" "<...>\CK2\localisation" "<...>\CK3\localization\english" "<...>\CK3\localization\french" english french -source_col_ck2 1 -dest_col_ck2 2
```

Example to add in French HoI IV mod files some texts from Vanilla files.
```

```shell
python src/extract_existing_translation.py "<...>\Steam\steamapps\common\Hearts of Iron IV\localisation" "<...>\Steam\steamapps\common\Hearts of Iron IV\localisation" "<...>\mod\localisation" "<...>\mod\localisation" english french
```

Expand All @@ -217,20 +245,23 @@ python src/extract_existing_translation.py "<...>\Steam\steamapps\common\Hearts
This script only works for EUIV and sooner.

#### Usage
```

```shell
python src/extract_paratranz_translation.py <paratranz_dir> <localisation_dir> <language>
```

Example to extract french reviewed translations
```

```shell
python src/extract_paratranz_translation.py "<...>\paratranz" "<...>\localisation" french
```

By default it extracts only reviewed translation.
If you want to extract not reviewed translation, add `-extract_not_review` to the command.

Example to extract french translations (reviewed or not)
```

```shell
python src/extract_paratranz_translation.py "<...>\paratranz" "<...>\localisation" french -extract_not_review
```

Expand All @@ -245,42 +276,46 @@ It works only for Imperator Rome and sooner.
The usage is explicated in this [Steam guide](https://steamcommunity.com/sharedfiles/filedetails/?id=2342385980).

#### Developer Usage
```

```shell
python src/copy_on_other_languages.py <localization_dir> <source_lang> <dest_lang1> <dest_lang2> ...
```

Example to add some languages to available only in English and French (the added languages will have English text).
```

```shell
python src/copy_on_other_languages.py "<...>\MyMod\localisation" english korean german russian simp_chinese spanish
```

To compile it (after running once `pip install cx_Freeze`), use the following command
```

```shell
cxfreeze -c src/copy_on_other_languages.py
```


### Get duplicates keys
> Get the list of duplicated keys in the localization to avoid localization erased by others.
### Get duplicates keys

> Get the list of duplicated keys in the localization to avoid localization erased by others.
#### Limits
#### Limits

The duplicated keys are searched in English localisation files only.
The duplicated keys are searched in English localisation files only.

#### Usage
#### Usage

To get the full list of duplicated keys run the following command
```
To get the full list of duplicated keys run the following command

```shell
python src/get_duplicates_key.py "<...>\MyMod\localisation"
```

To get only the list of duplicated keys which already cause a bug
(the value is not the same for each duplication), add *-only_different_value* to the command.
To get only the list of duplicated keys which already cause a bug
(the value is not the same for each duplication), add *-only_different_value* to the command.


### [DEPRECATED] Apply diff for EUIV and sooner
```

```shell
python src/apply_diff.py <old_source_file> <new_source_file> <dest_file>
```

Expand All @@ -293,31 +328,33 @@ Optionnal parameters :
The default behavior replace the destination value by the new source value.

Example to apply the English update from 0.11.1 to 0.11.2 to Russia file in French:

```shell
python src/apply_diff.py <...>\0.11.1\localisation\KR_Russia_l_english.yml <...>\0.11.2\localisation\KR_Russia_l_english.yml <...>\Translation_FR\localisation\KR_Russia_l_french.yml -space_prefix " " -keep_edited
```
<...>\0.11.1\localisation\KR_Russia_l_english.yml <...>\0.11.2\localisation\KR_Russia_l_english.yml <...>\Translation_FR\localisation\KR_Russia_l_french.yml -space_prefix " " -keep_edited
```

After running this, the new and edited lines need to be fixed by hand by seeking `:9 "`

## Tests

Go to the tests directory and run unittest
```
cd tests
python -m unittest discover
Run the tests with pytest

```shell
pytest
```

If some tests fails with error `ModuleNotFoundError: No module named 'src'`, run the following command in the root directory.


On Windows:

```
```shell
set PYTHONPATH=%CD%
```

On Linux

```
```shell
export PYTHONPATH=${PYTHONPATH}:${PWD}
```

Expand Down
3 changes: 2 additions & 1 deletion coverage.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env sh
python -m coverage run --omit "tests/*.py" -m pytest ./tests/ --junitxml=./report.xml -s
poetry shell
coverage run --omit "tests/*" -m pytest ./tests/ --junitxml=./report.xml -s
python -m coverage xml
genbadge tests -i ./report.xml -s -o ./badges/tests.svg
genbadge coverage -i ./coverage.xml -s -o ./badges/coverage.svg
Loading

0 comments on commit a1a3555

Please sign in to comment.