gitlab.com/thomasboni/go-enry/v2@v2.8.3-0.20220418031202-30b0d7a3de98/python/enry/definitions.py (about) 1 """ 2 Python library calling enry Go implementation trough cFFI (API, out-of-line) and Cgo. 3 """ 4 from typing import List 5 6 from _c_enry import lib 7 from enry.types import Guess 8 from enry.utils import transform_types, transform_types_ret_str_slice 9 10 GetLanguage = transform_types([str, bytes], str)(lib.GetLanguage) 11 GetLanguageByContent = transform_types([str, bytes], Guess)(lib.GetLanguageByContent) 12 GetLanguageByExtension = transform_types([str], Guess)(lib.GetLanguageByExtension) 13 GetLanguageByFilename = transform_types([str], Guess)(lib.GetLanguageByFilename) 14 GetLanguageByModeline = transform_types([bytes], Guess)(lib.GetLanguageByModeline) 15 GetLanguageByShebang = transform_types([bytes], Guess)(lib.GetLanguageByShebang) 16 GetLanguageByEmacsModeline = transform_types([bytes], Guess)(lib.GetLanguageByEmacsModeline) 17 GetLanguageByVimModeline = transform_types([bytes], Guess)(lib.GetLanguageByVimModeline) 18 19 GetLanguages = transform_types_ret_str_slice([str, bytes])(lib.GetLanguages) 20 GetLanguageExtensions = transform_types_ret_str_slice([str])(lib.GetLanguageExtensions) 21 22 GetMimeType = transform_types([str, str], str)(lib.GetMimeType) 23 GetColor = transform_types([str], str)(lib.GetColor) 24 25 IsVendor = transform_types([str], bool)(lib.IsVendor) 26 IsGenerated = transform_types([str, bytes], bool)(lib.IsGenerated) 27 IsBinary = transform_types([bytes], bool)(lib.IsBinary) 28 IsConfiguration = transform_types([str], bool)(lib.IsConfiguration) 29 IsDocumentation = transform_types([str], bool)(lib.IsDocumentation) 30 IsDotFile = transform_types([str], bool)(lib.IsDotFile) 31 IsImage = transform_types([str], bool)(lib.IsImage) 32 33 34 def get_language(filename: str, content: bytes) -> str: 35 """ 36 Return the language of the given file based on the filename and its contents. 37 38 :param filename: name of the file with the extension 39 :param content: array of bytes with the contents of the file (the code) 40 :return: the guessed language 41 """ 42 return GetLanguage(filename, content) 43 44 45 def get_language_by_content(filename: str, content: bytes) -> Guess: 46 """ 47 Return detected language by its content. 48 If there are more than one possible language, return the first language 49 in alphabetical order and safe = False. 50 51 :param filename: path of the file 52 :param content: array of bytes with the contents of the file (the code) 53 :return: guessed result 54 """ 55 return GetLanguageByContent(filename, content) 56 57 58 def get_language_by_extension(filename: str) -> Guess: 59 """ 60 Return detected language by the extension of the filename. 61 If there are more than one possible language return the first language 62 in alphabetical order and safe = False. 63 64 :param filename: path of the file 65 :return: guessed result 66 """ 67 return GetLanguageByExtension(filename) 68 69 70 def get_language_by_filename(filename: str) -> Guess: 71 """ 72 Return detected language by its filename. 73 If there are more than one possible language return the first language 74 in alphabetical order and safe = False. 75 76 :param filename: path of the file 77 :return: guessed result 78 """ 79 return GetLanguageByFilename(filename) 80 81 82 def get_language_by_modeline(content: bytes) -> Guess: 83 """ 84 Return detected language by its modeline. 85 If there are more than one possible language return the first language 86 in alphabetical order and safe = False. 87 88 :param content: array of bytes with the contents of the file (the code) 89 :return: guessed result 90 """ 91 return GetLanguageByModeline(content) 92 93 94 def get_language_by_vim_modeline(content: bytes) -> Guess: 95 """ 96 Return detected language by its vim modeline. 97 If there are more than one possible language return the first language 98 in alphabetical order and safe = False. 99 100 :param content: array of bytes with the contents of the file (the code) 101 :return: guessed result 102 """ 103 return GetLanguageByVimModeline(content) 104 105 106 def get_language_by_emacs_modeline(content: bytes) -> Guess: 107 """ 108 Return detected langauge by its emacs modeline. 109 If there are more than one possible language return the first language 110 in alphabetical order and safe = False. 111 112 :param content: array of bytes with the contents of the file (the code) 113 :return: guessed result 114 """ 115 return GetLanguageByEmacsModeline(content) 116 117 118 def get_language_by_shebang(content: bytes) -> Guess: 119 """ 120 Return detected langauge by its shebang. 121 If there are more than one possible language return the first language 122 in alphabetical order and safe = False. 123 124 :param content: array of bytes with the contents of the file (the code) 125 :return: guessed result 126 """ 127 return GetLanguageByShebang(content) 128 129 130 def get_languages(filename: str, content: bytes) -> List[str]: 131 """ 132 Return all possible languages for the given file. 133 134 :param filename: 135 :param content: array of bytes with the contents of the file (the code) 136 :return: all possible languages 137 """ 138 return GetLanguages(filename, content) 139 140 141 def get_language_extensions(language: str) -> List[str]: 142 """ 143 Return all the possible extensions for the given language. 144 145 :param language: language to get extensions from 146 :return: extensions for given language 147 """ 148 return GetLanguageExtensions(language) 149 150 151 def get_mime_type(path: str, language: str) -> str: 152 """ 153 Return mime type of the file. 154 155 :param path: path of the file 156 :param language: language to get mime type from 157 :return: mime type 158 """ 159 return GetMimeType(path, language) 160 161 162 def get_color(language: str) -> str: 163 """ 164 Return color code for given language 165 166 :param language: 167 :return: color in hex format 168 """ 169 return GetColor(language) 170 171 172 def is_vendor(filename: str) -> bool: 173 """ 174 Return True if given file is a vendor file. 175 176 :param filename: path of the file 177 :return: whether it's vendor or not 178 """ 179 return IsVendor(filename) 180 181 182 def is_generated(filename: str, content: bytes) -> bool: 183 """ 184 Return True if given file is a generated file. 185 186 :param filename: path of the file 187 :param content: array of bytes with the contents of the file (the code) 188 :return: whether it's generated or not 189 """ 190 return IsGenerated(filename, content) 191 192 193 def is_binary(content: bytes) -> bool: 194 """ 195 Return True if given file is a binary file. 196 197 :param content: array of bytes with the contents of the file (the code) 198 :return: whether it's binary or not 199 """ 200 return IsBinary(content) 201 202 203 def is_configuration(path: str) -> bool: 204 """ 205 Return True if given file is a configuration file. 206 207 :param path: path of the file 208 :return: whether it's a configuration file or not 209 """ 210 return IsConfiguration(path) 211 212 213 def is_documentation(path: str) -> bool: 214 """ 215 Return True if given file is a documentation file. 216 217 :param path: path of the file 218 :return: whether it's documentation or not 219 """ 220 return IsDocumentation(path) 221 222 223 def is_dot_file(path: str) -> bool: 224 """ 225 Return True if given file is a dot file. 226 227 :param path: path of the file 228 :return: whether it's a dot file or not 229 """ 230 return IsDotFile(path) 231 232 233 def is_image(path: str) -> bool: 234 """ 235 Return True if given file is an image file. 236 237 :param path: path of the file 238 :return: whether it's an image or not 239 """ 240 return IsImage(path)