Repository navigation
Expand file tree
/
Copy pathbuffer_log.py
More file actions
99 lines (86 loc) · 3.73 KB
/
Copy pathbuffer_log.py
File metadata and controls
99 lines (86 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This file is part of pwm_fanshim.
# Copyright (C) 2015 Ivmech Mechatronics Ltd. <bilgi@ivmech.com>
#
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# title :buffer_log.py
# description :pwm control for R Pi Cooling Fan
# author :David Torrens
# start date :2019 11 20
# version :0.1
# python_version :3
# Standard library imports
#from configparser import RawConfigParser
from csv import DictReader as csv_DictReader
from csv import DictWriter as csv_DictWriter
from datetime import datetime
from shutil import copyfile
from ftplib import FTP
from sys import argv as sys_argv
from sys import exit as sys_exit
import socket
# Third party importstemp_log
# none
# Local application imports
from utility import pr,make_time_text,send_by_ftp
class class_buffer_log:
def __init__(self,config):
self.dbug = False
self.__send_plain_count = 5
self.__no_heading_yet = True
self.__config = config
starttime = datetime.now()
timestamp = make_time_text(starttime)
self.log_filename = timestamp + "_" + self.__config.prog_name + "_" + "lg.csv"
self.__log_filename_save_as = self.__config.prog_path + self.__config.log_directory + self.log_filename
self.__local_www_log_filename = self.__config.local_dir_www + "/" + self.__config.log_directory + self.log_filename
def log_to_file(self,log_headings,log_values):
here = "log_cpu_data_to_file"
#write the time at the start of the line in logging file
if self.__no_heading_yet:
self.__no_heading_yet = False
self.__log_file = open(self.__log_filename_save_as,'w')
for hdg_ind in range(0,len(log_headings)):
self.__log_file.write(log_headings[hdg_ind] + ",")
self.__log_file.write("\n")
for z in range(0,len(log_values),1):
self.__log_file.write(str(log_values[z]) + ",")
self.__log_file.write("\n")
self.__log_file.flush()
return
def send_log_by_ftp(self,FTP_dbug_flag,remote_log_dir,ftp_timeout):
here = "bffr_log_log_by_ftp"
ftp_result = send_by_ftp(FTP_dbug_flag,self.__config.ftp_creds_filename, self.__log_filename_save_as, \
self.log_filename,remote_log_dir,ftp_timeout)
for pres_ind in range(0,len(ftp_result)):
pr(FTP_dbug_flag,here, str(pres_ind) + " : ", ftp_result[pres_ind])
if self.__send_plain_count < 0 :
ftp_result = send_by_ftp(FTP_dbug_flag,self.__config.ftp_creds_filename, self.__log_filename_save_as, \
"log.csv",remote_log_dir,ftp_timeout)
for pres_ind in range(0,len(ftp_result)):
pr(FTP_dbug_flag,here, str(pres_ind) + " : ", ftp_result[pres_ind])
self.__send_plain_count = 10
else:
self.__send_plain_count -= 1
#print("Send plain count : ",self.__send_plain_count)
return
def copy_log_to_www(self,dbug_flag):
here = "copy_log_to_www"
try:
# send the same html file to the local web site
copyfile(self.__log_filename_save_as, self.__local_www_log_filename)
pr(dbug_flag,0, "Sent : " + self.__log_filename_save_as + " to : ", self.__local_www_log_filename)
except:
pr(True,0,"Fail with copy " + self.__log_filename_save_as + " to : ", self.__local_www_log_filename)