Skip to content

Commit

Permalink
Support writing TOML config and data files
Browse files Browse the repository at this point in the history
- Use tomlkit instead of pytomlpp, as it can retain comments and element order
PhuNH committed Sep 8, 2023
1 parent c703a84 commit ed95ad7
Showing 6 changed files with 38 additions and 80 deletions.
9 changes: 5 additions & 4 deletions hugo_gettext/config.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
from mdit_py_plugins.deflist import deflist_plugin
from mdit_py_plugins.front_matter import front_matter_plugin

from .utils import read_file
from . import utils


def _read_domain_config(domain_config) -> List[str]:
@@ -110,11 +110,12 @@ def _get_rtl_langs() -> List[str]:


class Config:
def __init__(self, hugo_config, customs_path: str = ''):
def __init__(self, hugo_config, config_path: str = '', customs_path: str = ''):
if 'i18n' not in hugo_config:
return

i18n_config = hugo_config['i18n']
self.config_path = config_path
# env. var. > config value
self.package = os.environ.get('PACKAGE', '') or i18n_config.get('package', '')
if not self.package:
@@ -181,8 +182,8 @@ def __init__(self, hugo_config, customs_path: str = ''):
def from_config_file(cls, config_path: str, customs_path: str = ''):
if not config_path:
config_path = 'hugo.toml'
hugo_config = read_file(config_path)
return cls(hugo_config, customs_path)
hugo_config = utils.read_file(config_path)
return cls(hugo_config, config_path, customs_path)


def initialize(renderer_cls: Type[RendererProtocol],
4 changes: 2 additions & 2 deletions hugo_gettext/generation/g_lang.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@
from mdit_py_i18n.utils import L10NResult

from .g_domain import HugoDomainG
from .. import utils
from ..utils import HugoGProtocol

L10NResults = Dict[str, List[L10NResult]]
@@ -102,8 +103,7 @@ def generate_data_files(self):

o_result = self.default_domain_g.localize_object(data, self.g.hg_config.excluded_data_keys, self.g.mdi)
if o_result.l10n_count > 0:
with open(target_path, 'w+') as f_target:
f_target.write(yaml.dump(data, default_flow_style=False, allow_unicode=True))
utils.write_file(target_path, data)

def generate_data_others(self):
"""Generate strings file and data files, and localize config fields.
4 changes: 1 addition & 3 deletions hugo_gettext/generation/index.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
import shutil
from typing import Dict

import yaml
from markdown_it import MarkdownIt

from .g_lang import HugoLangG
@@ -55,5 +54,4 @@ def generate(args):
Generation(src_strings, src_data, hg_config, mdi).generate(args.keep_locale)

if hg_config.hugo_config != original_hugo_config:
with open('config.yaml', 'w+') as f_config:
f_config.write(yaml.dump(hg_config.hugo_config, default_flow_style=False, allow_unicode=True))
utils.write_file(hg_config.config_path, hg_config.hugo_config)
18 changes: 16 additions & 2 deletions hugo_gettext/utils.py
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
from enum import Enum
from typing import Dict, Protocol, Any, List

import pytomlpp
import tomlkit
import yaml
from markdown_it import MarkdownIt
from mdit_py_i18n.utils import DomainGenerationProtocol, DomainExtractionProtocol
@@ -65,17 +65,31 @@ def load_str(self, content: str):
if self == TextFormat.YAML:
return yaml.safe_load(content)
elif self == TextFormat.TOML:
return pytomlpp.loads(content)
return tomlkit.loads(content)
else:
return {}

def dump_obj(self, obj):
if self == TextFormat.YAML:
return yaml.dump(obj, default_flow_style=False, allow_unicode=True)
elif self == TextFormat.TOML:
return tomlkit.dumps(obj)
else:
return ''


def read_file(file_path: str):
text_format = TextFormat.decide_by_path(file_path)
with open(file_path) as f:
return text_format.load_str(f.read())


def write_file(file_path: str, obj):
text_format = TextFormat.decide_by_path(file_path)
with open(file_path, 'w+') as f:
f.write(text_format.dump_obj(obj))


def read_data_files(file_paths: List[str]) -> Dict:
src_data = {}
for path in file_paths:
81 changes: 13 additions & 68 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ python = "^3.8"
mdit-py-i18n = "^0.1.2"
markdown-gettext = "^0.1.2"
mdit-py-hugo = "^0.3.1"
pytomlpp = "^1.0.13"
tomlkit = "^0.12.1"

[tool.poetry.scripts]
hugo-gettext = 'hugo_gettext.cli:main'

0 comments on commit ed95ad7

Please sign in to comment.