python: expose language_by_filename()

Signed-off-by: Alexander Bezzubov <bzz@apache.org>
This commit is contained in:
Alexander Bezzubov 2019-06-01 21:19:55 +02:00
parent ee7a0f1139
commit cff9c07009
No known key found for this signature in database
GPG Key ID: 8039F5787EFCD05D
2 changed files with 23 additions and 1 deletions

View File

@ -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()

View File

@ -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