ahvn.utils.basic.jinja_utils module

ahvn.utils.basic.jinja_utils.babel_init(path, langs=None, main=None, overwrite=False, encoding=None, **kwargs)[源代码]

Initializes Babel localization files in the specified path.

参数:
  • path (str) -- The root directory where 'locale' folder will be created. Templates are expected to be in this path or a subfolder.

  • langs (Optional[List[str]]) -- A list of language codes (e.g., ['en', 'fr', 'es', 'zh']). If empty, only the POT file is extracted. Defaults to None, which will use the encoding in the config file ("prompts.langs").

  • main (Optional[str]) -- The main language of the POT file. Defaults to None, which will use the encoding in the config file ("prompts.main").

  • overwrite (bool) -- Clear all existing translations and create new .po files. Defaults to False.

  • encoding (Optional[str]) -- The encoding to use for the Babel configuration file. Defaults to None, which will use the encoding in the config file ("prompts.encoding").

  • **kwargs -- Additional keyword arguments passed to cmd (subprocess.Popen).

ahvn.utils.basic.jinja_utils.babel_compile(path, langs=None, main=None, **kwargs)[源代码]

Compiles Babel .po files into .mo files.

参数:
  • path (str) -- The root directory containing the 'locale' folder.

  • langs (Optional[List[str]]) -- A list of language codes (e.g., ['en', 'fr', 'es', 'zh']). If empty, will scan directories under locale path. Defaults to None, which will use the encoding in the config file ("prompts.langs").

  • main (Optional[str]) -- The main language of the POT file. Defaults to None, which will use the encoding in the config file ("prompts.main").

  • **kwargs -- Additional keyword arguments passed to cmd (subprocess.Popen).

ahvn.utils.basic.jinja_utils.load_jinja_env(path=None, lang=None, env=None, **kwargs)[源代码]

Loads a Jinja2 environment with the specified path and language.

参数:
  • path (Optional[Union[str,List[str],Dict[str,str]]]) -- The root directory where Jinja2 templates are located. - If a list, it will be treated as multiple paths to search for templates (ChoiceLoader). - If a dictionary, it should map environment names to their respective paths (PrefixLoader). - If None, it will load from scan paths defined in the config. Defaults to None, which will use the encoding in the config file ("prompts.scan") with PrefixLoader on their subdirectories.

  • lang (Optional[str]) -- The language code for localization. Defaults to None, which will use the encoding in the config file ("prompts.lang").

  • env (Optional[Environment]) -- Pre-loaded Jinja2 Environment to use. If provided, it will be used to augment the template loading. Defaults to None.

  • **kwargs -- Additional keyword arguments for the Jinja2 Environment. By default, we use: - trim_blocks=True - lstrip_blocks=True - extensions including 'jinja2.ext.i18n'

返回:

A Jinja2 Environment instance. Specifically, a NativeEnvironment is used to support native python types.

返回类型:

Environment

ahvn.utils.basic.jinja_utils.create_jinja(path, entry='default.jinja', content='', autojinja=False, autoi18n=False)[源代码]

Creates a Jinja2 template from the specified path and entry, and renders it with the provided keyword arguments.

参数:
  • path (str) -- The root directory where Jinja2 templates are located.

  • entry (str) -- The template filename to render. Defaults to "default.jinja".

  • content (str) -- The content of the template to be created if it does not exist. Defaults to an empty string.

  • autojinja (bool) -- Whether to automatically convert "content" to a standard Jinja2 template. Defaults to False. If True, it will call autojinja with jinja as the LLM preset after creating the template.

  • autoi18n (bool) -- Whether to automatically initialize Babel localization files. Defaults to False. If True, it will call autoi18n with translator as the LLM preset after creating the template.

返回:

The path to the created Jinja2 template.

返回类型:

str

ahvn.utils.basic.jinja_utils.get_lang_instruction(lang)[源代码]

Get instruction string for the specified language.

参数:

lang (str) -- Language code (e.g., 'en', 'fr', 'es', 'zh').

返回:

Instruction string for the specified language.

返回类型:

str