Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
.PHONY: install
install:
@pip install -r requirements/dev.txt
pip install -r requirements/dev.txt -r requirements/prod.txt

.PHONY: venv
venv:
@python3 -m venv .venv
python3 -m venv .venv

.PHONY: fix
fix:
@echo "==> fixing black"
@black --line-length 120 src/

@echo "==> fixing isort"
@isort src/
.PHONY: black
black:
black --line-length 120 src

.PHONY: check
check:
@echo "==> checking isort"
@isort --check-only --diff src
.PHONY: ruff
ruff:
ruff check src --fix

@echo "==> checking black"
@black --check --diff --line-length 120 src
.PHONY: mypy
mypy:
mypy src

@echo "==> checking flake8"
@flake8 src

@echo "==> checking mypy"
@mypy src
.PHONY: fix
fix: black ruff mypy
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"
build-backend = "setuptools.build_meta"

[tool.ruff]
line-length = 120
12 changes: 5 additions & 7 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
mypy
flake8
black
isort
build
twine
python-docx
mypy==1.2.0
ruff==0.0.265
black==23.3.0
build==0.10.0
twine==4.0.2
1 change: 1 addition & 0 deletions requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-docx>=0.8.11
5 changes: 1 addition & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,4 @@ install_requires =
python-docx

[options.packages.find]
where = src

[flake8]
max-line-length=120
where = src
10 changes: 4 additions & 6 deletions src/python_docx_replace/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import re
from typing import Any

from python_docx_replace.exceptions import (EndTagNotFound, InitialTagNotFound,
TableIndexNotFound)
from typing import Any, List
from python_docx_replace.exceptions import EndTagNotFound, InitialTagNotFound, TableIndexNotFound
from python_docx_replace.paragraph import Paragraph

__all__ = ["docx_replace", "docx_blocks", "docx_remove_table"]
Expand Down Expand Up @@ -91,12 +89,12 @@ def docx_remove_table(doc: Any, index: int) -> None:
raise TableIndexNotFound(index, len(doc.tables))


def docx_get_keys(doc: Any) -> set:
def docx_get_keys(doc: Any) -> List[str]:
"""
Search for all keys in the Word document and return a list of unique elements

ATTENTION: The required format for the keys inside the Word document is: ${key}

For a document with the following content: "Hello ${name}, is your phone ${phone}?"
Result example: ["name", "phone"]
"""
Expand Down
6 changes: 3 additions & 3 deletions src/python_docx_replace/block_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def replace(self, initial: str, end: str, keep_block: bool) -> None:
if not runs_to_change.get(run_index):
runs_to_change[run_index] = [char for char_index, char in enumerate(run.text)]

run_to_change: Dict = runs_to_change.get(run_index)
run_to_change: Dict = runs_to_change.get(run_index) # type: ignore[assignment]
if (
(not keep_block)
or (index in range(initial_index, initial_index_plus_key_length))
Expand All @@ -61,7 +61,7 @@ def clear_key_and_after(self, key: str, keep_block: bool) -> None:
if not runs_to_change.get(run_index):
runs_to_change[run_index] = [char for char_index, char in enumerate(run.text)]

run_to_change: Dict = runs_to_change.get(run_index)
run_to_change: Dict = runs_to_change.get(run_index) # type: ignore[assignment]
if (not keep_block) or (index in range(key_index, key_index_plus_key_length)):
run_to_change[run_char_index] = ""

Expand All @@ -81,7 +81,7 @@ def clear_key_and_before(self, key: str, keep_block: bool) -> None:
if not runs_to_change.get(run_index):
runs_to_change[run_index] = [char for char_index, char in enumerate(run.text)]

run_to_change: Dict = runs_to_change.get(run_index)
run_to_change: Dict = runs_to_change.get(run_index) # type: ignore[assignment]
if (not keep_block) or (index in range(key_index, key_index_plus_key_length)):
run_to_change[run_char_index] = ""

Expand Down
2 changes: 1 addition & 1 deletion src/python_docx_replace/key_changer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def replace(self) -> None:
if not self.runs_to_change.get(run_index):
self.runs_to_change[run_index] = [char for char_index, char in enumerate(run.text)]

run_to_change: Dict = self.runs_to_change.get(run_index)
run_to_change: Dict = self.runs_to_change.get(run_index) # type: ignore[assignment]
if index == index_to_replace:
run_to_change[run_char_index] = self.value
else:
Expand Down