Last active
March 10, 2023 12:14
-
-
Save BrunoMoraes-Z/326ac1ddc7a4fb06c4fe53512c3366f5 to your computer and use it in GitHub Desktop.
Simple RobotLibraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import os | |
from robot.api.deco import keyword, library | |
from robot.libraries.BuiltIn import BuiltIn | |
@library(scope='GLOBAL') | |
class CSVLibrary(object): | |
def __init__(self, search_directory: str = '') -> None: | |
self.dir = search_directory | |
@keyword(name='Set CSV Search Directory') | |
def set_csv_search_directory(self, search_directory: str): | |
self.dir = search_directory | |
@keyword(name='Get CSV Content') | |
def get_csv_content(self, file_name: str, delimiter: str =';', line: int = -1) -> list: | |
file = os.path.join(self.dir, file_name) | |
try: | |
with open(file) as csv_file: | |
reader = csv.reader(csv_file, delimiter=delimiter) | |
header = [] | |
rows = [] | |
row = {} | |
line_count = 0 | |
for r_row in reader: | |
if line_count == 0: | |
header = r_row | |
line_count += 1 | |
else: | |
row['g_id'] = line_count | |
for content in r_row: | |
index = r_row.index(content) | |
if index < len(header): | |
row[header[index]] = str(content) | |
line_count += 1 | |
rows.append(row) | |
row = {} | |
except Exception as e: | |
BuiltIn().fatal_error( | |
f'Ouve um erro na leitura do arquivo.\n\n{e}' | |
) | |
if line == -1: | |
print(rows) | |
BuiltIn().set_global_variable(f'${{{file_name.rsplit(".", 1)[0]}_input}}', rows) | |
return rows | |
else: | |
if line < 2: | |
line = 2 | |
if len(rows) < line - 1: | |
BuiltIn().fatal_error( | |
f'Favor informar um linha válida.\n\n> Linhas encontradas ({len(rows) + 1})' | |
) | |
data = rows[line - 2] | |
BuiltIn().set_global_variable(f'${{{file_name.rsplit(".", 1)[0]}_input}}', data) | |
print(data) | |
return data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import pandas as pd | |
from pandas import ExcelFile | |
from robot.api.deco import keyword, library | |
from robot.libraries.BuiltIn import BuiltIn | |
@library(scope='GLOBAL') | |
class ExcelLibrary(object): | |
@keyword(name='Init Pandas Library') | |
def init_pandas_library(self, excel_name: str): | |
storage_dir = BuiltIn().get_variable_value('${STORAGE_DIR}') | |
self.index = int(BuiltIn().get_variable_value('${ROW_INDEX}')) | |
self.doc: ExcelFile = pd.ExcelFile(os.path.join(storage_dir, excel_name)) | |
@keyword(name='List Sheet Names') | |
def list_sheet_names(self): | |
return self.doc.sheet_names | |
@keyword(name='Get Excel Content') | |
def get_excel_content(self, sheet_name: str) -> list: | |
if sheet_name not in self.list_sheet_names(): | |
return None | |
body = self.doc.parse(sheet_name) | |
raw_content = body.to_dict() | |
rows = [] | |
row = {} | |
header = [k for k in body.head(0).to_dict().keys()] | |
for x in range(0, len(raw_content[list(raw_content.keys())[0]].keys())): | |
row['in_tb_id'] = x | |
for y in range(0, len(header)): | |
try: | |
row[header[y]] = raw_content[list(raw_content.keys())[y]][x] | |
except: | |
row[header[y]] = 'null' | |
rows.append(row) | |
row = {} | |
if self.index < 2: | |
self.index = 2 | |
if len(rows) < self.index - 1: | |
BuiltIn().fatal_error( | |
f'Favor informar uma linha válida.\n\n> Linhas encontradas ({len(rows) + 1}).' | |
) | |
self.row = rows[self.index - 2] | |
BuiltIn().set_global_variable(f'${{{sheet_name}_input}}', self.row) | |
self.data = rows | |
return row | |
@keyword(name='Get Excel Value') | |
def get_excel_value(self, key): | |
return self.row[key] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment