Skip to content

Commit

Permalink
Finalization FileService updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusWirtz committed Jan 31, 2024
1 parent 7e6f904 commit e3bc795
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
28 changes: 20 additions & 8 deletions TM1py/Services/FileService.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import json
import warnings
from typing import List, Iterable

from TM1py.Services import RestService
Expand All @@ -22,10 +23,21 @@ def __init__(self, tm1_rest: RestService):
else:
self.version_content_path = 'Blobs'

def get_names(self, return_list: bool=False, **kwargs) -> bytes:
def get_names(self, **kwargs) -> bytes:
warnings.warn(
f"Function get_names will be deprecated. Use get_all_names instead",
DeprecationWarning,
stacklevel=2)

url = format_url(
"/Contents('{version_content_path}')/Contents?$select=Name",
version_content_path=self.version_content_path)

return self._rest.GET(url, **kwargs).content

def get_all_names(self, **kwargs) -> List[str]:
""" return list of blob file names
:param return_list: True to return list, otherwise will return bytes
"""

url = format_url(
Expand All @@ -34,7 +46,7 @@ def get_names(self, return_list: bool=False, **kwargs) -> bytes:

response = self._rest.GET(url, **kwargs).content

return response if not return_list else [file['Name'] for file in json.loads(response)['value']]
return [file['Name'] for file in json.loads(response)['value']]

def get(self, file_name: str, **kwargs) -> bytes:

Expand Down Expand Up @@ -92,24 +104,24 @@ def delete(self, file_name: str, **kwargs):
version_content_path=self.version_content_path)

return self._rest.DELETE(url, **kwargs)
def search_string_in_name(self, name_startswith: str = None, name_contains: Iterable = None,

def search_string_in_name(self, name_startswith: str = None, name_contains: Iterable = None,
name_contains_operator: str = 'and', **kwargs) -> List[str]:
""" Return list of blob files that match search critera
:param name_startswith: str, file name begins with (case insensitive)
:param name_contains: iterable, found anywhere in name (case insensitive)
:param name_contains_operator: 'and' or 'or'
"""

url = format_url(
"/Contents('{version_content_path}')/Contents?$select=Name",
version_content_path=self.version_content_path)

name_contains_operator = name_contains_operator.strip().lower()
if name_contains_operator not in ("and", "or"):
raise ValueError("'name_contains_operator' must be either 'AND' or 'OR'")

name_filters = []

if name_startswith:
Expand Down
37 changes: 28 additions & 9 deletions Tests/FileService_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,36 @@ def test_update_get(self):
self.assertEqual(original_file.read(), created_file)

@skip_if_insufficient_version(version="11.4")
def test_get_names(self):
self.assertEqual('bytes', type(self.tm1.files.get_names()))
self.assertEqual('list', type(self.tm1.files.get_names(return_list=True)))
def test_get_all_names(self):
result = self.tm1.files.get_all_names()
self.assertIn(self.FILE_NAME1, result)

@skip_if_insufficient_version(version="11.4")
def test_search_string_in_name(self):
self.assertTrue(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_starts_with=self.FILE_NAME1[:5]))
self.assertFalse(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_starts_with='not_the_file_im_looking_for'))
self.assertTrue(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], self.FILE_NAME1[-5:]]))
self.assertFalse(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], 'NotFound']))
self.assertTrue(self.FILE_NAME1 in self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], 'NotFound']), name_contains_operator='OR')
def test_search_string_in_name__name_startswith(self):
result = self.tm1.files.search_string_in_name(name_startswith=self.FILE_NAME1)
self.assertEqual([self.FILE_NAME1], result)

@skip_if_insufficient_version(version="11.4")
def test_search_string_in_name__name_startswith_not_existing(self):
result = self.tm1.files.search_string_in_name(name_startswith='not_the_file_im_looking_for')
self.assertEqual([], result)

@skip_if_insufficient_version(version="11.4")
def test_search_string_in_name__name_contains_both_existing(self):
result = self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], self.FILE_NAME1[-5:]])
self.assertEqual([self.FILE_NAME1], result)

@skip_if_insufficient_version(version="11.4")
def test_search_string_in_name__name_contains_mixed_existing_and(self):
result = self.tm1.files.search_string_in_name(name_contains=[self.FILE_NAME1[:5], 'NotFound'])
self.assertEqual([], result)

@skip_if_insufficient_version(version="11.4")
def test_search_string_in_name__name_contains_mixed_existing_or(self):
result = self.tm1.files.search_string_in_name(
name_contains=[self.FILE_NAME1, 'NotFound'],
name_contains_operator='OR')
self.assertEqual([self.FILE_NAME1], result)

@skip_if_insufficient_version(version="11.4")
def test_delete_exists(self):
Expand Down

0 comments on commit e3bc795

Please sign in to comment.