#!/usr/bin/python3
# coding: utf-8

import os
import sys
from time import sleep, time

sys.path.append(os.path.join(__file__, '../util'))
from .recorderthread import RecorderThread
from util.mailutil import create_mail, send_mail


class Mail(RecorderThread):
    def record(self, eventid, duration):
        eventdir = os.path.join(self.homedir,
                                'data',
                                'recorder',
                                self.setting['datasource'],
                                eventid)
        processed_files = set()

        sleep(0.1)
        finishtime = time() + duration
        is_last = False

        while True:
            now = time()
            next = now + self.setting['interval']
            if now > finishtime:
               is_last = True 

            new_files = self.get_newfiles(eventdir, processed_files)
            if not new_files:
                if is_last:
                    break
                sleep(now + self.setting['interval'] - time())
                continue

            mail = create_mail(mfrom=self.setting['mail_from'],
                               mto=self.setting['mail_to'],
                               subject=self.setting['subject'] + ' Event ID: ' + eventid,
                               attachment_paths=new_files)
            send_mail(mfrom=self.setting['mail_from'],
                      mpassword=self.setting['password'],
                      mto=self.setting['mail_to'],
                      mserver=self.setting['server'],
                      mport=self.setting['port'],
                      mailcontent=mail)
            processed_files.update(new_files)

            now = time()
            if is_last or now > finishtime:
                break
            if next > now:
                sleep(next - now)

    def get_newfiles(self, dir_name, processed):
        try:
            files = os.listdir(dir_name)
        except OSError:
            return None

        current_files = []
        for f in files:
            f = os.path.join(dir_name, f)
            if os.path.isfile(f):
                current_files.append(f)
        newfiles =  set(current_files).difference(processed)
        return sorted(list(newfiles))
        

if __name__ == '__main__':
    pass
#    mailer = Mail()
#    mailer.start_recording('test', 20)
#    mailer.join()

@@ -314,153 +314,61 @@
 tdir
+s
  = 
-os.path.join(self.homedir,%0A                                'data',%0A                                'recorder',%0A                               
+%5B%5D%0A        processed_files = %7B%7D%0A        for ds in
  sel
@@ -376,17 +376,21 @@
 .setting
-%5B
+.get(
 'datasou
@@ -393,21 +393,23 @@
 asource'
-%5D,%0A  
+, %5B%5D):%0A
         
@@ -416,35 +416,122 @@
     
-                  eventid)%0A
+eventdir = os.path.join(self.homedir, 'data', 'recorder', ds, eventid)%0A            eventdirs.append(eventdir)%0A    
     
@@ -545,24 +545,34 @@
 cessed_files
+%5Beventdir%5D
  = set()%0A%0A  
@@ -587,11 +587,39 @@
 eep(
-0.1
+self.setting.get('interval', 1)
 )%0A  
@@ -840,16 +840,54 @@
  True %0A%0A
+            for dir in eventdirs:%0A    
         
@@ -920,21 +920,16 @@
 ewfiles(
-event
 dir, pro
@@ -936,26 +936,35 @@
 cessed_files
-)%0A
+%5Bdir%5D)%0A    
             
@@ -989,121 +989,8 @@
     
-            if is_last:%0A                    break%0A                sleep(now + self.setting%5B'interval'%5D - time())%0A
     
@@ -1019,24 +1019,28 @@
             
+   
+ 
 mail = creat
@@ -1102,32 +1102,36 @@
                 
+    
 mto=self.setting
@@ -1174,16 +1174,20 @@
         
+   
+ 
 subject=
@@ -1268,16 +1268,20 @@
         
+    
 attachme
@@ -1312,16 +1312,20 @@
         
+  
+  
 send_mai
@@ -1381,16 +1381,20 @@
         
+    
 mpasswor
@@ -1435,32 +1435,36 @@
                 
+  
+  
 mto=self.setting
@@ -1468,32 +1468,36 @@
 ing%5B'mail_to'%5D,%0A
+    
                 
@@ -1556,16 +1556,20 @@
         
+  
+  
 mport=se
@@ -1606,24 +1606,28 @@
             
+    
 mailcontent=
@@ -1636,32 +1636,36 @@
 il)%0A            
+  
+  
 processed_files.
@@ -1663,16 +1663,21 @@
 ed_files
+%5Bdir%5D
 .update(

handle multiple datasource