Skip to content

Commit

Permalink
[add] finshing up the imports sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Animenosekai committed Sep 23, 2023
1 parent 749e9b6 commit 1cafa6a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
3 changes: 2 additions & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def build_binary(output: typing.Optional[str] = None, platform: typing.Optional[
"--python-flag=isolated,nosite,-O",
"--plugin-enable=anti-bloat,implicit-imports,data-files,pylint-warnings",
"--warn-implicit-exceptions", "--warn-unusual-code", "--prefer-source-code",
"--static-libpython=yes", output or "translatepy"
# "--static-libpython=yes",
output or "translatepy"
])
elif platform == "win32":
# logger.log("Building binaries for Windows")
Expand Down
44 changes: 33 additions & 11 deletions translatepy/cli/sdk/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,63 @@
translatepy's Imports Software Development Kit
"""
import argparse
import json
import sys
import typing
import dataclasses
import json

import cain

import translatepy
from translatepy import logger
from translatepy.__info__ import __repository__
from translatepy.utils import vectorize, importer
from translatepy.utils import importer, vectorize


def add(name: str, path: str, translate: bool = False, forceload: bool = False):
"""Adds a translator to the database"""
name = str(name)

importer.get_translator_from_path(path, forceload=forceload)
# Verify that the path exists
importer.translator_from_path(path, forceload=forceload)

names = [name]
if translate:
logger.debug(f"Translating `{name}`")
for lang in []:
try:
result = translatepy.translate(name, lang)
names.append(result.translation)
except Exception:
pass

logger.debug("Vectorizing the new names")
for name in names:
importer.IMPORTER_VECTORS.append(
vectorize.vectorize(path, name)
)
with (importer.IMPORTER_DATA_DIR / "translators").open("wb") as f:

logger.debug("Dumping the vectors to the translators DB")
with (importer.IMPORTER_DATA_FILE).open("wb") as f:
cain.dump(importer.IMPORTER_VECTORS, f, typing.List[vectorize.Vector])


def remove(name: str):
"""Removes a translator from the database"""
vectors = [vector for vector in importer.IMPORTER_VECTORS if vector.string != name]
with (importer.IMPORTER_DATA_DIR / "translators").open("wb") as f:
def remove(name: str, all: bool = False):
"""Removes a translator name variant from the database"""
if all:
vectors = [vector for vector in importer.IMPORTER_VECTORS if vector.id != name]
else:
name = vectorize.string_preprocessing(name)
vectors = [vector for vector in importer.IMPORTER_VECTORS if vector.string != name]
with (importer.IMPORTER_DATA_FILE).open("wb") as f:
cain.dump(vectors, f, typing.List[vectorize.Vector])


def variants(translator: str):
"""Returns all of the name variants available in the database for the given translator ID"""
vectors = [vector for vector in importer.IMPORTER_VECTORS if vector.id == translator]
print(json.dumps([vector.string for vector in vectors], ensure_ascii=False, indent=4))


def search(query: str, limit: int = 10):
"""Searches for the given `query` in the database"""
results = sorted(vectorize.search(query, importer.IMPORTER_VECTORS), key=lambda element: element.similarity, reverse=True)
Expand All @@ -71,7 +86,11 @@ def prepare_argparse(parser: argparse.ArgumentParser):
imports_add_parser.add_argument("--forceload", action="store_true", help="Enables pydoc's `forceload` while searching for the translator")

imports_remove_parser = imports_subparsers.add_parser("remove", help=remove.__doc__)
imports_remove_parser.add_argument("name", help="The translator name")
imports_remove_parser.add_argument("name", help="The translator name variant or id if `--all` is set")
imports_remove_parser.add_argument("--all", action="store_true", help="Treats `name` as the translator ID and removes every variants associated with the translator.")

imports_remove_parser = imports_subparsers.add_parser("variants", help=variants.__doc__)
imports_remove_parser.add_argument("id", help="The translator ID")

imports_search_parser = imports_subparsers.add_parser("search", help=search.__doc__)
imports_search_parser.add_argument("query", help="The search query")
Expand All @@ -88,7 +107,10 @@ def entry(args: argparse.Namespace):
add(name=args.name, path=args.path, translate=args.translate, forceload=args.forceload)

if args.imports_action in ("remove",):
remove(name=args.name)
remove(name=args.name, all=args.all)

if args.imports_action in ("variants",):
variants(translator=args.id)

if args.imports_action in ("search",):
search(query=args.query, limit=args.limit)
Expand Down
Binary file modified translatepy/data/translators/vectors.cain
Binary file not shown.
12 changes: 7 additions & 5 deletions translatepy/utils/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
IMPORTER_CACHE = lru.LRUDictCache(512)

IMPORTER_DATA_DIR = pathlib.Path(__file__).parent.parent / "data" / "translators"
with open(IMPORTER_DATA_DIR / "vectors.cain", "b+r") as f:
IMPORTER_DATA_FILE = IMPORTER_DATA_DIR / "vectors.cain"
with open(IMPORTER_DATA_FILE, "b+r") as f:
IMPORTER_VECTORS = cain.load(f, typing.List[vectorize.Vector])


Expand All @@ -41,14 +42,15 @@ def translator_from_name(name: str) -> typing.Type[BaseTranslator]:
raise ValueError(f"Couldn't get the translator {name}") from err


def get_translator_from_path(path: str, forceload: bool = False) -> typing.Type[BaseTranslator]:
def translator_from_path(path: str, forceload: bool = False) -> typing.Type[BaseTranslator]:
"""Returns a translator from its dot path"""
result = pydoc.locate(path, forceload=forceload)
try:
if not issubclass(result, BaseTranslator):
raise ImportError
raise ImportError(f"Couldn't import translator `{path}`")
except TypeError:
if not isinstance(result, BaseTranslator):
raise ImportError
raise ImportError(f"Couldn't import translator `{path}`")
result = result.__class__
return result

Expand All @@ -67,7 +69,7 @@ def get_translator(query: str,
return cache_result

try:
get_translator_from_path(query, forceload=forceload)
translator_from_path(query, forceload=forceload)
except ImportError: # this also catches ErrorDuringImport
pass

Expand Down
1 change: 1 addition & 0 deletions translatepy/utils/vectorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def set(self):
"""Returns a set for the vector"""
return set(self.string)

A = list[Vector]

def vectorize(id: str, string: str):
"""Vectorizes the given string"""
Expand Down

0 comments on commit 1cafa6a

Please sign in to comment.