Squashed 'go-enry/' content from commit 7e3a9a7

git-subtree-dir: go-enry
git-subtree-split: 7e3a9a7241
This commit is contained in:
2024-09-04 16:33:41 -03:00
commit f955c625ad
192 changed files with 528500 additions and 0 deletions

26
python/enry/__init__.py Normal file
View File

@ -0,0 +1,26 @@
from enry.definitions import get_color, get_language, get_language_by_content, get_language_by_emacs_modeline, \
get_language_by_extension, get_language_by_filename, get_language_by_modeline, get_language_by_shebang, \
get_language_by_vim_modeline, get_languages, get_mime_type, is_binary, is_configuration, is_documentation, \
is_dot_file, is_generated, is_image, is_vendor, get_language_extensions
__all__ = [
"get_color",
"get_language",
"get_language_extensions",
"get_languages",
"get_mime_type",
"get_language_by_vim_modeline",
"get_language_by_extension",
"get_language_by_content",
"get_language_by_emacs_modeline",
"get_language_by_modeline",
"get_language_by_filename",
"get_language_by_shebang",
"is_vendor",
"is_binary",
"is_image",
"is_generated",
"is_documentation",
"is_dot_file",
"is_configuration",
]

240
python/enry/definitions.py Normal file
View File

@ -0,0 +1,240 @@
"""
Python library calling enry Go implementation trough cFFI (API, out-of-line) and Cgo.
"""
from typing import List
from _c_enry import lib
from enry.types import Guess
from enry.utils import transform_types, transform_types_ret_str_slice
GetLanguage = transform_types([str, bytes], str)(lib.GetLanguage)
GetLanguageByContent = transform_types([str, bytes], Guess)(lib.GetLanguageByContent)
GetLanguageByExtension = transform_types([str], Guess)(lib.GetLanguageByExtension)
GetLanguageByFilename = transform_types([str], Guess)(lib.GetLanguageByFilename)
GetLanguageByModeline = transform_types([bytes], Guess)(lib.GetLanguageByModeline)
GetLanguageByShebang = transform_types([bytes], Guess)(lib.GetLanguageByShebang)
GetLanguageByEmacsModeline = transform_types([bytes], Guess)(lib.GetLanguageByEmacsModeline)
GetLanguageByVimModeline = transform_types([bytes], Guess)(lib.GetLanguageByVimModeline)
GetLanguages = transform_types_ret_str_slice([str, bytes])(lib.GetLanguages)
GetLanguageExtensions = transform_types_ret_str_slice([str])(lib.GetLanguageExtensions)
GetMimeType = transform_types([str, str], str)(lib.GetMimeType)
GetColor = transform_types([str], str)(lib.GetColor)
IsVendor = transform_types([str], bool)(lib.IsVendor)
IsGenerated = transform_types([str, bytes], bool)(lib.IsGenerated)
IsBinary = transform_types([bytes], bool)(lib.IsBinary)
IsConfiguration = transform_types([str], bool)(lib.IsConfiguration)
IsDocumentation = transform_types([str], bool)(lib.IsDocumentation)
IsDotFile = transform_types([str], bool)(lib.IsDotFile)
IsImage = transform_types([str], bool)(lib.IsImage)
def get_language(filename: str, content: bytes) -> str:
"""
Return the language of the given file based on the filename and its contents.
:param filename: name of the file with the extension
:param content: array of bytes with the contents of the file (the code)
:return: the guessed language
"""
return GetLanguage(filename, content)
def get_language_by_content(filename: str, content: bytes) -> Guess:
"""
Return detected language by its content.
If there are more than one possible language, return the first language
in alphabetical order and safe = False.
:param filename: path of the file
:param content: array of bytes with the contents of the file (the code)
:return: guessed result
"""
return GetLanguageByContent(filename, content)
def get_language_by_extension(filename: str) -> Guess:
"""
Return detected language by the extension of the filename.
If there are more than one possible language return the first language
in alphabetical order and safe = False.
:param filename: path of the file
:return: guessed result
"""
return GetLanguageByExtension(filename)
def get_language_by_filename(filename: str) -> Guess:
"""
Return detected language by its filename.
If there are more than one possible language return the first language
in alphabetical order and safe = False.
:param filename: path of the file
:return: guessed result
"""
return GetLanguageByFilename(filename)
def get_language_by_modeline(content: bytes) -> Guess:
"""
Return detected language by its modeline.
If there are more than one possible language return the first language
in alphabetical order and safe = False.
:param content: array of bytes with the contents of the file (the code)
:return: guessed result
"""
return GetLanguageByModeline(content)
def get_language_by_vim_modeline(content: bytes) -> Guess:
"""
Return detected language by its vim modeline.
If there are more than one possible language return the first language
in alphabetical order and safe = False.
:param content: array of bytes with the contents of the file (the code)
:return: guessed result
"""
return GetLanguageByVimModeline(content)
def get_language_by_emacs_modeline(content: bytes) -> Guess:
"""
Return detected langauge by its emacs modeline.
If there are more than one possible language return the first language
in alphabetical order and safe = False.
:param content: array of bytes with the contents of the file (the code)
:return: guessed result
"""
return GetLanguageByEmacsModeline(content)
def get_language_by_shebang(content: bytes) -> Guess:
"""
Return detected langauge by its shebang.
If there are more than one possible language return the first language
in alphabetical order and safe = False.
:param content: array of bytes with the contents of the file (the code)
:return: guessed result
"""
return GetLanguageByShebang(content)
def get_languages(filename: str, content: bytes) -> List[str]:
"""
Return all possible languages for the given file.
:param filename:
:param content: array of bytes with the contents of the file (the code)
:return: all possible languages
"""
return GetLanguages(filename, content)
def get_language_extensions(language: str) -> List[str]:
"""
Return all the possible extensions for the given language.
:param language: language to get extensions from
:return: extensions for given language
"""
return GetLanguageExtensions(language)
def get_mime_type(path: str, language: str) -> str:
"""
Return mime type of the file.
:param path: path of the file
:param language: language to get mime type from
:return: mime type
"""
return GetMimeType(path, language)
def get_color(language: str) -> str:
"""
Return color code for given language
:param language:
:return: color in hex format
"""
return GetColor(language)
def is_vendor(filename: str) -> bool:
"""
Return True if given file is a vendor file.
:param filename: path of the file
:return: whether it's vendor or not
"""
return IsVendor(filename)
def is_generated(filename: str, content: bytes) -> bool:
"""
Return True if given file is a generated file.
:param filename: path of the file
:param content: array of bytes with the contents of the file (the code)
:return: whether it's generated or not
"""
return IsGenerated(filename, content)
def is_binary(content: bytes) -> bool:
"""
Return True if given file is a binary file.
:param content: array of bytes with the contents of the file (the code)
:return: whether it's binary or not
"""
return IsBinary(content)
def is_configuration(path: str) -> bool:
"""
Return True if given file is a configuration file.
:param path: path of the file
:return: whether it's a configuration file or not
"""
return IsConfiguration(path)
def is_documentation(path: str) -> bool:
"""
Return True if given file is a documentation file.
:param path: path of the file
:return: whether it's documentation or not
"""
return IsDocumentation(path)
def is_dot_file(path: str) -> bool:
"""
Return True if given file is a dot file.
:param path: path of the file
:return: whether it's a dot file or not
"""
return IsDotFile(path)
def is_image(path: str) -> bool:
"""
Return True if given file is an image file.
:param path: path of the file
:return: whether it's an image or not
"""
return IsImage(path)

6
python/enry/types.py Normal file
View File

@ -0,0 +1,6 @@
from typing import NamedTuple
class Guess(NamedTuple):
language: str
safe: bool

77
python/enry/utils.py Normal file
View File

@ -0,0 +1,77 @@
from _c_enry import ffi
from enry.types import Guess
from functools import wraps
from typing import Hashable, List, Sequence
def py_bytes_to_go(py_bytes: bytes):
c_bytes = ffi.new("char[]", py_bytes)
go_slice = ffi.new("GoSlice *", [c_bytes, len(py_bytes), len(py_bytes)])
return (go_slice[0], c_bytes)
def py_str_to_go(py_str: str):
str_bytes = py_str.encode()
c_str = ffi.new("char[]", str_bytes)
go_str = ffi.new("_GoString_ *", [c_str, len(str_bytes)])
return (go_str[0], c_str)
def go_str_to_py(go_str: str):
str_len = go_str.n
if str_len > 0:
return ffi.unpack(go_str.p, go_str.n).decode()
return ""
def init_go_slice():
return ffi.new("GoSlice *")
def go_str_slice_to_py(str_slice) -> List[str]:
slice_len = str_slice.len
char_arr = ffi.cast("char **", str_slice.data)
return [ffi.string(char_arr[i]).decode() for i in range(slice_len)]
def go_bool_to_py(go_bool: bool):
return go_bool == 1
def go_guess_to_py(guess) -> Guess:
return Guess(go_str_to_py(guess.r0), go_bool_to_py(guess.r1))
py_to_go = {
str: py_str_to_go,
bytes: py_bytes_to_go,
}
go_to_py = {
str: go_str_to_py,
bool: go_bool_to_py,
Guess: go_guess_to_py,
}
def transform_types(in_types: Sequence[Hashable], out_type: Hashable):
def decorator(fn):
@wraps(fn)
def inner(*args):
args_transformed = [py_to_go[type_](arg) for type_, arg in zip(in_types, args)]
return go_to_py[out_type](fn(*(arg[0] for arg in args_transformed)))
return inner
return decorator
def transform_types_ret_str_slice(in_types: Sequence[Hashable]):
def decorator(fn):
@wraps(fn)
def inner(*args):
ret_slice = init_go_slice()
args_transformed = [py_to_go[type_](arg) for type_, arg in zip(in_types, args)]
fn(*(arg[0] for arg in args_transformed), ret_slice)
return go_str_slice_to_py(ret_slice)
return inner
return decorator