Beispiel #1
0
from TM1py import TM1Queries, Process
import uuid

# connection to TM1 Server
q = TM1Queries(ip='', port=8008, user='******', password='******', ssl=True)
# just a random string
random_string = str(uuid.uuid4()).replace('-', '_')

# create new Process
p_ascii = Process(name='unittest_ascii_' + random_string, datasource_type='ASCII',
                  datasource_ascii_delimiter_char=',',
                  datasource_data_source_name_for_server='C:\Data\simple_csv.csv',
                  datasource_data_source_name_for_client='C:\Data\simple_csv.csv')
# variables
p_ascii.add_variable('v_1', 'Numeric')
p_ascii.add_variable('v_2', 'Numeric')
p_ascii.add_variable('v_3', 'Numeric')
p_ascii.add_variable('v_4', 'Numeric')
# parameters
p_ascii.add_parameter('p_Year', 'year?', '2016')
# create process on Server
q.create_process(p_ascii)

# update existing Process:
p_new = q.get_process(p_ascii.name)
# modify
p_new.set_data_procedure(Process.auto_generated_string() + "x = 'Hi this is a test';")
# update on Server
q.update_process(p_new)

# delete Process from Server
Beispiel #2
0
    def put_turbointegrator(self, active_view):
        process_name = self.active_file_base

        sublime.status_message(
            'Processing server update of ti process: {}'.format(process_name))

        try:
            process = self._session.processes.get(process_name)
            update_process = True
        except TM1pyException as e:
            process = Process(name=process_name)
            update_process = False

        regions = active_view.split_by_newlines(
            sublime.Region(0, active_view.size()))
        text = [
            active_view.substr(region).rstrip() + '\n' for region in regions
        ]

        # Parse Sections
        sections = [
            'PARAMETERS', 'DATASOURCE', 'VARIABLES', 'PROLOG', 'METADATA',
            'DATA', 'EPILOG'
        ]
        section_text = {}

        comment_line = '###############################################################################'
        for section in sections:
            parse_section = section
            section_text[parse_section] = ''

            regex_find = re.compile('^(#+\s*?)?(' + parse_section + ')(:|;)')
            regex_end = re.compile('^(#+\s*?)?({})(:|;)'.format(
                '|'.join(sections)))

            search_active = False

            for line in text:
                if search_active and not regex_end.search(
                        line) and line.rstrip() != comment_line:
                    section_text[parse_section] += line.rstrip() + '\n'
                if regex_end.search(line) and search_active:
                    break
                if regex_find.search(line) and not search_active:
                    search_active = True

        for section in ['PARAMETERS', 'DATASOURCE', 'VARIABLES']:
            section_text[section] = section_text[section].replace("### ", "")

        parameters = yaml.load(section_text['PARAMETERS'])
        if parameters == 'None':
            parameters = []

        for parameter in process.parameters.copy():
            process.remove_parameter(parameter['Name'])

        for parameter in parameters:
            process.add_parameter(parameter['name'], parameter['prompt'],
                                  parameter['value'])

        # Datasource
        datasource = yaml.load(section_text['DATASOURCE'])

        if datasource == 'None':
            datasource = {'type': 'None'}

        for key, item in datasource.items():
            obj_key = 'datasource_' + key
            try:
                if obj_key in dir(process):
                    setattr(process, '' + obj_key, item)
                else:
                    print('encountered unknown datasource setting: ' + key)
            except Exception as e:
                sublime.message_dialog(
                    'An error occurred updating {}\n\n{}'.format(
                        process_name, e))
                raise

        # Variables
        variables = yaml.load(section_text['VARIABLES'])
        for variable in process.variables.copy():
            process.remove_variable(variable['Name'])

        if variables != 'None':
            for x in variables:
                if '(Numeric)' in x['name']:
                    var_type = 'Numeric'
                    var_name = x['name'].replace('(Numeric)', '')
                else:
                    var_type = 'String'
                    var_name = x['name'].replace('(String)', '')

                var_name = var_name.strip()
                process.add_variable(var_name, var_type)

        process.prolog_procedure = section_text['PROLOG'].strip()
        process.metadata_procedure = section_text['METADATA'].strip()
        process.data_procedure = section_text['DATA'].strip()
        process.epilog_procedure = section_text['EPILOG'].strip()

        try:
            if not update_process:
                self._session.processes.create(process)
            else:
                self._session.processes.update(process)

            errors = self._session.processes.compile(process_name)
            if errors:
                sublime.message_dialog(
                    'Error compiling {}: \n\nProcedure: {} (~Line {})\n\n{}'.
                    format(process_name, errors[0]['Procedure'],
                           str(errors[0]['LineNumber']), errors[0]['Message']))
            else:
                sublime.message_dialog(
                    'Updated {} TI Process Successfully'.format(process_name))
        except Exception as e:
            sublime.message_dialog(
                'An error occurred updating {}\n\n{}'.format(process_name, e))
            raise