Skip to content

Commit 27c953a

Browse files
committed
Add function to check if a path is a source file
Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 2500ea9 commit 27c953a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

codebasin/source.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (C) 2019-2024 Intel Corporation
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
import os
5+
from pathlib import Path
6+
from typing import Union
7+
8+
9+
def is_source_file(filename: Union[str, os.PathLike]) -> bool:
10+
"""
11+
Parameters
12+
----------
13+
filename: Union[str, os.Pathlike]
14+
The filename of a potential source file.
15+
16+
Returns
17+
-------
18+
bool
19+
True if the file ends in a recognized extension and False otherwise.
20+
Only files that can be parsed correctly have recognized extensions.
21+
22+
Raises
23+
------
24+
TypeError
25+
If filename is not a string or Path.
26+
"""
27+
if not (isinstance(filename, str) or isinstance(filename, Path)):
28+
raise TypeError("filename must be a string or Path")
29+
30+
extension = Path(filename).suffix
31+
supported_extensions = [
32+
".f90",
33+
".F90",
34+
".f",
35+
".ftn",
36+
".fpp",
37+
".F",
38+
".FOR",
39+
".FTN",
40+
".FPP",
41+
".c",
42+
".h",
43+
".c++",
44+
".cxx",
45+
".cpp",
46+
".cc",
47+
".hpp",
48+
".hxx",
49+
".h++",
50+
".hh",
51+
".inc",
52+
".inl",
53+
".tcc",
54+
".icc",
55+
".ipp",
56+
".cu",
57+
".cuh",
58+
".cl",
59+
".s",
60+
".S",
61+
".asm",
62+
]
63+
return extension in supported_extensions

0 commit comments

Comments
 (0)