def _read(self, service, resource): try: read_params = { match: self.spec["data"][match] for match in self.spec["matches"] } verb = self.spec["verb"]["read"] # list를 지원안하면 exception read_resources = _execute_api(service, resource, verb, read_params, api_version="v1", output="yaml", parser={}, silent=self.silent) if isinstance(read_resources, list): length = len(read_resources) if length == 0: self.output = {} elif length >= 1: self.output = read_resources[0] if length >= 2: click.echo( "Multiple resources are searched so select the first one." ) elif isinstance(read_resources, dict): # like dict self.output = read_resources echo(f'### {verb} Response ###', flag=not self.silent) echo(read_resources, flag=not self.silent) except Exception as e: click.echo(e, err=True)
def _format_header(worksheet, start_at, headers): header_format = { 'horizontalAlignment': 'CENTER', 'borders': { 'top': {'style': 'SOLID'}, 'bottom': {'style': 'SOLID'}, 'left': {'style': 'SOLID'}, 'right': {'style': 'SOLID'} }, "backgroundColor": { "red": 12, "green": 12, "blue": 12 }, 'textFormat': {'bold': True} } try: header_length = len(headers) _header_split = re.split('(\d+)', start_at) _col = _header_split[0] _col_idx = string.ascii_uppercase.index(_col) _chr_index = 64 + _col_idx + header_length if 64 + _col_idx + header_length <= 90 else 90 _num = _header_split[1] header_cells = f'{start_at}:{chr(_chr_index)}{_num}' worksheet.format(header_cells, header_format) except Exception as e: echo(f'e => {e}') pass
def clear_all_worksheet(self, sheet): reset = self.spec.get('reset', False) if reset: echo(f"Clear All Worksheet in selected sheet..", flag=not self.silent) sheet.add_worksheet(title='', rows=1000, cols=26) for worksheet in sheet.worksheets()[:-1]: sheet.del_worksheet(worksheet)
def export_data(self, sheet): fill_na = self.spec.get('fill_na') for idx, raw_data in enumerate(self.spec.get('data', [])): time.sleep(3) # task = self._convert_json(raw_data.get('input', {})) task = raw_data.get('input', {}) worksheet_name = self.set_worksheet_name(task) echo(f"Export Worksheet: {worksheet_name}") worksheet = self.select_worksheet(sheet, idx, worksheet_name) # self.write_update_time(worksheet) self.export_worksheet(worksheet, task.get('output', []), fill_na)
def parse_key_value(inputs): result = {} for data in inputs: try: key, value = data.split("=", 1) if value.find("=") != -1: raise ValueError result[key] = value except ValueError: echo(f'[Error] input should be like <key>=<value>, not {data}', err=True, terminate=True) return result
def apply_template(obj, task_id): fields = [] if isinstance(obj, (str, bool, int, float, bool)): return 0 elif obj is None: return 0 elif isinstance(obj, list): # if it's a list, apply template to its items for item in obj: apply_template(item, task_id) return 0 elif isinstance(obj, dict): # if it's a dict, apply template to its items fields = list(obj.keys()) elif hasattr(obj, "fields_to_apply_template"): # if it has "fields_to_apply_template" apply only the correspondent fields fields = obj.fields_to_apply_template else: fields = obj.__dict__ for field in fields: value = _get_obj_value(obj, field) if isinstance(value, str): template = jinja_env.from_string(value) try: template_applied_value = template.render( var=store.get_var(), env=store.get_env(), tasks=store.get_task_results()) if field == 'if': obj[field] = _evaluate_if_statement(template_applied_value) else: obj[field] = template_applied_value except jinja2.exceptions.UndefinedError as e: echo( "While applying templates for Task(id={task_id}), an undefined error has occurred\n{error_message}" .format(error_message=e.message, task_id=task_id), err=True, terminate=True) else: apply_template(value, task_id)
def execute(self): stdout = subprocess.PIPE stderr = subprocess.STDOUT completed_process = subprocess.run( [ "/bin/bash", "-c", self.spec["run"] # "abcde" ], stdout=stdout, stderr=stderr, ) self.output = { "result": completed_process.stdout.decode("utf-8"), "return_code": completed_process.returncode } echo(self.output["result"], flag=not self.silent, err=bool(self.output["return_code"]))
def _exec(self, service, resource): try: verb = self.spec["verb"]["exec"] echo("Start " + ".".join([service, resource, verb]), flag=not self.silent) _check_api_permissions(service, resource, verb) self.output = _execute_api(service, resource, verb, self.spec.get("data", {}), api_version="v1", output="yaml", parser={}, silent=self.silent) echo("Finish " + ".".join([service, resource, verb]), flag=not self.silent) echo(f'### {verb} Response ###', flag=not self.silent) echo(self.output, flag=not self.silent) except Exception as e: echo(e, err=True, terminate=True)
def _execute_api(service, resource, verb, params={}, api_version='v1', output='yaml', parser=None, silent=False): config = get_config() _check_api_permissions(service, resource, verb) client = _get_client(service, api_version) # _call_api can change some data of params so need deepcopy # e.g. credential of identity.Token response_stream = _call_api(client, resource, verb, copy.deepcopy(params), config=config) for response in response_stream: if verb in ['list', 'stat', 'analyze']: results = response.get('results', []) if len(results) == 0: return [] elif len(results) > 0: return results else: Exception() elif verb == 'create': return response elif verb == 'update': return response else: echo("[INFO] Non-standard verb: " + verb, flag=not silent) return response
def _create(self, service, resource): verb = self.spec["verb"]["create"] echo("Start " + ".".join([service, resource, verb]), flag=not self.silent) create_result = _execute_api(service, resource, verb, self.spec['data'], api_version="v1", output="yaml", parser={}, silent=self.silent) echo("Finished " + ".".join([service, resource, verb]), flag=not self.silent) self.output = create_result echo(f'### {verb} Response ###', flag=not self.silent) echo(self.output, flag=not self.silent)
def _update(self, service, resource): try: verb = self.spec["verb"]["update"] echo("Start " + ".".join([service, resource, verb]), flag=not self.silent) _check_api_permissions(service, resource, verb) self.output = _execute_api(service, resource, verb, self.spec['data'], api_version="v1", output="yaml", parser={}, silent=self.silent) echo("Finish " + ".".join([service, resource, verb]), flag=not self.silent) echo(f'### {verb} Response ###', flag=not self.silent) echo(self.output, flag=not self.silent) except Exception as e: echo(e, flag=True, err=True) echo("Unavailable update field so Skip" + ".".join([service, resource, "update"]), flag=True, err=True, terminate=True)
def dot_access_error_handler(self, task_id): echo( 'You cannot access to a Task(id={task_id}). Accessible Task IDs are {task_ids}' .format(task_id=task_id, task_ids=self.get_task_ids()), err=True, terminate=True)
def dot_access_error_handler(self, attr): echo("An attr named {attr} doesn't exist in {self}".format(self=self, attr=attr), err=True, terminate=True)
def _get_sheets(self, google_sheets): echo(f"Open Sheets : {self.spec.get('sheet_id')}", flag=not self.silent) return google_sheets.open_by_key(self.spec.get('sheet_id'))
def _init_google_sheets(self): echo("Access Google Sheets..", flag=not self.silent) service_account = self.spec.get('service_account_json') return gspread.service_account(filename=service_account)