Skip to content

Commit

Permalink
Final adjustments for batch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusWirtz committed Feb 15, 2022
1 parent 84d2b37 commit b64cbe7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ cubecalc offers two execution modes:
> 1. Single Mode
If no `dimension`, `hierarchy` and `subset` arguments are passed, cubecalc will execute the calculation for a single
view
view.

> 2. Batch Mode
Expand Down
47 changes: 41 additions & 6 deletions cubecalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
import datetime
import logging
import os
import re
import sys
from typing import Dict

import click
from TM1py import TM1Service, MDXView
from TM1py.Utils.Utils import CaseAndSpaceInsensitiveDict
from TM1py import TM1Service, MDXView, AnonymousSubset
from TM1py.Utils.Utils import CaseAndSpaceInsensitiveDict, case_and_space_insensitive_equals

import methods
from utils import set_current_directory
Expand Down Expand Up @@ -166,18 +167,52 @@ def execute_iterative_mode(self, method, parameters):
private=False)

for element in element_names:
self.alter_view(**parameters, element=element)
self.alter_view(tm1_source=tm1_source_name, tm1_target=tm1_target_name, cube_source=cube_source,
view_source=view_source, cube_target=cube_target, view_target=view_target,
dimension=dimension, hierarchy=hierarchy, element=element)
result = METHODS[method](
**parameters,
tm1_services=self.tm1_services,
tidy=tidy if element == element_names[-1] else False)
logging.info(f"Successfully calculated {method} with result: {result} for title element {element}")
logging.info(f"Successfully calculated {method} with result: {result} for title element '{element}'")

# restore original source_view, target_view
if not tidy:
tm1_source.views.update_or_create(original_view_source, False)
tm1_target.views.update_or_create(original_view_target, False)

def substitute_mdx_view_title(self, view, dimension, hierarchy, element):
pattern = re.compile(r"\[" + dimension + r"\].\[" + hierarchy + r"\].\[(.*?)\]", re.IGNORECASE)
findings = re.findall(pattern, view.mdx)

if findings:
view.mdx = re.sub(
pattern=pattern,
repl=f"[{dimension}].[{hierarchy}].[{element}]",
string=view.mdx)
return

if hierarchy is None or case_and_space_insensitive_equals(dimension, hierarchy):
pattern = re.compile(r"\[" + dimension + r"\].\[(.*?)\]", re.IGNORECASE)
findings = re.findall(pattern, view.mdx)
if findings:
view.mdx = re.sub(
pattern=pattern,
repl=f"[{dimension}].[{element}]",
string=view.mdx)
return

raise ValueError(f"No selection in title with dimension: '{dimension}' and hierarchy: '{hierarchy}'")

def substitute_native_view_title(self, view, dimension, element):
for title in view.titles:
if case_and_space_insensitive_equals(title.dimension_name, dimension):
title._subset = AnonymousSubset(dimension, dimension, elements=[element])
title._selected = element
return

raise ValueError(f"Dimension '{dimension}' not found in titles")

def alter_view(self, tm1_source: str, tm1_target: str, cube_source: str, view_source: str, cube_target: str,
view_target: str, dimension: str, hierarchy: str, element: str):

Expand All @@ -191,10 +226,10 @@ def alter_view(self, tm1_source: str, tm1_target: str, cube_source: str, view_so
if isinstance(view, MDXView):
dimension = tm1.dimensions.determine_actual_object_name("Dimension", dimension)
hierarchy = tm1.hierarchies.determine_actual_object_name("Hierarchy", hierarchy)
view.substitute_title(dimension, hierarchy, element)
self.substitute_mdx_view_title(view, dimension, hierarchy, element)

else:
view.substitute_title(dimension, element)
self.substitute_native_view_title(view, dimension, element)

tm1.views.update(view, private=False)

Expand Down

0 comments on commit b64cbe7

Please sign in to comment.