Skip to content

Commit

Permalink
2024.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
FredHappyface committed Jan 19, 2024
1 parent 747372c commit 90f2c3b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 60 deletions.
57 changes: 31 additions & 26 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
repos:
- repo: https://github.com/FHPythonUtils/Blackt
rev: '2022.0.3'
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
hooks:
- id: blackt
- id: ruff
args: [ --fix ]

- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.347
hooks:
- id: pyright

- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/FHPythonUtils/Blackt
rev: '2024'
hooks:
- id: isort
- id: blackt

- repo: https://github.com/pycqa/pylint
rev: v3.0.0a6
- repo: https://github.com/Lucas-C/pre-commit-hooks-safety
rev: v1.3.2
hooks:
- id: pylint
exclude: "tests/"
args: [--disable=import-error,--jobs=0]
- id: python-safety-dependencies-check
files: pyproject.toml

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
exclude: "tests/"
- id: end-of-file-fixer
exclude: "tests/"
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
- id: check-shebang-scripts-are-executable
- id: check-symlinks
- id: check-toml
- id: check-vcs-permalinks
- id: check-yaml
- id: detect-private-key
- id: mixed-line-ending

- repo: https://github.com/asottile/pyupgrade
rev: v3.7.0
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/boidolr/pre-commit-images
rev: v1.2.1
rev: v1.5.1
hooks:
- id: optimize-avif
exclude: "tests/"
- id: optimize-jpg
exclude: "tests/"
- id: optimize-png
exclude: "tests/"
- id: optimize-svg
exclude: "tests/"
- id: optimize-webp
exclude: "tests/"

exclude: "tests/data|documentation/reference"
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All major and minor version changes will be documented in this file. Details of
patch-level version changes can be found in [commit messages](../../commits/master).

## 2024.0.1 - 2024/01/19

- fix: preserve line endings

## 2024 - 2024/01/07

- update dependencies
Expand Down
22 changes: 14 additions & 8 deletions blackt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,26 @@ def convertSpacesToTabs(files: list[str], find: str, replace: str):


def convertFile(file: str, find: str, replace: str):
"""Convert spaces to tabs of vice versa
"""Convert spaces to tabs or vice versa.
Args:
file (str): file to modify
find (str): tabs/ spaces to find
replace (str): tabs/ spaces to replace
"""
lines = Path(file).read_text(encoding="utf-8").split("\n")
outLines = []
for line in lines:
match = re.match(f"^({find})*", line)
span = match.span()
outLines.append(replace * (span[1] // len(find)) + line[span[1] :])
Path(file).write_text("\n".join(outLines), encoding="utf-8")
file_path = Path(file)

with file_path.open(encoding="utf-8", newline="") as fp:
out_lines = []

pattern = re.compile(f"^({find})*")

for line in fp.read().splitlines(keepends=True):
match = pattern.match(line)
span = match.span() # type:ignore[reportOptionalMemberAccess]
out_lines.append(replace * (span[1] // len(find)) + line[span[1] :])

file_path.write_text("".join(out_lines), encoding="utf-8", newline="")


def runBlack(unknown: list[str]) -> tuple[int, str]:
Expand Down
33 changes: 28 additions & 5 deletions documentation/reference/blackt/index.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Blackt

[Blackt Index](../README.md#blackt-index) /
Blackt
[Blackt Index](../README.md#blackt-index) / Blackt

> Auto-generated documentation for [blackt](../../../blackt/__init__.py) module.
- [Blackt](#blackt)
- [_doSysExec](#_dosysexec)
- [convertFile](#convertfile)
- [convertSpacesToTabs](#convertspacestotabs)
- [convertTabsToSpaces](#converttabstospaces)
Expand All @@ -15,11 +15,34 @@ Blackt
- [runBlack](#runblack)
- [Modules](#modules)

## _doSysExec

[Show source in __init__.py:116](../../../blackt/__init__.py#L116)

Execute a command and check for errors.

#### Arguments

- `command` *str* - commands as a string
- `errorAsOut` *bool, optional* - redirect errors to stdout

#### Returns

- `tuple[int,` *str]* - tuple of return code (int) and stdout (str)

#### Signature

```python
def _doSysExec(command: str, errorAsOut: bool = True) -> tuple[int, str]: ...
```



## convertFile

[Show source in __init__.py:80](../../../blackt/__init__.py#L80)

Convert spaces to tabs of vice versa
Convert spaces to tabs or vice versa.

#### Arguments

Expand Down Expand Up @@ -93,7 +116,7 @@ def main(): ...

## printOutput

[Show source in __init__.py:102](../../../blackt/__init__.py#L102)
[Show source in __init__.py:108](../../../blackt/__init__.py#L108)

Print the output

Expand All @@ -107,7 +130,7 @@ def printOutput(out: str): ...

## runBlack

[Show source in __init__.py:97](../../../blackt/__init__.py#L97)
[Show source in __init__.py:103](../../../blackt/__init__.py#L103)

Run black with forwarded args

Expand Down
4 changes: 1 addition & 3 deletions documentation/reference/blackt/module.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Module

[Blackt Index](../README.md#blackt-index) /
[Blackt](./index.md#blackt) /
Module
[Blackt Index](../README.md#blackt-index) / [Blackt](./index.md#blackt) / Module

> Auto-generated documentation for [blackt.__main__](../../../blackt/__main__.py) module.
- [Module](#module)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "blackt"
license = "mit"
version = "2024"
version = "2024.0.1"
description = "Black but with tabs"
authors = ["FredHappyface"]
classifiers = [
Expand Down
34 changes: 17 additions & 17 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@

THISDIR = str(Path(__file__).resolve().parent)

from blackt import _doSysExec, convertFile
from blackt import _doSysExec, convertFile # noqa: E402


def aux_testConvertFile(sourceFile: str, destFile: str, find: str, replace: str):
with NamedTemporaryFile(mode="w", suffix=".txt", delete=False, newline="") as file:
src = Path(sourceFile).open(encoding="utf-8", newline="").read()
Path(file.name).write_text(src, encoding="utf-8", newline="")
convertFile(file.name, find, replace)
tempFileContents = Path(file.name).open(encoding="utf-8", newline="").read()
assert "\r" not in tempFileContents
assert tempFileContents == Path(destFile).open(encoding="utf-8", newline="").read()


def test_convertFile_toTabs():
with NamedTemporaryFile(suffix=".txt", delete=False) as file:
Path(file.name).write_text(
Path(f"{THISDIR}/data/spaces.txt").read_text(encoding="utf-8"), encoding="utf-8"
)
convertFile(file.name, find=" ", replace="\t")
assert Path(file.name).read_text(encoding="utf-8") == Path(
f"{THISDIR}/data/tabs.txt"
).read_text(encoding="utf-8")
aux_testConvertFile(
f"{THISDIR}/data/spaces.txt", f"{THISDIR}/data/tabs.txt", find=" ", replace="\t"
)


def test_convertFile_toSpaces():
with NamedTemporaryFile(suffix=".txt", delete=False) as file:
Path(file.name).write_text(
Path(f"{THISDIR}/data/tabs.txt").read_text(encoding="utf-8"), encoding="utf-8"
)
convertFile(file.name, find="\t", replace=" ")
assert Path(file.name).read_text(encoding="utf-8") == Path(
f"{THISDIR}/data/spaces.txt"
).read_text(encoding="utf-8")
aux_testConvertFile(
f"{THISDIR}/data/tabs.txt", f"{THISDIR}/data/spaces.txt", find="\t", replace=" "
)


def test_shell():
Expand Down

0 comments on commit 90f2c3b

Please sign in to comment.