Coverage for hdl_registers/register_code_generator.py: 95%

20 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-29 22:03 +0000

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

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

3# 

4# This file is part of the hdl_registers project, a HDL register generator fast enough to be run 

5# in real time. 

6# https://hdl-registers.com 

7# https://gitlab.com/hdl_registers/hdl_registers 

8# -------------------------------------------------------------------------------------------------- 

9 

10# Local folder libraries 

11from .register import Register 

12 

13 

14class RegisterCodeGenerator: 

15 

16 """ 

17 Common functions for generating register code. 

18 """ 

19 

20 @staticmethod 

21 def _iterate_registers(register_objects): 

22 for register_object in register_objects: 

23 if isinstance(register_object, Register): 

24 yield (register_object, None) 

25 else: 

26 for register in register_object.registers: 

27 yield (register, register_object) 

28 

29 @staticmethod 

30 def _comment(comment, indentation=0): 

31 raise NotImplementedError("Should be overloaded in child class") 

32 

33 def _comment_block(self, text, indentation=0): 

34 """ 

35 Create a comment block from a string with newlines. 

36 """ 

37 text_lines = text.split("\n") 

38 

39 # Very common that the last line is empty. An effect of TOML formatting with 

40 # multi-line strings. Remove to make the output look more clean. 

41 if text_lines[-1] == "": 

42 text_lines.pop() 

43 

44 return "".join(self._comment(line, indentation=indentation) for line in text_lines) 

45 

46 @staticmethod 

47 def _to_pascal_case(snake_string): 

48 """ 

49 Returns e.g., my_funny_string -> MyFunnyString 

50 """ 

51 return snake_string.title().replace("_", "")