Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added feature to enable or disable rules #1103

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions TM1py/Services/CubeService.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import List, Iterable, Dict

from requests import Response
import base64

from TM1py.Objects.Cube import Cube
from TM1py.Services.CellService import CellService
Expand Down Expand Up @@ -231,7 +232,7 @@ def search_for_dimension(self, dimension_name: str, skip_control_cubes: bool = F

def search_for_dimension_substring(self, substring: str, skip_control_cubes: bool = False,
**kwargs) -> Dict[str, List[str]]:
""" Ask TM1 Server for a dictinary of cube names with the dimension whose name contains the substring
""" Ask TM1 Server for a dictionary of cube names with the dimension whose name contains the substring

:param substring: string to search for in dim name
:param skip_control_cubes: bool, True will exclude control cubes from result
Expand All @@ -249,6 +250,39 @@ def search_for_dimension_substring(self, substring: str, skip_control_cubes: boo
cube_dict = {entry['Name']: [dim['Name'] for dim in entry['Dimensions']] for entry in response.json()['value']}
return cube_dict

def enable_cube_rule(self, cube: Cube) -> None:
"""
Enable a cube rule from its base64-encoded hash if it exists.

:param cube: An instance of a Cube.
"""
current_rule = cube.rules.text
if not current_rule:
# If there is no rule, there is nothing to do.
return
try:
current_rule = current_rule[1:] if current_rule.startswith('#') else current_rule
cube.rules = base64.b64decode(current_rule).decode('utf-8')
except Exception:
raise ValueError(f"Current rule is not decodable by b64decode standards")
MariusWirtz marked this conversation as resolved.
Show resolved Hide resolved

self.update(cube)

def disable_cube_rule(self, cube: Cube) -> None:
"""
Disable a cube rule by saving its base64-encoded hash and commenting each line.

:param cube: An instance of a Cube.
"""
current_rule = cube.rules.text
if not current_rule:
# If there is no rule, there is nothing to do.
return

# Save the current rule as a base64-encoded hash
cube.rules = f"#{base64.b64encode(current_rule.encode('utf-8')).decode('utf-8')}"
MariusWirtz marked this conversation as resolved.
Show resolved Hide resolved
self.update(cube)

def search_for_rule_substring(self, substring: str, skip_control_cubes: bool = False, case_insensitive=True,
space_insensitive=True, **kwargs) -> List[Cube]:
""" get all cubes from TM1 Server as TM1py.Cube instances where rules for given cube contain specified substring
Expand Down Expand Up @@ -405,4 +439,4 @@ def get_vmt(self, cube_name: str):
def set_vmt(self, cube_name: str, vmt: int):
url = format_url("/Cubes('{}')", cube_name)
payload = {"ViewStorageMinTime": vmt}
response = self._rest.PATCH(url=url, data=json.dumps(payload))
response = self._rest.PATCH(url=url, data=json.dumps(payload))
19 changes: 18 additions & 1 deletion Tests/CubeService_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import configparser
import unittest
import uuid
Expand Down Expand Up @@ -27,7 +28,7 @@ def setUp(self):
# Connection to TM1
self.config = configparser.ConfigParser()
self.config.read(Path(__file__).parent.joinpath('config.ini'))
self.tm1 = TM1Service(**self.config['tm1srv01'])
self.tm1 = TM1Service(**self.config['tm1srv04'])

for dimension_name in self.dimension_names:
elements = [Element('Element {}'.format(str(j)), 'Numeric') for j in range(1, 1001)]
Expand Down Expand Up @@ -301,6 +302,22 @@ def test_get_measure_dimension(self):

self.assertEqual(self.dimension_names[-1], measure_dimension)

def test_toggle_cube_rule(self):
uncommented = "SKIPCHECK;\n[]=N:2;\n#find_me_comment\nFEEDERS;\n"
c = self.tm1.cubes.get(self.cube_name)
c.rules = uncommented
self.tm1.cubes.update(c)

# test disabling
self.tm1.cubes.disable_cube_rule(c)
self.assertEqual(c.has_rules, False)
self.assertEqual(c.rules.text.startswith('#'), True)

# test re-enable
self.tm1.cubes.enable_cube_rule(c)
self.assertEqual(c.rules.text, uncommented)


def tearDown(self):
self.tm1.cubes.delete(self.cube_name)
if self.tm1.cubes.exists(self.cube_name_to_delete):
Expand Down