Coverage for /home/ubuntu/rolling-pin/python/rolling_pin/conform_config.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-11-15 00:43 +0000

1from typing import Dict, List # noqa: F401 

2 

3from pathlib import Path 

4 

5from schematics import Model 

6from schematics.exceptions import ValidationError 

7from schematics.types import ListType, ModelType, StringType 

8 

9Rules = List[Dict[str, str]] 

10# ------------------------------------------------------------------------------ 

11 

12 

13def is_dir(dirpath): 

14 # type: (str) -> None 

15 ''' 

16 Validates whether a given dirpath exists. 

17 

18 Args: 

19 dirpath (str): Directory path. 

20 

21 Raises: 

22 ValidationError: If dirpath is not a directory or does not exist. 

23 ''' 

24 if not Path(dirpath).is_dir(): 

25 msg = f'{dirpath} is not a directory or does not exist.' 

26 raise ValidationError(msg) 

27 

28 

29class ConformConfig(Model): 

30 ''' 

31 A class for validating configurations supplied to ConformETL. 

32 

33 Attributes: 

34 source_rules (Rules): A list of rules for parsing directories. 

35 Default: []. 

36 rename_rules (Rules): A list of rules for renaming source filepath 

37 to target filepaths. Default: []. 

38 group_rules (Rules): A list of rules for grouping files. 

39 Default: []. 

40 line_rules (Rules): A list of rules for peforming line copies and 

41 substitutions on files belonging to a given group. Default: []. 

42 ''' 

43 class SourceRule(Model): 

44 path = StringType(required=True, validators=[is_dir]) # type: StringType 

45 include = StringType(required=False, serialize_when_none=False) # type: StringType 

46 exclude = StringType(required=False, serialize_when_none=False) # type: StringType 

47 

48 class RenameRule(Model): 

49 regex = StringType(required=True) # type: StringType 

50 replace = StringType(required=True) # type: StringType 

51 

52 class GroupRule(Model): 

53 name = StringType(required=True) # type: StringType 

54 regex = StringType(required=True) # type: StringType 

55 

56 class LineRule(Model): 

57 group = StringType(required=True) # type: StringType 

58 include = StringType(required=False, serialize_when_none=False) # type: StringType 

59 exclude = StringType(required=False, serialize_when_none=False) # type: StringType 

60 regex = StringType(required=False) # type: StringType 

61 replace = StringType(required=False) # type: StringType 

62 

63 source_rules = ListType(ModelType(SourceRule), required=True) # type: ListType 

64 rename_rules = ListType(ModelType(RenameRule), required=False) # type: ListType 

65 group_rules = ListType(ModelType(GroupRule), required=False) # type: ListType 

66 line_rules = ListType(ModelType(LineRule), required=False) # type: ListType