MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / is_datafile

Method is_datafile

src/packagedcode/models.py:1085–1120  ·  view source on GitHub ↗

Return True if the file at ``location`` is likely a package data file that this parser can handle. This implementation is based on: - matching the ``location`` as a whole with any one of the ``path_patterns`` sequence of patterns defined as a class attributes.

(cls, location, filetypes=tuple(), _bare_filename=False)

Source from the content-addressed store, hash-verified

1083
1084 @classmethod
1085 def is_datafile(cls, location, filetypes=tuple(), _bare_filename=False):
1086 """
1087 Return True if the file at ``location`` is likely a package data file
1088 that this parser can handle. This implementation is based on:
1089
1090 - matching the ``location`` as a whole with any one of the
1091 ``path_patterns`` sequence of patterns defined as a class attributes.
1092 The path patterns are for POSIX paths.
1093
1094 - if defined, ensuring that the filetype of the file at ``location``
1095 contains any of the type listed in the ``filetypes`` class attribute.
1096
1097 - ``_bare_filename`` is for testing using a bare path that does not
1098 point to real files.
1099 Subclasses can override to implement more complex data file recognition.
1100 """
1101 if filetype.is_file(location) or _bare_filename:
1102 loc = as_posixpath(location)
1103
1104 # Some extension strings are used interchangebly
1105 extension_aliases = {"yaml": "yml"}
1106 path_patterns = list(cls.path_patterns)
1107 for pattern in cls.path_patterns:
1108 for extension, extension_alias in extension_aliases.items():
1109 new_pattern = pattern.replace(extension, extension_alias)
1110 path_patterns.append(new_pattern)
1111
1112 if any(fnmatchcase(loc, pat) for pat in path_patterns):
1113 filetypes = filetypes or cls.filetypes
1114 if not filetypes:
1115 return True
1116 # we check for contenttype IFF this is available
1117 if contenttype:
1118 T = contenttype.get_type(location)
1119 actual_type = T.filetype_file.lower()
1120 return any(ft in actual_type for ft in filetypes)
1121
1122 @classmethod
1123 def parse(cls, location, package_only=False):

Callers

nothing calls this directly

Calls 4

as_posixpathFunction · 0.90
get_typeMethod · 0.80
replaceMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected