Skip to content

Commit

Permalink
Fix error when 0 or inf wrapwidths are passed to po2md (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Mar 4, 2023
1 parent 0c5bdcd commit b60c57d
Show file tree
Hide file tree
Showing 20 changed files with 92 additions and 30 deletions.
16 changes: 8 additions & 8 deletions docs/pre-commit-hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ so you don't need to specify them.
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v1.1.2
rev: v1.1.3
hooks:
- id: md2po
args:
Expand All @@ -32,7 +32,7 @@ so you don't need to specify them.
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v1.1.2
rev: v1.1.3
hooks:
- id: md2po
files: ^README\.md
Expand All @@ -53,7 +53,7 @@ po2md
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v1.1.2
rev: v1.1.3
hooks:
- id: po2md
args:
Expand All @@ -68,7 +68,7 @@ po2md
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v1.1.2
rev: v1.1.3
hooks:
- id: po2md
files: ^README\.md
Expand All @@ -91,7 +91,7 @@ md2po2md
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v1.1.2
rev: v1.1.3
hooks:
- id: md2po2md
args:
Expand All @@ -107,7 +107,7 @@ md2po2md
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v1.1.2
rev: v1.1.3
hooks:
- id: md2po2md
files: ^README\.md
Expand All @@ -126,7 +126,7 @@ mdpo2html
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v1.1.2
rev: v1.1.3
hooks:
- id: mdpo2html
args:
Expand All @@ -141,7 +141,7 @@ mdpo2html
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v1.1.2
rev: v1.1.3
hooks:
- id: mdpo2html
files: ^README\.html
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "mdpo"
version = "1.1.2"
version = "1.1.3"
description = "Markdown files translation using PO files."
readme = "README.md"
license = "BSD-3-Clause"
Expand Down
11 changes: 6 additions & 5 deletions src/mdpo/po2md/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
po_escaped_string,
pofiles_to_unique_translations_dicts,
)
from mdpo.text import min_not_max_chars_in_a_row, parse_wrapwidth_argument
from mdpo.text import (
INFINITE_WRAPWIDTH,
min_not_max_chars_in_a_row,
parse_wrapwidth_argument,
)


class Po2Md:
Expand Down Expand Up @@ -184,11 +188,8 @@ def __init__(self, pofiles, ignore=frozenset(), po_encoding=None, **kwargs):
)

self.wrapwidth = (
# infinite would be 2 ** 24 because the underlying library
# for wrapping (md-ulb-pwrap) doesn't accept the infinite
# Python object
(
2 ** 24 if kwargs['wrapwidth'] in [float('inf'), 0]
INFINITE_WRAPWIDTH if kwargs['wrapwidth'] in [float('inf'), 0]
else parse_wrapwidth_argument(kwargs['wrapwidth'])
) if 'wrapwidth' in kwargs else 80
)
Expand Down
7 changes: 5 additions & 2 deletions src/mdpo/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import sys


INFINITE_WRAPWIDTH = 65535


def and_join(values):
"""Comma and space join using "and" between the last and penultimate items.
Expand Down Expand Up @@ -127,9 +130,9 @@ def parse_strint_0_inf(value):

num = float(value)
try:
return int(num) if num > 0 else float('inf')
return int(num) if num > 0 else INFINITE_WRAPWIDTH
except OverflowError: # cannot convert float infinity to integer
return float('inf')
return INFINITE_WRAPWIDTH


def parse_wrapwidth_argument(value):
Expand Down
5 changes: 4 additions & 1 deletion tests/test_unit/test_po2md/test_po2md_wrapwidth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Wrapping rendering tests for ``po2md`` CLI."""

import glob
import math
import os

import pytest
Expand All @@ -17,11 +18,13 @@
)


@pytest.mark.parametrize('wrapwidth', (10, 40, 80, 2000))
@pytest.mark.parametrize('wrapwidth', (10, 40, 80, 2000, 0, math.inf))
@pytest.mark.parametrize('filename', EXAMPLES)
def test_wrapwidth(filename, wrapwidth):
filepath_in = os.path.join(EXAMPLES_DIR, filename)
wrapwidth = wrapwidth if wrapwidth != math.inf else 'inf'
filepath_out = f'{filepath_in}.{wrapwidth}.expect.md'

po_filepath = os.path.join(
os.path.dirname(filepath_in),
os.path.splitext(os.path.basename(filepath_in))[0] + '.po',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
> Un bloque de cita que debe ser envuelto si el parámetro de ancho de envoltura es menor a su largo.
> 345 7 9 1 cita rota
>
> 34 6 8 0 cita rota
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
> Un bloque de cita que debe ser envuelto si el parámetro de ancho de envoltura es menor a su largo.
> 345 7 9 1 cita rota
>
> 34 6 8 0 cita rota
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. 4567
1. 45 7 9 1 broken text
1. 4 6 8 0 1 broken text
1. Un elemento de lista que debe envolverse ignorando el ancho del delimitador
1. Otro elemento de lista que debe envolverse ignorando el ancho del delimitador
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. 4567
1. 45 7 9 1 broken text
1. 4 6 8 0 1 broken text
1. Un elemento de lista que debe envolverse ignorando el ancho del delimitador
1. Otro elemento de lista que debe envolverse ignorando el ancho del delimitador
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Algo de texto largo que debería envolverse si el parámetro de envoltura de ancho es menor que su largo.

Algo más de texto largo que contiene `un código en línea` y un [largo largo largo largo largo enlace cuyo objetivo no debería ser envuelto en múltiples líneas](#123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789).
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Algo de texto largo que debería envolverse si el parámetro de envoltura de ancho es menor que su largo.

Algo más de texto largo que contiene `un código en línea` y un [largo largo largo largo largo enlace cuyo objetivo no debería ser envuelto en múltiples líneas](#123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* 支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* 支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。支持常见的温度传感器(例如,常见的热敏电阻、AD595、AD597、AD849x、PT100、PT1000、MAX6675、MAX31855、MAX31856、MAX31865、BME280、HTU21D和LM75)。还可以配置自定义热敏电阻和自定义模拟温度传感器。
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* 同类项目中最佳的性能。Klipper能够在新旧微控制器上实现高步进速率。即使是旧的8 位微控制器也可以发超过每秒175K步的速率。在较新的微控制器上,每秒数百万步也可以实现。更高的步进速率可以实现更高的打印速度。步进事件计时即使在高速下也能保持精确,提高了整体稳定性。
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* 同类项目中最佳的性能。Klipper能够在新旧微控制器上实现高步进速率。即使是旧的8 位微控制器也可以发超过每秒175K步的速率。在较新的微控制器上,每秒数百万步也可以实现。更高的步进速率可以实现更高的打印速度。步进事件计时即使在高速下也能保持精确,提高了整体稳定性。
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- [ ] 7890
- [ ] 78 0 2 4 texto roto
- [x] 7 9 1 3 5 texto roto
- [ ] Un elemento de lista de tareas que debe envolverse ignorando el ancho del checkbox
- [X] Otro elemento de lista de tareas que debe envolverse ignorando el ancho del checkbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- [ ] 7890
- [ ] 78 0 2 4 texto roto
- [x] 7 9 1 3 5 texto roto
- [ ] Un elemento de lista de tareas que debe envolverse ignorando el ancho del checkbox
- [X] Otro elemento de lista de tareas que debe envolverse ignorando el ancho del checkbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- 3456
- 345 7 9 1 texto roto
- 34 6 8 0 texto roto
- Un elemento de lista que debe envolverse ignorando el ancho de la marca
- Otro elemento de lista que debe envolverse ignorando el ancho de la marca
- [¿Puedo ejecutar múltiples instancias de MyApp en la misma máquina del host?](#puedo-ejecutar-multiples-instancias-de-myapp-en-la-misma-maquina-de-host)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- 3456
- 345 7 9 1 texto roto
- 34 6 8 0 texto roto
- Un elemento de lista que debe envolverse ignorando el ancho de la marca
- Otro elemento de lista que debe envolverse ignorando el ancho de la marca
- [¿Puedo ejecutar múltiples instancias de MyApp en la misma máquina del host?](#puedo-ejecutar-multiples-instancias-de-myapp-en-la-misma-maquina-de-host)
29 changes: 16 additions & 13 deletions tests/test_unit/test_text.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Tests for mdpo text utilities."""

import math

import pytest

from mdpo.text import (
INFINITE_WRAPWIDTH,
min_not_max_chars_in_a_row,
parse_escaped_pair,
parse_strint_0_inf,
Expand Down Expand Up @@ -61,20 +61,20 @@ def test_parse_escaped_pair(text, expected_result, maybe_raises):
(
('1', 1),
('1.1', 1),
(-1, math.inf),
(-1.1, math.inf),
('-1', math.inf),
('-1.1', math.inf),
(0, math.inf),
(-0, math.inf),
(-1, INFINITE_WRAPWIDTH),
(-1.1, INFINITE_WRAPWIDTH),
('-1', INFINITE_WRAPWIDTH),
('-1.1', INFINITE_WRAPWIDTH),
(0, INFINITE_WRAPWIDTH),
(-0, INFINITE_WRAPWIDTH),
('a', ValueError),
('nan', ValueError),
('NotANumber', ValueError),
('inf', math.inf),
('InF', math.inf),
('-inf', math.inf),
('-iNf', math.inf),
('iNfInItY', math.inf),
('inf', INFINITE_WRAPWIDTH),
('InF', INFINITE_WRAPWIDTH),
('-inf', INFINITE_WRAPWIDTH),
('-iNf', INFINITE_WRAPWIDTH),
('iNfInItY', INFINITE_WRAPWIDTH),
('+1E6', 1000000),
),
)
Expand All @@ -92,6 +92,9 @@ def test_parse_wrapwidth_argument(value):
with pytest.raises(ValueError, match=expected_msg):
parse_wrapwidth_argument(value)
else:

assert parse_wrapwidth_argument(value) == (
float('inf') if float(value) == 0 else float(value)
INFINITE_WRAPWIDTH if 'inf' in value or value == '0' else int(
value,
)
)

0 comments on commit b60c57d

Please sign in to comment.