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
2 changes: 2 additions & 0 deletions docs/release_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Release Log
- **Add Czech, Slovak, Slovenian and Croatian packs, locales.CS, SK, SL and HR.** ``parser_for(locales.CS).parse("Jan Novák, MUDr")`` gives title ``MUDr``, given ``Jan``, family ``Novák``, where 1.4.0 through 2.8.0 gave given ``MUDr``, family ``Jan Novák``. Each pack chooses its one language and turns on words the default does not read: ``ml.`` and ``st.`` after the name in all four (``Jiří Dienstbier ml.``), the Czech and Slovak academic titles and ``DrSc.``, and Czech ``Pan`` and ``roz.``. Choosing the language drops ``st``'s Saint readings (``St Clair Smith`` reads given ``St``), which choosing ``fr`` as well keeps. ``CS`` and ``SK`` also read two family names, so ``Alena Pešová Řehořová`` gives family ``Pešová Řehořová`` and two given names read as a given name and a family name (``Petr Pavel Novák`` gives family ``Pavel Novák``); a family comma keeps both. :issue:`753`
- **Add Hindi and Bengali packs, locales.HI and locales.BN, for the romanized Indic titles.** ``parser_for(locales.HI).parse("Smt Nirmala Sitharaman")`` gives title ``Smt``, given ``Nirmala``, where 1.4.0 through 2.8.0 gave given ``Smt``, middle ``Nirmala``. ``HI`` reads ``Smt``, ``Shrimati`` and ``Shriyut`` as titles and ``BN`` ``Smt``, ``Shrimati`` and ``Adhyapak``; ``Shri`` and ``Sushri`` stay name words under both, since each opens given names (``Shri Kant``, ``Sushri Sangita``). See :doc:`locales`. :issue:`345`, :issue:`753`

- **Add --order to python -m nameparser, to read a name in another order.** ``python -m nameparser --order family-first "Kovács János Péter"`` gives family ``Kovács``, given ``János``, where the command, in 2.0.0 through 2.8.0, read only the default order and gave family ``Péter``. It takes ``given-first``, ``family-first`` or ``family-first-given-last``; a name in Han, Hangul or kana keeps its script's order. :issue:`983`

**Breaking Changes**

- **Remove HumanName, Constants and the rest of the 1.x compatibility layer.** ``from nameparser import HumanName`` raises ``ImportError``, where 1.4.0 through 2.8.0 imported it (2.8.0 warning where it was constructed); ``parse("Dr. Juan de la Vega")`` reads the same name, with given ``Juan`` and family ``de la Vega``. ``nameparser.parser``, ``nameparser.util``, ``nameparser.config``'s ``Constants``, ``CONSTANTS`` and managers, and the five word-list names and two module paths 2.2 renamed go too, and a pickled ``HumanName`` or ``Constants`` no longer loads; the word lists in ``nameparser.config`` stay. See :doc:`migrate`. :issue:`357`
Expand Down
12 changes: 11 additions & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,17 @@ form to pipe somewhere:
{"title": "", "given": "John", "middle": "", "family": "Doe", "suffix": "", "nickname": "", "maiden": ""}

Add ``--locale`` to parse with a locale pack (for example ``--locale
ru``); see :doc:`locales`.
ru``); see :doc:`locales`. ``--order`` reads the name in another order
— ``given-first`` (the default), ``family-first`` or
``family-first-given-last``, the three ``Policy.name_order`` values —
and replaces any order a pack sets. A name written wholly in Han,
Hangul or kana keeps the order its script gives it, and a family comma
fixes the order whatever the flag:

::

$ python -m nameparser --order family-first --json "Kovács János Péter"
{"title": "", "given": "János", "middle": "Péter", "family": "Kovács", "suffix": "", "nickname": "", "maiden": ""}

Where next
----------
Expand Down
46 changes: 40 additions & 6 deletions nameparser/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@

python -m nameparser "Dr. Juan Q. Xavier de la Vega III"
python -m nameparser --json "Doe, John"
python -m nameparser --order family-first "Kovács János Péter"
"""
import argparse
import dataclasses
import json

from nameparser import parse
from nameparser import (
FAMILY_FIRST,
FAMILY_FIRST_GIVEN_LAST,
GIVEN_FIRST,
Lexicon,
Locale,
PolicyPatch,
parser_for,
)

# The vocabulary [rules] tables' spelling of each name_order constant,
# copied because __main__ imports only the public package;
# tests/v2/test_cli.py holds it equal to _locale's.
_ORDERS = {
"given-first": GIVEN_FIRST,
"family-first": FAMILY_FIRST,
"family-first-given-last": FAMILY_FIRST_GIVEN_LAST,
}


def main(argv: list[str] | None = None) -> int:
Expand All @@ -18,18 +37,33 @@ def main(argv: list[str] | None = None) -> int:
ap.add_argument("--locale", metavar="CODE",
help="parse with a locale pack (e.g. 'ru'); see "
"nameparser.locales")
ap.add_argument("--order", choices=_ORDERS,
help="the order to read a name's words in "
"(default: given-first); a Han, Hangul or kana "
"name keeps its script's order, and a family "
"comma fixes the order")
args = ap.parse_args(argv)
pack = None
# `is not None`, not truthiness: --locale "" must reach get() and
# exit 2 listing the codes, not silently fall back to the default
if args.locale is not None:
from nameparser import locales, parser_for
from nameparser import locales
try:
parser = parser_for(locales.get(args.locale))
pack = locales.get(args.locale)
except KeyError as exc:
ap.error(exc.args[0]) # exits 2, message to stderr
n = parser.parse(args.name)
else:
n = parse(args.name)
# The order is written into the pack's own patch, so it replaces a
# pack's order without a two-pack conflict warning, and the parser
# is built once, parser_for re-emitting its warnings (#337)
if args.order is not None:
order = _ORDERS[args.order]
if pack is None:
pack = Locale(code="cli_order", lexicon=Lexicon.empty(),
policy=PolicyPatch(name_order=order))
else:
pack = dataclasses.replace(pack, policy=dataclasses.replace(
pack.policy, name_order=order))
n = (parser_for() if pack is None else parser_for(pack)).parse(args.name)
if args.json:
print(json.dumps(n.as_dict(), ensure_ascii=False))
return 0
Expand Down
87 changes: 87 additions & 0 deletions tests/v2/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
import subprocess
import sys

import pytest

import nameparser.locales
from nameparser import FAMILY_FIRST, Lexicon, Locale, PolicyPatch
from nameparser import __main__ as cli
from nameparser import _locale


def _run(*args: str) -> subprocess.CompletedProcess:
return subprocess.run([sys.executable, "-m", "nameparser", *args],
Expand Down Expand Up @@ -73,3 +80,83 @@ def test_cli_locale_ja_activates_nothing_by_itself() -> None:
default = _run("山田太郎")
assert packed.returncode == 0
assert packed.stdout == default.stdout


def _fields(capsys: pytest.CaptureFixture[str], *argv: str) -> dict[str, str]:
# in process, unlike _run: fast, and the warning tests can see what
# main() emits
assert cli.main([*argv, "--json"]) == 0
result: dict[str, str] = json.loads(capsys.readouterr().out)
return result


def _roles(fields: dict[str, str]) -> tuple[str, str, str]:
return fields["given"], fields["middle"], fields["family"]


@pytest.mark.parametrize(("order", "roles"), [
("given-first", ("Kovács", "János", "Péter")),
("family-first", ("János", "Péter", "Kovács")),
("family-first-given-last", ("Péter", "János", "Kovács")),
])
def test_cli_order_reads_the_words_in_that_order(
capsys: pytest.CaptureFixture[str],
order: str, roles: tuple[str, str, str]) -> None:
assert _roles(_fields(capsys, "--order", order,
"Kovács János Péter")) == roles


def test_cli_order_keeps_the_locale_packs_vocabulary_and_policy(
capsys: pytest.CaptureFixture[str]) -> None:
# es reads Lic as a title (the pack's lexicon); cs reads two family
# names (its policy), which a parser rebuilt from the order alone
# would lose: family 'Novák', given 'Jan', middle 'Petr'
es = _fields(capsys, "--locale", "es", "--order", "family-first",
"Lic Kovács János")
assert (es["title"], es["given"], es["family"]) == (
"Lic", "János", "Kovács")
cs = _fields(capsys, "--locale", "cs", "--order", "family-first",
"Novák Jan Petr")
assert _roles(cs) == ("Petr", "", "Novák Jan")


def test_cli_order_wins_over_a_packs_order(
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch) -> None:
# no shipped pack sets name_order, so a stand-in does
pack = Locale(code="xx", lexicon=Lexicon.empty(),
policy=PolicyPatch(name_order=FAMILY_FIRST))
monkeypatch.setattr(nameparser.locales, "get", lambda code: pack)
assert _fields(capsys, "--locale", "xx",
"Kovács János Péter")["family"] == "Kovács"
assert _fields(capsys, "--locale", "xx", "--order", "given-first",
"Kovács János Péter")["family"] == "Péter"


def test_cli_order_leaves_a_han_name_in_its_scripts_order(
capsys: pytest.CaptureFixture[str]) -> None:
# Policy.script_orders reads a wholly-Han name family-first (rules/W4)
fields = _fields(capsys, "--order", "given-first", "毛 泽东")
assert (fields["given"], fields["family"]) == ("泽东", "毛")


def test_cli_order_with_a_locale_warns_once_from_main() -> None:
# the parser is built once, by parser_for, which re-emits the
# segmenterless warning from its caller's frame (#337)
with pytest.warns(UserWarning, match="segment_scripts") as record:
assert cli.main(["--locale", "ja", "--order", "family-first",
"--json", "高橋一郎"]) == 0
assert len(record) == 1
assert record[0].filename == cli.__file__


def test_cli_order_unknown_value_exits_2_listing_the_choices(
capsys: pytest.CaptureFixture[str]) -> None:
with pytest.raises(SystemExit) as exc:
cli.main(["--order", "sideways", "Jane Doe"])
assert exc.value.code == 2
assert "family-first-given-last" in capsys.readouterr().err


def test_cli_order_spellings_match_the_vocabulary_rules_tables() -> None:
assert cli._ORDERS == _locale._ORDERS
Loading