From 93aaa66a42fff181419d0786507141113f6e7d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Gim=C3=A9nez?= Date: Sat, 8 Aug 2020 12:32:30 -0300 Subject: [PATCH] lint endpoints --- .pre-commit-config.yaml | 19 +++-- modules/logs/endpoints.py | 154 ++++++++++++++++++++++++++++---------- 2 files changed, 128 insertions(+), 45 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 67017dc..19be268 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,9 +8,18 @@ repos: - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files - - repo: https://github.com/pycqa/pylint - rev: pylint-2.5.3 + +# - repo: https://github.com/pycqa/pylint +# rev: pylint-2.5.3 +# hooks: +# - id: pylint +# stages: [commit] +# additional_dependencies: [pylint-flask] + + - repo: local hooks: - - id: pylint - stages: [commit] - additional_dependencies: [pylint-flask] + - id: pylint + name: pylint + entry: pylint + language: system + types: [python] diff --git a/modules/logs/endpoints.py b/modules/logs/endpoints.py index 4023494..a740974 100644 --- a/modules/logs/endpoints.py +++ b/modules/logs/endpoints.py @@ -1,15 +1,29 @@ +# -*- coding: utf-8 -*- +""" +enpoints for logging +""" import csv import datetime import os -from flask import Blueprint, request, send_from_directory, json +import re + +from flask import send_from_directory, json from flask_classy import FlaskView, route from modules import cbpi class LogView(FlaskView): + """ + View for logging + """ @route('/', methods=['GET']) - def get_all_logfiles(self): + def get_all_logfiles(self): # pylint: disable=no-self-use + """ + get all log files + + :return: json for all logs + """ result = [] for filename in os.listdir("./logs"): if filename.endswith(".log"): @@ -17,90 +31,150 @@ class LogView(FlaskView): return json.dumps(result) @route('/actions') - def actions(self): + def actions(self): # pylint: disable=no-self-use + """ + get actions log + + :return: json for actions log + """ filename = "./logs/action.log" - if os.path.isfile(filename) == False: - return + if not os.path.isfile(filename): + return ('File not found', 404) array = [] - with open(filename, 'rb', encoding='utf-8') as f: - reader = csv.reader(f) + with open(filename, 'rb', encoding='utf-8') as csv_file: + reader = csv.reader(csv_file) for row in reader: try: - array.append([int((datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S") - datetime.datetime(1970, 1, 1)).total_seconds()) * 1000, row[1]]) - except: + array.append([ + int((datetime.datetime.strptime( + row[0], "%Y-%m-%d %H:%M:%S") - + datetime.datetime(1970, 1, 1)).total_seconds()) * + 1000, row[1] + ]) + except IndexError: pass return json.dumps(array) @route('/', methods=["DELETE"]) def clearlog(self, file): """ - Overload delete method to shutdown sensor before delete - :param id: sensor id + log delete + :param file: log file name :return: HTTP 204 """ if not self.check_filename(file): return ('File Not Found', 404) filename = "./logs/%s" % file - if os.path.isfile(filename) == True: + if os.path.isfile(filename): os.remove(filename) cbpi.notify("log deleted succesfully", "") else: cbpi.notify("Failed to delete log", "", type="danger") return ('', 204) - def read_log_as_json(self, type, id): - filename = "./logs/%s_%s.log" % (type, id) - if os.path.isfile(filename) == False: - return + def read_log_as_json(self, log_type, log_id): # pylint: disable=no-self-use + """ + :param log_type: log type + :param log_id: log id + + :return: log as array + """ + filename = "./logs/%s_%s.log" % (log_type, log_id) + if not os.path.isfile(filename): + return ('File not found', 404) - import csv array = [] - with open(filename, 'rb', encoding='utf-8') as f: - reader = csv.reader(f) + with open(filename, 'rb', encoding='utf-8') as csv_file: + reader = csv.reader(csv_file) for row in reader: try: - array.append([int((datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S") - datetime.datetime(1970, 1, 1)).total_seconds()) * 1000, float(row[1])]) - except: + array.append([ + int((datetime.datetime.strptime( + row[0], "%Y-%m-%d %H:%M:%S") - + datetime.datetime(1970, 1, 1)).total_seconds()) * + 1000, + float(row[1]) + ]) + except IndexError: pass return array def convert_chart_data_to_json(self, chart_data): - return {"name": chart_data["name"], "data": self.read_log_as_json(chart_data["data_type"], chart_data["data_id"])} + """ + :param chart_data: data for a chart - @route('//', methods=["POST"]) - def get_logs_as_json(self, t, id): - data = request.json - result = [] - if t == "s": - name = cbpi.cache.get("sensors").get(id).name - result.append({"name": name, "data": self.read_log_as_json("sensor", id)}) + :return: json for chart data + """ + return { + "name": + chart_data["name"], + "data": + self.read_log_as_json(chart_data["data_type"], + chart_data["data_id"]) + } - if t == "k": - kettle = cbpi.cache.get("kettle").get(id) - result = list(map(self.convert_chart_data_to_json, cbpi.get_controller(kettle.logic).get("class").chart(kettle))) + @route('//', methods=["POST"]) + def get_logs_as_json(self, log_type, log_id): + """ + :param log_type: log type + :param log_id: log id - if t == "f": - fermenter = cbpi.cache.get("fermenter").get(id) - result = list(map(self.convert_chart_data_to_json, cbpi.get_fermentation_controller(fermenter.logic).get("class").chart(fermenter))) + :return: log as array + """ + result = [] + if log_type == "s": + name = cbpi.cache.get("sensors").get(log_id).name + result.append({ + "name": name, + "data": self.read_log_as_json("sensor", log_id) + }) + + if log_type == "k": + kettle = cbpi.cache.get("kettle").get(log_id) + result = list( + map( + self.convert_chart_data_to_json, + cbpi.get_controller( + kettle.logic).get("class").chart(kettle))) + + if log_type == "f": + fermenter = cbpi.cache.get("fermenter").get(log_id) + result = list( + map( + self.convert_chart_data_to_json, + cbpi.get_fermentation_controller( + fermenter.logic).get("class").chart(fermenter))) return json.dumps(result) @route('/download/') @cbpi.nocache def download(self, file): + """ + :param file: log file name + + :return: log data + """ if not self.check_filename(file): return ('File Not Found', 404) - return send_from_directory('../logs', file, as_attachment=True, attachment_filename=file) + return send_from_directory('../logs', + file, + as_attachment=True, + attachment_filename=file) - def check_filename(self, name): - import re + def check_filename(self, name): # pylint: disable=no-self-use + """ + :param name: log file name + :return: bool + """ pattern = re.compile('^([A-Za-z0-9-_])+.log$') - return True if pattern.match(name) else False + return bool(pattern.match(name)) + @cbpi.initalizer() -def init(app): +def init(app): # pylint: disable=unused-argument """ Initializer for the message module :param app: the flask app