Skip to content

Commit 887d5e9

Browse files
committed
Release 1.0.0
0 parents  commit 887d5e9

18 files changed

Lines changed: 2446 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: ci
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
permissions:
7+
contents: read
8+
jobs:
9+
test:
10+
strategy:
11+
matrix:
12+
python: ['3.10', '3.12', '3.13']
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
16+
with:
17+
persist-credentials: false
18+
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
19+
with:
20+
python-version: ${{ matrix.python }}
21+
- run: python -m unittest

‎.github/workflows/release.yml‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: release
2+
on:
3+
push:
4+
tags: ['v*']
5+
permissions:
6+
contents: read
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
12+
with:
13+
persist-credentials: false
14+
- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
15+
with:
16+
python-version: '3.13'
17+
- run: python -m unittest
18+
- run: python -m pip install build
19+
- run: python -m build
20+
- run: test -f "dist/base64hub-${GITHUB_REF_NAME#v}.tar.gz"
21+
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
22+
with:
23+
name: dist
24+
path: dist/
25+
publish:
26+
needs: build
27+
runs-on: ubuntu-latest
28+
environment: pypi
29+
permissions:
30+
id-token: write
31+
steps:
32+
- uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
33+
with:
34+
name: dist
35+
path: dist/
36+
- uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3

‎.gitignore‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
dist/
2+
build/
3+
*.egg-info/
4+
__pycache__/
5+
*.py[cod]
6+
.venv/
7+
venv/
8+
env/
9+
.env/
10+
.tox/
11+
.nox/
12+
.coverage
13+
htmlcov/
14+
.mypy_cache/
15+
.pytest_cache/
16+
.ruff_cache/

‎LICENSE‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 base64hub
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎PUBLISH.md‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Publishing to PyPI
2+
3+
Owner steps for releasing `base64hub` on [PyPI](https://pypi.org/project/base64hub/). Nothing here runs
4+
automatically. This file is not part of the sdist or the wheel.
5+
6+
## 1. Account (once)
7+
8+
1. Register at https://pypi.org/account/register/ as user `base64hub` with `g@base64hub.com`
9+
and verify the email.
10+
2. Turn on two-factor authentication (TOTP app or security key). PyPI requires it before uploads
11+
and API tokens. Store the recovery codes.
12+
3. Optional dry run: the same on https://test.pypi.org/ (separate account and tokens).
13+
4. Check the name is still free: https://pypi.org/project/base64hub/ should return 404.
14+
15+
## 2. Pre-release checks
16+
17+
Run in a full clone of the repo:
18+
19+
```sh
20+
python3 -m unittest
21+
git log --format='%an <%ae>' | sort -u
22+
git log --format='%cn <%ce>' | sort -u
23+
```
24+
25+
Both `git log` lines must print only `base64hub <g@base64hub.com>`. Also run the identity grep
26+
from the Identity section of the internal software contract over the tree and over `dist/` after
27+
the build; it must print nothing.
28+
29+
Version: `1.0.0` lives in two places, `pyproject.toml` and `src/base64hub/__init__.py`. Change both
30+
for a new release; PyPI never accepts the same version twice, even after a delete.
31+
32+
## 3. Build
33+
34+
Tools from apt, no downloads from PyPI:
35+
36+
```sh
37+
sudo apt-get install python3-build flit twine
38+
rm -rf dist
39+
python3 -m build --no-isolation
40+
unzip -l dist/base64hub-*.whl
41+
tar tzf dist/base64hub-*.tar.gz
42+
twine check dist/*
43+
```
44+
45+
Check the wheel holds only `base64hub/*.py`, `base64hub/py.typed`, the license and metadata, and that
46+
`twine check` passes (it renders README.md the way PyPI will).
47+
48+
Smoke test the wheel in a throwaway venv:
49+
50+
```sh
51+
python3 -m venv /tmp/b64-check
52+
/tmp/b64-check/bin/pip install --no-index dist/base64hub-*.whl
53+
/tmp/b64-check/bin/b64 "Hello, hub!"
54+
rm -rf /tmp/b64-check
55+
```
56+
57+
## 4. Upload with an API token
58+
59+
1. PyPI, Account settings, API tokens, "Add API token". The project does not exist before the first
60+
upload, so the first token has to be scoped to the whole account.
61+
2. Upload, giving the token only to this command (no file on disk):
62+
63+
```sh
64+
TWINE_USERNAME=__token__ TWINE_PASSWORD='pypi-...' twine upload dist/*
65+
```
66+
67+
Without the apt `twine`, a throwaway venv works too, nothing installed globally:
68+
`python3 -m venv /tmp/twine && /tmp/twine/bin/pip install twine && /tmp/twine/bin/twine upload dist/*`,
69+
then `rm -rf /tmp/twine`.
70+
3. Right after the first upload: create a new token scoped to project `base64hub` only, delete the
71+
account-wide one, and use the project token for later releases.
72+
4. Check https://pypi.org/project/base64hub/: README rendering, the four project links, author
73+
`base64hub`, license MIT. Then `pipx install base64hub` on a clean machine and `b64 -v`.
74+
75+
## 5. Later: trusted publishing
76+
77+
Once https://github.com/base64hub-com/base64hub-python is public, switch to token-free releases from GitHub Actions:
78+
79+
1. PyPI, project `base64hub`, Publishing, add a GitHub trusted publisher: owner `base64hub`,
80+
repository `python`, workflow `release.yml`, environment `pypi`.
81+
2. In the repo, create the `pypi` environment (Settings, Environments) and a workflow
82+
`.github/workflows/release.yml` that runs on a published release: check out, run
83+
`python3 -m unittest`, `python3 -m build`, then the `pypa/gh-action-pypi-publish` action pinned
84+
to a commit SHA, with `permissions: id-token: write` on that job only.
85+
3. Delete the remaining API token from PyPI.
86+
87+
## 6. After the release
88+
89+
- Fill the PyPI link (`https://pypi.org/project/base64hub/`) in the site's `data/apps.yaml` so the
90+
buttons on `/pypi/` and `/download/` appear.
91+
- Tag the release in git: `git tag v1.0.0`.

‎README.md‎

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# base64hub
2+
3+
Base64 encode, decode and validate from Python or the shell. One small package, standard library only,
4+
with a `b64` command that behaves the same as the base64hub CLI for Go and npm.
5+
6+
- **Lenient decoding**: standard and URL-safe alphabets (even mixed), missing padding, spaces, tabs
7+
and line breaks, `data:...;base64,` prefixes. Errors name the problem and its position.
8+
- **Encoding options**: URL-safe alphabet (RFC 4648 §5), no padding, line wrapping (76 for MIME),
9+
data URIs with the MIME type sniffed from the bytes (PNG, JPEG, GIF, WebP, PDF, SVG, text).
10+
- **Validation** that tells you the variant, whether it is padded and how many bytes it holds.
11+
- Python 3.9 or newer, zero dependencies, type hints included.
12+
13+
Home page: [base64hub.com/pypi](https://base64hub.com/pypi/) · Docs: [base64hub.com/docs/python](https://base64hub.com/docs/python/) ·
14+
Online tools: [base64hub.com](https://base64hub.com/)
15+
16+
## Install
17+
18+
As a command-line tool (isolated environment, puts `b64` on your PATH):
19+
20+
```sh
21+
pipx install base64hub
22+
```
23+
24+
As a library:
25+
26+
```sh
27+
pip install base64hub
28+
```
29+
30+
## Command line
31+
32+
```
33+
b64 [flags] [TEXT]
34+
```
35+
36+
With TEXT, `b64` works on its UTF-8 bytes; without it, it reads stdin (or `-i FILE`).
37+
38+
```console
39+
$ b64 "Hello, hub!"
40+
SGVsbG8sIGh1YiE=
41+
$ b64 -d SGVsbG8sIGh1YiE=
42+
Hello, hub!
43+
$ printf '\xfb\xff\xbf' | b64 --url
44+
-_-_
45+
$ b64 --url --no-pad "Привет"
46+
0J_RgNC40LLQtdGC
47+
```
48+
49+
| Flag | Meaning |
50+
|---|---|
51+
| `-d`, `--decode` | decode (writes the raw bytes, nothing appended) |
52+
| `-u`, `--url` | URL-safe alphabet, `-` and `_` (encode) |
53+
| `-n`, `--no-pad` | no `=` padding (encode) |
54+
| `-w N`, `--wrap N` | wrap encoded output every N characters; 0 = no wrap, 76 = MIME |
55+
| `--data-uri` | output `data:<mime>;base64,...` (encode) |
56+
| `-m TYPE`, `--mime TYPE` | MIME type for `--data-uri` (default: sniffed) |
57+
| `--validate` | print `valid (<variant>, <bytes> bytes)`, or `invalid: <reason>` and exit 1 |
58+
| `-i FILE`, `--input FILE` | read FILE instead of stdin (`-` = stdin) |
59+
| `-o FILE`, `--output FILE` | write FILE instead of stdout (`-` = stdout) |
60+
| `-h`, `--help` | help |
61+
| `-v`, `--version` | print `b64 <version>` |
62+
63+
More examples:
64+
65+
```console
66+
$ b64 -w 76 -i photo.jpg -o photo.b64
67+
$ b64 -d -i photo.b64 -o photo.jpg
68+
$ b64 --data-uri -i logo.png
69+
data:image/png;base64,iVBORw0KGgo...
70+
$ b64 --validate "SGVsbG8sIGh1YiE"
71+
valid (either, 11 bytes)
72+
$ b64 -d -- -_-_ | xxd -p
73+
fbffbf
74+
```
75+
76+
Flags may come before or after TEXT. Short flags can't be combined (`-d -n`, not `-dn`) and take
77+
their value as the next argument (`-w 76`, not `-w76`); long flags also accept `--wrap=76`. Use `--`
78+
before TEXT that starts with `-`, which URL-safe Base64 often does. A bare `-` as TEXT means stdin.
79+
80+
These are usage errors: encoding flags (`-u`, `-n`, `-w`, `--data-uri`, `-m`) with `-d` or
81+
`--validate`; `-u`, `-n` or `-w` with `--data-uri` (data URIs are standard padded Base64 on one line);
82+
`--mime` without `--data-uri`; TEXT together with `-i`; the same file for `-i` and `-o`.
83+
84+
Errors go to stderr as `b64: <message>`. Exit codes: `0` success, `1` invalid input or I/O error,
85+
`2` usage error. When a run fails, a file written with `-o` is removed, so no partial output is left.
86+
`b64` behaves exactly like the Go build of the command, the reference implementation.
87+
88+
The package installs the same command under a second name, `base64hub`, and runs as
89+
`python -m base64hub` too.
90+
91+
## Library
92+
93+
```python
94+
from base64hub import encode, decode, validate, data_uri
95+
96+
encode("Hello, hub!") # 'SGVsbG8sIGh1YiE='
97+
encode(b"\xfb\xff\xbf", url=True) # '-_-_'
98+
encode("Привет", url=True, pad=False) # '0J_RgNC40LLQtdGC'
99+
encode(bytes(100), wrap=76) # MIME lines, no trailing newline
100+
101+
decode("SGVsbG8sIGh1YiE") # b'Hello, hub!' (padding optional)
102+
decode("data:text/plain;base64,SGk=") # b'Hi'
103+
decode("a+b_") # b'k\xe6\xff' (mixed alphabets)
104+
105+
validate("-_-_")
106+
# {'valid': True, 'variant': 'url', 'padded': True, 'bytes': 3}
107+
validate("Zm9v!")
108+
# {'valid': False, 'error': "invalid character '!' at position 4"}
109+
110+
data_uri(b"hi") # 'data:text/plain;charset=utf-8;base64,aGk='
111+
data_uri(png_bytes) # 'data:image/png;base64,...'
112+
data_uri(svg_bytes, "image/svg+xml") # MIME given explicitly
113+
```
114+
115+
- `encode(data: bytes | str, *, url=False, pad=True, wrap=0) -> str`. A `str` is encoded as UTF-8 first.
116+
- `decode(text: str | bytes) -> bytes` raises `base64hub.Base64Error`, a subclass of `ValueError`,
117+
for the first problem from the left. Its `position` attribute is the 0-based position in the
118+
cleaned input; for a bad character or a misplaced `=`, `char` is that character.
119+
- `validate(text) -> dict` never raises. Valid input gives
120+
`{"valid": True, "variant": ..., "padded": ..., "bytes": ...}`: `variant` is `standard` (`+` or `/`
121+
seen), `url` (`-` or `_`), `mixed` (both) or `either` (neither); `padded` means the length with `=`
122+
is a multiple of 4; `bytes` is the decoded length. Invalid input gives
123+
`{"valid": False, "error": "<message decode would raise>"}`.
124+
- `data_uri(data, mime=None) -> str` uses the standard alphabet with padding and never wraps.
125+
`sniff_mime(data)` returns the type it would pick.
126+
127+
### How decoding works
128+
129+
Whitespace means exactly space, tab, CR and LF; anything else, such as a no-break space, is an
130+
invalid character.
131+
132+
1. Skip leading whitespace. If the input starts with `data:` (any case) and contains `;base64,`
133+
(lower case), drop everything up to and including `;base64,`.
134+
2. Remove every space, tab, CR and LF.
135+
3. Strip up to two trailing `=`; more, or `=` anywhere else, is an error.
136+
4. Accept both alphabets; any other character is an error with its position.
137+
5. A length that leaves one character over (length mod 4 = 1) is an error.
138+
139+
Errors are found left to right, positions count characters of the cleaned input from 0, and the
140+
messages are exactly:
141+
142+
- `invalid character '!' at position 4`
143+
- `unexpected '=' at position 2: padding is only allowed at the end`
144+
- `too much padding: 3 '=' at position 2, at most 2 are allowed`
145+
- `invalid length: 5 characters leave a single character in the last group`
146+
147+
## Links
148+
149+
- Package page: [base64hub.com/pypi](https://base64hub.com/pypi/)
150+
- Documentation: [base64hub.com/docs/python](https://base64hub.com/docs/python/)
151+
- Base64 tools in the browser: [base64hub.com](https://base64hub.com/)
152+
- Source and issues: [github.com/base64hub-com/base64hub-python](https://github.com/base64hub-com/base64hub-python)
153+
154+
## License
155+
156+
MIT

0 commit comments

Comments
 (0)