Metadata-Version: 2.4
Name: msx-charset
Version: 0.2.0
Summary: MSX character set codecs for Python
Author-email: yamamo-to <humorum@gmail.com>
Project-URL: Repository, https://github.com/yamamo-to/msx-charset
Project-URL: Issue, https://github.com/yamamo-to/msx-charset/issues
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# MSX Character Set Converter

[![Test](https://github.com/yamamo-to/msx-charset/actions/workflows/test.yml/badge.svg)](https://github.com/yamamo-to/msx-charset/actions/workflows/test.yml)

A Python library for converting between Unicode and MSX character sets. It registers
standard `codecs` so you can use the familiar `str.encode` / `bytes.decode` API,
filesystem I/O, and incremental/stream interfaces.

## Features

- MSX character sets:
  - **Japanese** (`msx-jp`) — hiragana/katakana, dakuten/handakuten, graphic characters
  - **International** (`msx-intl`)
  - **Brazilian** (`msx-br`)
- Python **3.9+**
- `errors` handling: `strict`, `replace`, `ignore`
- Full `codecs` integration: incremental encoder/decoder, stream reader/writer
- Underscore aliases: `msx_jp`, `msx_intl`, `msx_br`

Importing the package registers the codecs:

```python
import msx_charset  # required once per process

"こんにちは".encode("msx-jp")
```

## Installation

```bash
pip install msx-charset
```

## Usage

### Japanese (`msx-jp`)

```python
import msx_charset

text = "こんにちは"
encoded = text.encode("msx-jp")
decoded = encoded.decode("msx-jp")
```

Voiced/semi-voiced kana and two-byte graphic characters (e.g. `月`) are supported.

### International (`msx-intl`)

```python
import msx_charset

text = "áéíóú"
encoded = text.encode("msx-intl")
decoded = encoded.decode("msx-intl")
```

### Brazilian (`msx-br`)

```python
import msx_charset

text = "Olá, mundo!"
encoded = text.encode("msx-br")
decoded = encoded.decode("msx-br")
```

### Error handling

By default, `errors="strict"` is used:

| Situation | `strict` | `replace` | `ignore` |
|-----------|----------|-----------|----------|
| Unmappable character on **encode** | `UnicodeEncodeError` | `0x3F` (`?`) | skip |
| Invalid/unknown byte on **decode** | `UnicodeDecodeError` | `?` | skip |
| Incomplete graphic sequence (`0x01` …) | `UnicodeDecodeError` | `?` | skip |

```python
# Legacy behaviour (silent ?) — use replace explicitly
text.encode("msx-jp", errors="replace")
data.decode("msx-jp", errors="replace")
```

**Note (0.2.0):** Versions before 0.2.0 behaved like `replace` even when `strict`
was requested. From 0.2.0 onward, `strict` raises as documented above.

### Files and streams

```python
import msx_charset

with open("message.txt", "w", encoding="msx-jp") as f:
    f.write("Hello、こんにちは")

with open("message.txt", "r", encoding="msx-jp") as f:
    text = f.read()
```

### Japanese encoding notes

- Full-width space (U+3000) is defined at both `0x90` and `0xA0` on decode; both
  decode to `　`. **Encode** uses the canonical byte **`0xA0`**. Text round-trips
  are stable; raw byte-for-byte round-trip is not guaranteed when the source used
  `0x90` for space.

## Development

### Setup

```bash
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install -e .
```

`pip install -e .` is recommended so package metadata (including version from Git
tags via [setuptools-scm](https://github.com/pypa/setuptools-scm)) is available.

You can also add the repo root to `PYTHONPATH` to run tests without installing;
codecs still work, but `msx_charset.__version__` is `0.0.0+unknown` until the
package is installed.

### Running tests

```bash
python -m unittest discover -s tests
```

## Contributing

Contributions are welcome. Please open a Pull Request.

## License

MIT License
