Skip to content

Commit

Permalink
Merge pull request #147 from hugovk/update-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Aug 2, 2020
2 parents 15ecb0a + 52538c1 commit 5df1fc1
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 116 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Docs

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Cache
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: docs-${{ hashFiles('**/tox.ini') }}
restore-keys: |
docs-
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8

- name: Install dependencies
run: |
python -m pip install -U pip
python -m pip install -U tox
- name: Docs
run: tox -e docs
22 changes: 8 additions & 14 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@ on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: pip cache
uses: actions/cache@v1
- name: Cache
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: lint-pip-${{ hashFiles('**/setup.py') }}
path: |
~/.cache/pip
~/.cache/pre-commit
key: lint-${{ hashFiles('**/setup.py') }}-${{ hashFiles('**/.pre-commit-config.yaml') }}
restore-keys: |
lint-pip-
- name: pre-commit cache
uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
key: lint-pre-commit-${{ hashFiles('**/.pre-commit-config.yaml') }}
restore-keys: |
lint-pre-commit-
lint-
- name: Set up Python
uses: actions/setup-python@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
echo "::set-output name=dir::$(pip cache dir)"
- name: pip cache
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ build
dist
docs/_build*
htmlcov
site
tags
3 changes: 3 additions & 0 deletions docs/filesize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Filesize

::: humanize.filesize
3 changes: 3 additions & 0 deletions docs/i18n.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Internationalisation

::: humanize.i18n
11 changes: 11 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# humanize

Welcome to the humanize API reference.

* [Number](number)
* [Time](time)
* [Filesize](filesize)
* [I18n](i18n)

Or see [README.md](https://github.com/jmoiron/humanize/blob/master/README.md)
for usage examples.
3 changes: 3 additions & 0 deletions docs/number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Number

::: humanize.number
3 changes: 3 additions & 0 deletions docs/time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Time

::: humanize.time
18 changes: 18 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
site_name: humanize
site_url: https://example.com/docs/TODO
repo_url: https://github.com/jmoiron/humanize
theme:
name: "material"

nav:
- Home: index.md
- Number: number.md
- Time: time.md
- Filesize: filesize.md
- Internationalisation: i18n.md

plugins:
- search
- mkdocstrings:
watch:
- src/humanize
16 changes: 8 additions & 8 deletions src/humanize/filesize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

"""Bits & Bytes related humanization."""
"""Bits and bytes related humanization."""

suffixes = {
"decimal": ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"),
Expand All @@ -10,18 +10,18 @@


def naturalsize(value, binary=False, gnu=False, format="%.1f"):
"""Format a number of bytes like a human readable filesize (eg. 10 kB).
"""Format a number of bytes like a human readable filesize (e.g. 10 kB).
By default, decimal suffixes (kB, MB) are used.
Non-gnu modes are compatible with jinja2's ``filesizeformat`` filter.
Non-GNU modes are compatible with jinja2's `filesizeformat` filter.
Args:
value (int, float, string): Integer to convert.
binary (Boolean): If `True`, uses binary suffixes (KiB, MiB) with base 2**10
instead of 10**3.
gnu (Boolean): If `True`, the binary argument is ignored and GNU-style
(`ls -sh` style) prefixes are used (K, M) with the 2**10 definition.
value (int, float, str): Integer to convert.
binary (bool): If `True`, uses binary suffixes (KiB, MiB) with base
2<sup>10</sup> instead of 10<sup>3</sup>.
gnu (bool): If `True`, the binary argument is ignored and GNU-style
(`ls -sh` style) prefixes are used (K, M) with the 2**10 definition.
format (str): Custom formatter.
"""
if gnu:
Expand Down
66 changes: 57 additions & 9 deletions src/humanize/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ def get_translation():


def activate(locale, path=None):
"""Set 'locale' as current locale. Search for locale in directory 'path'
@param locale: language name, eg 'en_GB'
@param path: path to search for locales"""
"""Activate internationalisation.
Set `locale` as current locale. Search for locale in directory `path`.
Args:
locale (str): Language name, e.g. `en_GB`.
path (str): Path to search for locales.
Returns:
dict: Translations.
"""
if path is None:
path = _DEFAULT_LOCALE_PATH

Expand All @@ -41,19 +49,37 @@ def activate(locale, path=None):


def deactivate():
"""Deactivate internationalisation."""
_CURRENT.locale = None


def gettext(message):
"""Get translation.
Args:
message (str): Text to translate.
Returns:
str: Translated text.
"""
return get_translation().gettext(message)


def pgettext(msgctxt, message):
"""'Particular gettext' function.
It works with 'msgctxt' .po modifiers and allow duplicate keys with
different translations.
This GNU gettext function was added in Python 3.8, so for older versions we
reimplement it. It works by joining msgctx and msgid by '4' byte."""
It works with `msgctxt` .po modifiers and allows duplicate keys with different
translations.
Args:
msgctxt (str): Context of the translation.
message (str): Text to translate.
Returns:
str: Translated text.
"""
# This GNU gettext function was added in Python 3.8, so for older versions we
# reimplement it. It works by joining `msgctx` and `message` by '4' byte.
try:
# Python 3.8+
return get_translation().pgettext(msgctxt, message)
Expand All @@ -65,12 +91,34 @@ def pgettext(msgctxt, message):


def ngettext(message, plural, num):
"""Plural version of gettext.
Args:
message (str): Singlular text to translate.
plural (str): Plural text to translate.
num (str): The number (e.g. item count) to determine translation for the
respective grammatical number.
Returns:
str: Translated text.
"""
return get_translation().ngettext(message, plural, num)


def gettext_noop(message):
"""Example usage:
"""Mark a string as a translation string without translating it.
Example usage:
```python
CONSTANTS = [gettext_noop('first'), gettext_noop('second')]
def num_name(n):
return gettext(CONSTANTS[n])"""
return gettext(CONSTANTS[n])
```
Args:
message (str): Text to translate in the future.
Returns:
str: Original text, unchanged.
"""
return message
Loading

0 comments on commit 5df1fc1

Please sign in to comment.