|
| 1 | +"""See [TMDL Scripting].(https://learn.microsoft.com/en-us/analysis-services/tmdl/tmdl-overview). |
| 2 | +
|
| 3 | +Run `Tmdl(model).save_to_folder()` to save tmdl of model. |
| 4 | +Run `Tmdl(model).execute()` to execute a specific set of tmdl scripts. |
| 5 | +""" |
| 6 | + |
| 7 | +from Microsoft.AnalysisServices.Tabular import TmdlSerializer |
| 8 | + |
| 9 | + |
| 10 | +class Tmdl: |
| 11 | + """Specify the specific model you want to use for scripting. |
| 12 | +
|
| 13 | + Args: |
| 14 | + model (Tabular): Initialize with Tabular model. |
| 15 | + """ |
| 16 | + def __init__(self, model): |
| 17 | + self.model = model |
| 18 | + |
| 19 | + def save_to_folder(self, path: str = "tmdl"): |
| 20 | + """Runs `SerializeModelToFolder` from .net library. |
| 21 | +
|
| 22 | + Args: |
| 23 | + path (str, optional): directory where to save tmdl structure. |
| 24 | + Defaults to "tmdl". |
| 25 | + """ |
| 26 | + TmdlSerializer.SerializeModelToFolder(self.model._object, path) |
| 27 | + return True |
| 28 | + |
| 29 | + def execute(self, path: str = "tmdl", auto_save: bool = True): |
| 30 | + """Runs `DeserializeModelFromFolder` from .net library. |
| 31 | +
|
| 32 | + Args: |
| 33 | + path (str, optional): directory to look for tmdl scripts. |
| 34 | + Defaults to "tmdl". |
| 35 | + auto_save (bool, optional): You can set to false |
| 36 | + if you want to precheck a few things, but will need to |
| 37 | + run `model.save_changes()`. Setting to `True` will go ahead |
| 38 | + and execute `model.save_changes()` Defaults to True. |
| 39 | + """ |
| 40 | + model_object = TmdlSerializer.DeserializeModelFromFolder(path) |
| 41 | + model_object.CopyTo(self.model._object) |
| 42 | + if auto_save: |
| 43 | + return self.model.save_changes() |
| 44 | + else: |
| 45 | + return True |
0 commit comments