Coverage for tsfpga/hdl_file.py: 96%

28 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-20 20:51 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project, a project platform for modern FPGA development. 

5# https://tsfpga.com 

6# https://github.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9# Standard libraries 

10from enum import Enum, auto 

11from pathlib import Path 

12 

13 

14class HdlFile: 

15 """ 

16 Class for representing a HDL source code file in the file system. 

17 """ 

18 

19 class Type(Enum): 

20 """ 

21 Enumeration of supported HDL file types. 

22 """ 

23 

24 VHDL = auto() 

25 VERILOG_SOURCE = auto() 

26 VERILOG_HEADER = auto() 

27 SYSTEMVERILOG_SOURCE = auto() 

28 SYSTEMVERILOG_HEADER = auto() 

29 

30 # Decides which file endings are associated with which file type. 

31 file_endings_mapping = { 

32 Type.VHDL: (".vhd", ".vhdl"), 

33 Type.VERILOG_SOURCE: (".v",), 

34 Type.VERILOG_HEADER: (".vh",), 

35 Type.SYSTEMVERILOG_SOURCE: (".sv",), 

36 Type.SYSTEMVERILOG_HEADER: (".svh",), 

37 } 

38 

39 # A tuple of all supported HDL file endings. 

40 file_endings = tuple( 

41 file_ending 

42 for type_file_endings in file_endings_mapping.values() 

43 for file_ending in type_file_endings 

44 ) 

45 

46 def __init__(self, path: Path) -> None: 

47 """ 

48 Arguments: 

49 path: Path to a HDL source code file. 

50 """ 

51 self._path = path 

52 

53 for file_type, file_endings in self.file_endings_mapping.items(): 

54 if path.name.endswith(file_endings): 

55 self._type = file_type 

56 break 

57 else: 

58 raise ValueError(f"Unsupported HDL file ending: {path}") 

59 

60 @property 

61 def path(self) -> Path: 

62 """ 

63 Path to the HDL file. 

64 Getter for read-only class variable. 

65 """ 

66 return self._path 

67 

68 @property 

69 def type(self) -> Type: 

70 """ 

71 The file type of the HDL file. 

72 Getter for read-only class variable. 

73 """ 

74 return self._type 

75 

76 def __str__(self) -> str: 

77 return f"{self.__class__.__name__}('{self._path}', '{self._type}')" 

78 

79 def __repr__(self) -> str: 

80 return str(self)