From cff9c07009bf4036bf940a046f521a6024602775 Mon Sep 17 00:00:00 2001 From: Alexander Bezzubov Date: Sat, 1 Jun 2019 21:19:55 +0200 Subject: [PATCH] python: expose language_by_filename() Signed-off-by: Alexander Bezzubov --- python/enry.py | 16 +++++++++++++++- python/enry_build.py | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/python/enry.py b/python/enry.py index 0905e9e..77830f8 100644 --- a/python/enry.py +++ b/python/enry.py @@ -26,15 +26,29 @@ def language_by_extension(filename: str) -> str: return lang +def language_by_filename(filename: str) -> str: + fName = py_str_to_go(filename) + guess = lib.GetLanguageByFilename(fName) + lang = go_str_to_py(guess.r0) + return lang + + ## Test def main(): - files = ["Parse.hs", "some.cpp", "and.go", "type.h"] + files = ["Parse.hs", "some.cpp", "and.go", "type.h", ".bashrc"] + + print("strategy: extension") for filename in files: lang = language_by_extension(filename) print("file: {:10s} language: '{}'".format(filename, lang)) + print("\nstrategy: filename") + for filename in files: + lang = language_by_filename(filename) + print("file: {:10s} language: '{}'".format(filename, lang)) + if __name__ == "__main__": main() diff --git a/python/enry_build.py b/python/enry_build.py index 637f8fe..8e5d776 100644 --- a/python/enry_build.py +++ b/python/enry_build.py @@ -15,6 +15,14 @@ struct GetLanguageByExtension_return { }; extern struct GetLanguageByExtension_return GetLanguageByExtension(GoString p0); + +/* Return type for GetLanguageByFilename */ +struct GetLanguageByFilename_return { + GoString r0; /* language */ + GoUint8 r1; /* safe */ +}; + +extern struct GetLanguageByFilename_return GetLanguageByFilename(GoString p0); """) # set_source() gives the name of the python extension module to