added 'glossary' command
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import sys
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
from pydoc import pager
|
||||
from ..configuration import Config
|
||||
from ..glossary import Glossary
|
||||
|
||||
|
||||
class GlossaryCmdError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def print_paged(text: str) -> None:
|
||||
pager(text)
|
||||
|
||||
|
||||
def get_glossary_file_path(name: str, config: Config) -> Path:
|
||||
"""
|
||||
Get the complete filename for a glossary with the given path.
|
||||
"""
|
||||
if not config.glossaries:
|
||||
raise GlossaryCmdError("Can't create glossary name without a glossary directory")
|
||||
return Path(config.glossaries, name).with_suffix(Glossary.file_suffix).absolute()
|
||||
|
||||
|
||||
def list_glossaries(args: argparse.Namespace, config: Config) -> None:
|
||||
"""
|
||||
List existing glossaries in the 'glossaries' directory.
|
||||
"""
|
||||
if not config.glossaries:
|
||||
raise GlossaryCmdError("Glossaries directory missing in the configuration file")
|
||||
glossaries = Path(config.glossaries).glob(f'*.{Glossary.file_suffix}')
|
||||
for glo in glossaries:
|
||||
print(Glossary.from_file(glo).to_str())
|
||||
|
||||
|
||||
def print_glossary(args: argparse.Namespace, config: Config) -> None:
|
||||
"""
|
||||
Print an existing glossary.
|
||||
"""
|
||||
# sanity checks
|
||||
if args.name is None:
|
||||
raise GlossaryCmdError("Missing glossary name")
|
||||
if config.glossaries is None and args.file is None:
|
||||
raise GlossaryCmdError("Glossaries directory missing in the configuration file")
|
||||
# create file path or use the given one
|
||||
glo_file = Path(args.file) if args.file else get_glossary_file_path(args.name, config)
|
||||
if not glo_file.exists():
|
||||
raise GlossaryCmdError(f"Glossary '{glo_file}' does not exist")
|
||||
# read glossary
|
||||
glo = Glossary.from_file(glo_file)
|
||||
print_paged(glo.to_str(with_entries=True))
|
||||
|
||||
|
||||
def create_glossary(args: argparse.Namespace, config: Config) -> None:
|
||||
"""
|
||||
Create a new glossary and write it either to the glossaries directory
|
||||
or the given file.
|
||||
"""
|
||||
# sanity checks
|
||||
if args.name is None:
|
||||
raise GlossaryCmdError("Missing glossary name")
|
||||
if args.source_lang is None:
|
||||
raise GlossaryCmdError("Missing source language")
|
||||
if args.target_lang is None:
|
||||
raise GlossaryCmdError("Missing target language")
|
||||
if config.glossaries is None and args.file is None:
|
||||
raise GlossaryCmdError("Glossaries directory missing in the configuration file")
|
||||
# create file path or use the given one
|
||||
glo_file = Path(args.file) if args.file else get_glossary_file_path(args.name, config)
|
||||
if glo_file.exists():
|
||||
raise GlossaryCmdError(f"Glossary '{glo_file}' already exists")
|
||||
glo = Glossary(name=args.name,
|
||||
source_lang=args.source_lang,
|
||||
target_lang=args.target_lang,
|
||||
desc=args.description,
|
||||
file_path=glo_file)
|
||||
glo.to_file()
|
||||
print(f"Successfully created new glossary '{glo_file}'.")
|
||||
|
||||
|
||||
def glossary_cmd(args: argparse.Namespace, config: Config) -> None:
|
||||
"""
|
||||
Handler for the 'glossary' command.
|
||||
"""
|
||||
try:
|
||||
if args.create:
|
||||
create_glossary(args, config)
|
||||
elif args.list:
|
||||
list_glossaries(args, config)
|
||||
except GlossaryCmdError as err:
|
||||
print(f"Error: {err}")
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user