Coverage for src/models/site.py: 100%

41 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2024-04-27 07:17 -0500

1import argparse 

2import datetime 

3import os 

4import platform 

5import sys 

6 

7 

8class Site: 

9 """ 

10 Website model. 

11 """ 

12 

13 def __init__(self, timestamp=None, entries=[], **kwargs): # noqa: E501 

14 """ 

15 Build a Site model. 

16 

17 Customize with the following kwargs: 

18 

19 - `title` 

20 - `description` 

21 - `author` 

22 - `email` 

23 - `domain` 

24 - `protocol` 

25 """ 

26 

27 fields = ['title', 'description', 'author', 

28 'email', 'domain', 'protocol'] 

29 

30 for key in fields: 

31 if value := kwargs.get(key): 

32 setattr(self, '_' + key, value) 

33 

34 # timestamp 

35 self._timestamp = timestamp 

36 

37 @property 

38 def title(self) -> str: 

39 """ 

40 Website title (ex `"Blog"`) 

41 """ 

42 return self._title 

43 

44 @property 

45 def description(self) -> str: 

46 """ 

47 Website description (ex `"A Place for my Thoughts"`) 

48 """ 

49 return self._description 

50 

51 @property 

52 def author(self) -> str: 

53 """ 

54 Website maintainer's full name. 

55 """ 

56 return self._author 

57 

58 @property 

59 def email(self) -> str: 

60 """ 

61 Website maintainer's email. 

62 """ 

63 return self._email 

64 

65 @property 

66 def url(self) -> str: 

67 """ 

68 Full website URL (ex. `"https://www.alexrecker.com"`) 

69 """ 

70 return f'{self._protocol}://{self._domain}' 

71 

72 @property 

73 def timestamp(self) -> datetime.datetime: 

74 """ 

75 Website build timestamp. 

76 """ 

77 return self._timestamp 

78 

79 @property 

80 def python_version(self) -> str: 

81 """ 

82 The python version used to build the website. (ex. `"v3.11.0"`) 

83 """ 

84 return f'v{platform.python_version()}' 

85 

86 @property 

87 def python_executable(self) -> str: 

88 """ 

89 Path to `python` executable used to build the site 

90 (ex. `"/usr/bin/python"`) 

91 """ 

92 return sys.executable 

93 

94 

95def load_site(args: argparse.Namespace) -> Site: 

96 """ 

97 Creates a `Site` from the results of `parser.parse_args()`. 

98 

99 ```python 

100 args = parser.parse_args() 

101 site = src.load_site(args) 

102 ``` 

103 

104 Note: the timezone is hard-coded to `"America/Chicago"` 

105 (because nobody ever brags about the beef sandwich they had in 

106 Greenwich). 

107 """ 

108 # set timestamp 

109 os.environ['TZ'] = 'America/Chicago' 

110 timestamp = datetime.datetime.now() 

111 

112 site_args = {k[5:]: v for k, v in vars( 

113 args).items() if k.startswith('site_')} 

114 

115 return Site(timestamp=timestamp, **site_args)