Manages document content, including parsing, updating, rewriting, and retrieving document content.
| 5 | |
| 6 | |
| 7 | class Documents: |
| 8 | """ |
| 9 | Manages document content, including parsing, updating, rewriting, and retrieving document content. |
| 10 | """ |
| 11 | |
| 12 | def __init__(self, generated_content="", parse=True, predefined_filename=None): |
| 13 | """ |
| 14 | Initializes the Documents class, parsing the generated document content and storing it in docbooks. |
| 15 | Args: |
| 16 | generated_content: Generated document content (string format) |
| 17 | parse: Whether to parse document content (default is True) |
| 18 | predefined_filename: Predefined filename (used if not parsing) |
| 19 | """ |
| 20 | self.directory: str = None # Directory to store documents |
| 21 | self.generated_content = generated_content # Generated document content |
| 22 | self.docbooks = {} # Stores filenames and their corresponding document content |
| 23 | |
| 24 | # Parse and store the document content if it's not empty |
| 25 | if generated_content != "": |
| 26 | if parse: |
| 27 | # Use regex to extract document content |
| 28 | regex = r"```\n(.*?)```" |
| 29 | matches = re.finditer(regex, self.generated_content, re.DOTALL) |
| 30 | for match in matches: |
| 31 | filename = "requirements.txt" # Default filename |
| 32 | doc = match.group(1) # Extract document content |
| 33 | self.docbooks[filename] = doc |
| 34 | else: |
| 35 | # If not parsing, use the predefined filename and content |
| 36 | self.docbooks[predefined_filename] = self.generated_content |
| 37 | |
| 38 | def _update_docs(self, generated_content, parse=True, predefined_filename=""): |
| 39 | """ |
| 40 | Updates document content, compares old and new content, and prints update information. |
| 41 | Args: |
| 42 | generated_content: New generated document content |
| 43 | parse: Whether to parse the content (default is True) |
| 44 | predefined_filename: Predefined filename (used if not parsing) |
| 45 | """ |
| 46 | new_docs = Documents(generated_content, parse, predefined_filename) # Create a new Documents instance |
| 47 | for key in new_docs.docbooks.keys(): |
| 48 | # If the document content has changed or is new, update docbooks |
| 49 | if key not in self.docbooks.keys() or self.docbooks[key] != new_docs.docbooks[key]: |
| 50 | print(f"{key} updated.") # Print updated filename |
| 51 | print(Fore.WHITE + f"------Old:\n{self.docbooks.get(key, '# None')}\n------New:\n{new_docs.docbooks[key]}") |
| 52 | self.docbooks[key] = new_docs.docbooks[key] # Update docbooks |
| 53 | |
| 54 | def _rewrite_docs(self): |
| 55 | """ |
| 56 | Writes document content to disk. |
| 57 | """ |
| 58 | directory = self.directory # Get the document storage directory |
| 59 | if not os.path.exists(directory): |
| 60 | # Create the directory if it doesn't exist |
| 61 | os.mkdir(directory) |
| 62 | print(f"{directory} Created.") |
| 63 | for filename in self.docbooks.keys(): |
| 64 | # Write document content to corresponding files |
no outgoing calls
no test coverage detected