File: fixture.py

package info (click to toggle)
mkdocs-macros-plugin 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 372 kB
  • sloc: python: 1,336; makefile: 4
file content (141 lines) | stat: -rw-r--r-- 4,938 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Specific for MkDocs Projects

(C) Laurent Franceschetti 2024
"""

import os
import warnings
import json
import subprocess
from typing import Dict


from super_collections import SuperDict
from mkdocs_test import DocProject, MkDocsPage
import os

class MacrosPage(MkDocsPage):
    "Specific for MkDocs-Macros"

    def has_error(self:MkDocsPage):
            "Predicate: check whether the page has an error"
            return self.find_text('Macro Rendering Error')
    
    @property
    def is_rendered(self):
        "Accomodate earlier formulation"
        warnings.warn("The page property `.is_rendered` is DEPRECATED "
                      "use `.is_markdown_rendered()` instead.", 
                      UserWarning, stacklevel=2)
        return self.is_markdown_rendered()

class MacrosDocProject(DocProject):
     "Specific for MkDocs-Macros"

     def build(self, strict:bool=False) -> subprocess.CompletedProcess:
          """
          Build the documentation, to perform the tests
          Verbose is forced to True, to get the variables, functions and filters
          """
          super().build(strict=strict, verbose=True)

     @property
     def pages(self) -> Dict[str, MacrosPage]:
          "List of pages"
          pages = super().pages
          return {key: MacrosPage(value) for key, value in pages.items()}
     

     
     @property
     def macros_plugin(self):
          "Information on the plugin"
          return self.get_plugin('macros')
     
     # ------------------------------------
     # Get information through the payload
     # ------------------------------------
     @property
     def variables(self):
               "Return the variables"
               try:
                    return self._variables
               except AttributeError:
                    print("ENTRIES:", self.find_entries("config variables",
                                                  source='',
                                                  severity='debug'))
                    print("ENTRIES:", self.find_entries("config variables",
                                                  source='macros'))
                    entry = self.find_entry("config variables",
                                                  source='macros',
                                                  severity='debug')
                    if entry and entry.payload:
                         payload = json.loads(entry.payload)
                         self._variables = SuperDict(payload)
                    else:
                         # print(entry)
                         # raise ValueError("Cannot find variables")
                         self._variables = {}
                    return self._variables


     @property
     def macros(self):
          "Return the macros"
          try:
               return self._macros
          except AttributeError:
               entry = self.find_entry("config macros",
                                        source='macros',
                                        severity='debug')
               if entry and entry.payload:
                    self._macros = SuperDict(json.loads(entry.payload))
               else:
                    # print(entry)
                    # raise ValueError("Cannot find macros")
                    self._macros = {}
               return self._macros
          

     @property
     def filters(self):
          "Return the filters"
          try:
               return self._filters
          except AttributeError:
               entry = self.find_entry("config filters",
                                             source='macros',
                                             severity='debug')
               if entry and entry.payload:
                    self._filters = SuperDict(json.loads(entry.payload))
               else:
                    #   print(entry)
                    #   raise ValueError("Cannot find filters")
                    self._filters = {}
               return self._filters
     
     # ------------------------------------
     # Special operations
     # ------------------------------------

     def add_file(self, pathname:str, content, binary=False) -> str:
          """
          Stores a file (any type) into the directory structure,
          relative to project directory.

          If you want to add a source page (markdown), prefer the
          add_source_page() method.

          Return the full pathname.
          """
          full_pathname = os.path.join(self.project_dir, pathname)
          # Ensure parent directory exists
          os.makedirs(os.path.dirname(full_pathname), exist_ok=True)
          if binary:
               with open(full_pathname, 'wb') as f:
                    f.write(content)
          else:
               with open(full_pathname, 'w', encoding="utf-8") as f:
                    f.write(content)
          return full_pathname