Coverage for tsfpga/module_list.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 20:01 +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://gitlab.com/tsfpga/tsfpga 

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

8 

9# Standard libraries 

10import copy 

11 

12 

13class ModuleList: 

14 """ 

15 Wrapper for a list of modules, with convenience functions. 

16 """ 

17 

18 def __init__(self): 

19 self._modules = [] 

20 

21 def append(self, module): 

22 """ 

23 Append a module to the list. 

24 """ 

25 self._modules.append(module) 

26 

27 def get(self, module_name): 

28 """ 

29 Get the module with the specified name. If no module matched, an exception is raised. 

30 """ 

31 for module in self._modules: 

32 if module.name == module_name: 

33 return module 

34 

35 raise ValueError(f'No module "{module_name}" available') 

36 

37 def __iter__(self): 

38 return iter(self._modules) 

39 

40 def __getitem__(self, index): 

41 return self._modules[index] 

42 

43 def __len__(self): 

44 return len(self._modules) 

45 

46 def __add__(self, other): 

47 if not isinstance(other, self.__class__): 

48 raise TypeError(f"Can only concatenate with another {self.__class__.__name__}") 

49 

50 # Note that the list concatenation implies a shallow copy of the lists 

51 result = self.__class__() 

52 result._modules = self._modules + other._modules 

53 return result 

54 

55 def __copy__(self): 

56 result = self.__class__() 

57 result._modules = self._modules.copy() 

58 return result 

59 

60 def copy(self): 

61 """ 

62 Create a shallow copy of the module list. This public function is available as a 

63 convenience and to mimic the interface of a regular python list. 

64 """ 

65 return copy.copy(self) 

66 

67 def __str__(self): 

68 return str(self._modules)