Skip to content

Commit e6e7ad7

Browse files
committed
Re did all the plugin + changes on a new branch since my old one was having conflicts and could not be merged. #366
1 parent 12df3c1 commit e6e7ad7

File tree

5 files changed

+82
-78
lines changed

5 files changed

+82
-78
lines changed

sarra/plugins/html_page.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def file_size_fix(self):
3030
self.mysize = "%d" % isize
3131

3232
except:
33-
self.logger.debug("bad size %s" % self.mysize)
33+
# self.logger.debug("bad size %s" % self.mysize)
3434
return
3535

3636
return

sarra/plugins/line_date.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
"""comments:
3+
This plugin modifies the date of parent.line to a standard date fomrat: year-month-date hours:minutes:seconds
4+
An example of this date format is : 2021-09-06 11:09:00, which is Septemebr 6th, 2021 11:09PM
5+
Ten formats are acceptd so far, more can be added if needed (format on https://strftime.org/ ).
6+
FIXME: french input like Fev will not work - only Feb is accepted for the month
7+
If year is not provided, this means that the file is < 6 months old, so depending on todays date, assign
8+
appropriate year (for todays year: jan-jun -> assign prev year, for jul-dec assign current year)
9+
Note: is it possible for a file to be more than 6 months old and have the format Mo Day TIME ? (problematic)
10+
"""
11+
class Line_date(object):
12+
def __init__(self, parent):
13+
pass
14+
15+
def normalize_date_format(self, parent):
16+
import dateparser
17+
line_split = parent.line.split()
18+
# specify input for this routine, line format could change
19+
# line_mode.py format "-rwxrwxr-x 1 1000 1000 8123 24 Mar 22:54 2017-03-25-0254-CL2D-AUTO-minute-swob.xml"
20+
file_date = line_split[5] + " " + line_split[6] + " " + line_split[7]
21+
# case 1: the date contains '-' implies the date is in 1 string not 3 seperate ones, and H:M is also provided
22+
if "-" in file_date: file_date = line_split[5] + " " + line_split[6]
23+
current_date = datetime.datetime.now()
24+
standard_date_format = dateparser.parse(file_date,
25+
settings={
26+
'RELATIVE_BASE': datetime.datetime(1900, 1, 1),
27+
'TIMEZONE': parent.destination_timezone,
28+
'TO_TIMEZONE': 'UTC'})
29+
if standard_date_format is not None:
30+
type(standard_date_format)
31+
# case 2: the year was not given, it is defaulted to 1900. Must find which year (this one or last one).
32+
if standard_date_format.year == 1900:
33+
if standard_date_format.month - current_date.month >= 6:
34+
standard_date_format = standard_date_format.replace(year=(current_date.year - 1))
35+
else:
36+
standard_date_format = standard_date_format.replace(year=current_date.year)
37+
parent.logger.debug("Oldline is: " + parent.line)
38+
parent.line = parent.line.replace(file_date, str(standard_date_format))
39+
parent.logger.debug("Newline is: " + parent.line)
40+
return
41+
42+
def perform(self,parent):
43+
if hasattr(parent, 'line'):
44+
self.normalize_date_format(parent)
45+
return True
46+
47+
line_date = Line_date(self)
48+
self.on_line = line_date.perform

sarra/sr_config.py

+4
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ def declare_option(self,option):
659659
def defaults(self):
660660
self.logger.debug("sr_config defaults")
661661
self.file_time_limit = self.duration_from_str("60d")
662+
self.destination_timezone = 'UTC'
662663
self.retry_mode = True
663664
self.retry_ttl = None
664665

@@ -903,6 +904,7 @@ def defaults(self):
903904

904905
#self.on_post_list = [ self.on_post ]
905906
self.execfile("on_line",'line_mode')
907+
self.execfile("on_line", 'line_date')
906908

907909

908910
# this function converts duration into a specifid unit: [milliseconds, seconds or days]
@@ -1594,6 +1596,8 @@ def option(self,words):
15941596
self.logger.debug("Masks %s"% self.masks)
15951597
elif words0 =='file_time_limit':
15961598
self.file_time_limit = self.duration_from_str(words1)
1599+
elif words0 == 'destination_timezone':
1600+
self.destination_timezone = words1
15971601
elif words0 in ['accept_unmatched','accept_unmatch','au']: # See: sr_config.7
15981602
if (words1 is None) or words[0][0:1] == '-' :
15991603
self.accept_unmatch = True

sarra/sr_poll.py

+27-76
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
import os, sys, time
7070
import datetime
71-
71+
from dateparser import parse
7272
# ============================================================
7373
# DECLARE TRICK for false self.poster
7474

@@ -144,55 +144,6 @@ def check(self):
144144
self.pulls[maskDir].append(mask)
145145

146146

147-
def _file_date_exceed_limit(self, date, time_limit):
148-
"""comments:
149-
This method compares today's date to the file's date by creating a date time object
150-
Four formats are acceptd so far, more can be added if needed (format on https://strftime.org/ )
151-
Files with future dates are processed as long as the (future date - todays date) is < time_limit.
152-
FIXME: french input like Fev will not work - only Feb is accepted for the month
153-
If year is not provided, this means that the file is < 6 months old, so depending on todays date,
154-
assign appropriate year (for jan-jun -> assign prev year, for jul-dec assign current year)
155-
Note: is it possible for a file to be more than 6 months old and have the format Mo Day TIME ? (problematic)
156-
"""
157-
time_limit=int(time_limit)
158-
current_date = datetime.datetime.now()
159-
try:
160-
date_temp = datetime.datetime.strptime(date, '%d %b %H:%M')
161-
if date_temp.month - current_date.month >= 6:
162-
file_date = date_temp.replace(year=(current_date.year - 1))
163-
else:
164-
file_date = date_temp.replace(year=current_date.year)
165-
self.logger.debug("File date is: " + str(file_date) +
166-
" > File is " + str(abs((file_date - current_date).seconds))+" seconds old")
167-
return abs((file_date - current_date).seconds) < time_limit
168-
except Exception as e:
169-
try:
170-
date_temp = datetime.datetime.strptime(date, '%b %d %H:%M')
171-
if date_temp.month - current_date.month >= 6:
172-
file_date = date_temp.replace(year=(current_date.year - 1))
173-
else:
174-
file_date = date_temp.replace(year=current_date.year)
175-
self.logger.debug("File date is: " + str(file_date) +
176-
" > File is " + str(abs((file_date - current_date).seconds))+" seconds old")
177-
return abs((file_date - current_date).seconds) < time_limit
178-
except Exception as e:
179-
try:
180-
file_date = datetime.datetime.strptime(date, '%b %d %Y')
181-
self.logger.debug("File date is: " + str(file_date) +
182-
" > File is " + str(abs((file_date - current_date).seconds)) + " seconds old")
183-
return abs((file_date - current_date).seconds) < time_limit
184-
except Exception as e:
185-
try:
186-
file_date = datetime.datetime.strptime(date, '%d %b %Y')
187-
self.logger.debug("File date is: " + str(file_date) + " > File is " +
188-
str(abs((file_date - current_date).seconds)) + " seconds old")
189-
return abs((file_date - current_date).seconds) < time_limit
190-
except Exception as e:
191-
warning_msg = str(e)
192-
# self.logger.error("%s, assuming ok" % warning_msg)
193-
self.logger.error("Assuming ok, unrecognized date format, %s" % date)
194-
return True
195-
196147
# find differences between current ls and last ls
197148
# only the newer or modified files will be kept...
198149

@@ -207,6 +158,7 @@ def differ_ls_file(self, ls, lspath):
207158

208159
old_ls = self.load_ls_file(lspath)
209160

161+
file_within_date_limit = True
210162
# compare
211163

212164
filelst = []
@@ -215,34 +167,33 @@ def differ_ls_file(self, ls, lspath):
215167
for f in new_lst:
216168
# self.logger.debug("checking %s (%s)" % (f,ls[f]))
217169
try:
218-
str1 = ls[f]
219-
str2 = str1.split()
220-
# specify input for this routine.
221-
# ls[f] format controlled by online plugin (line_mode.py)
222-
# this format could change depending on plugin
223-
# line_mode.py format "-rwxrwxr-x 1 1000 1000 8123 24 Mar 22:54 2017-03-25-0254-CL2D-AUTO-minute-swob.xml"
224-
date = str2[5] + " " + str2[6] + " " + str2[7]
225-
if self._file_date_exceed_limit(date, self.file_time_limit):
226-
self.logger.debug("File should be processed")
227-
# execute rest of code
228-
# keep a newer entry
229-
if not f in old_ls:
230-
# self.logger.debug("IS NEW %s" % f)
231-
filelst.append(f)
232-
desclst[f] = ls[f]
233-
continue
234-
235-
# keep a modified entry
236-
if ls[f] != old_ls[f]:
237-
# self.logger.debug("IS DIFFERENT %s from (%s,%s)" % (f,old_ls[f],ls[f]))
238-
filelst.append(f)
239-
desclst[f] = ls[f]
240-
continue
241-
else:
242-
self.logger.debug("File should be skipped")
243-
# ignore rest of code and re iterate
170+
line_split = ls[f].split()
171+
date = line_split[5] + " " + line_split[6]
172+
file_date = parse(date)
173+
current_date = datetime.datetime.now()
174+
file_within_date_limit = abs((file_date - current_date).seconds) < self.file_time_limit
244175
except:
176+
self.logger.error("Assuming ok, incorrect date format for line: %s" % ls[f])
245177
pass
178+
if file_within_date_limit:
179+
self.logger.debug("File should be processed")
180+
# execute rest of code
181+
# keep a newer entry
182+
if not f in old_ls:
183+
# self.logger.debug("IS NEW %s" % f)
184+
filelst.append(f)
185+
desclst[f] = ls[f]
186+
continue
187+
188+
# keep a modified entry
189+
if ls[f] != old_ls[f]:
190+
# self.logger.debug("IS DIFFERENT %s from (%s,%s)" % (f,old_ls[f],ls[f]))
191+
filelst.append(f)
192+
desclst[f] = ls[f]
193+
continue
194+
else:
195+
self.logger.debug("File should be skipped")
196+
# ignore rest of code and re iterate
246197
# self.logger.debug("IS IDENTICAL %s" % f)
247198

248199
return filelst, desclst

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def read(*parts):
7272
"watchdog",
7373
"netifaces",
7474
"humanize",
75-
"psutil" ]
75+
"psutil",
76+
"dateparser"]
7677

7778
)

0 commit comments

Comments
 (0)