|
8 | 8 | from tomlkit import comment, document, dumps, nl, table |
9 | 9 | from tomlkit.items import Table |
10 | 10 |
|
| 11 | +from openedx_learning.apps.authoring.publishing.models.learning_package import LearningPackage |
| 12 | + |
11 | 13 |
|
12 | 14 | class TOMLLearningPackageFile(): |
13 | 15 | """ |
14 | | - Class to create a .toml file of a learning package (WIP) |
| 16 | + Class to create a .toml representation of a LearningPackage instance. |
| 17 | +
|
| 18 | + This class builds a structured TOML document using `tomlkit` with metadata and fields |
| 19 | + extracted from a `LearningPackage` object. The output can later be saved to a file or used elsewhere. |
15 | 20 | """ |
16 | 21 |
|
17 | | - def __init__(self): |
| 22 | + def __init__(self, learning_package: LearningPackage): |
18 | 23 | self.doc = document() |
| 24 | + self.learning_package = learning_package |
19 | 25 |
|
20 | 26 | def _create_header(self) -> None: |
| 27 | + """ |
| 28 | + Adds a comment with the current datetime to indicate when the export occurred. |
| 29 | + This helps with traceability and file versioning. |
| 30 | + """ |
21 | 31 | self.doc.add(comment(f"Datetime of the export: {datetime.now()}")) |
22 | 32 | self.doc.add(nl()) |
23 | 33 |
|
24 | 34 | def _create_table(self, params: Dict[str, Any]) -> Table: |
| 35 | + """ |
| 36 | + Builds a TOML table section from a dictionary of key-value pairs. |
| 37 | +
|
| 38 | + Args: |
| 39 | + params (Dict[str, Any]): A dictionary containing keys and values to include in the TOML table. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + Table: A TOML table populated with the provided keys and values. |
| 43 | + """ |
25 | 44 | section = table() |
26 | 45 | for key, value in params.items(): |
27 | 46 | section.add(key, value) |
28 | 47 | return section |
29 | 48 |
|
30 | | - def create(self, lp_key: str) -> None: |
| 49 | + def create(self) -> None: |
31 | 50 | """ |
32 | | - Process the toml file |
| 51 | + Populates the TOML document with a header and a table containing |
| 52 | + metadata from the LearningPackage instance. |
| 53 | +
|
| 54 | + This method must be called before calling `get()`, otherwise the document will be empty. |
33 | 55 | """ |
34 | 56 | self._create_header() |
35 | 57 | section = self._create_table({ |
36 | | - "title": "", |
37 | | - "key": lp_key, |
38 | | - "description": "", |
39 | | - "created": "", |
40 | | - "updated": "" |
| 58 | + "title": self.learning_package.title, |
| 59 | + "key": self.learning_package.key, |
| 60 | + "description": self.learning_package.description, |
| 61 | + "created": self.learning_package.created, |
| 62 | + "updated": self.learning_package.updated |
41 | 63 | }) |
42 | 64 | self.doc.add("learning_package", section) |
43 | 65 |
|
44 | 66 | def get(self) -> str: |
| 67 | + """ |
| 68 | + Returns: |
| 69 | + str: The string representation of the generated TOML document. |
| 70 | + Ensure `create()` has been called beforehand to get meaningful output. |
| 71 | + """ |
45 | 72 | return dumps(self.doc) |
0 commit comments