8913bbefeda797df1255120d0152bccaf51123f7
[myslice.git] / plugins / pres_view / daemon-todo / event2.py
1 # !/usr/bin/python
2 # -*-coding: utf-8-*-
3
4 import pickle
5 import os.path
6 import xmlrpclib
7 import ast
8 import time
9 import simplejson
10
11 class Event2:
12     def __init__(self, structure):
13         ##Definition des variables##
14         self.data_access_methode    =   ""
15         self.data_access_options    =   ""
16         
17         self.structure              =   structure
18         self.methode                =   ""
19         self.parameters             =   {}
20         self.return_fields          =   []
21         self.print_method           =   ""
22         self.print_options          =   ""
23         self.geo_methode            =   ""
24         self.id                     =   ""
25         self.timestamp              =   ""
26         #on paramètre timestamp, si c'est un dynamique, il sera fournit, sinon, on le met à 0
27         timestamp=0;
28         
29         ##APE###
30         self.raw                    =   ""
31         self.channel                =   ""
32         #######
33         
34         ## On creer l'objet ##
35         self.data = self.make_data(timestamp)
36         
37         
38     def split(self):
39         args = self.structure.split(";");
40         self.channel                =   args[1]  
41         self.raw                    =   args[3]
42         self.data_access_methode    =   args[4]
43         self.data_access_options    =   ast.literal_eval(args[5])
44         self.print_method           =   args[6]
45         self.print_options          =   ast.literal_eval(args[7])
46         self.methode                =   args[8]
47         self.parameters             =   ast.literal_eval(args[9])
48         self.return_fields          =   eval(args[10])
49         self.timestamp              =   args[11]
50
51     def get_event_list_from_xmlrpc(self,timestamp):
52         
53         ## on test pour voir si on est en dynamique, si c'est le cas, on met à jour la valeur du timestamp
54         if timestamp!=0:
55             self.parameters[self.timestamp]         =   int(timestamp)
56             
57             
58         ####on cree l'identification
59         auth = {}
60         if self.data_access_options["authType"]     ==  "anonymous":
61             auth["AuthMethod"]  =   "anonymous"
62             
63         elif self.data_access_options["authType"]   ==  "password":
64             auth["AuthMethod"]  =   "password"
65             auth["Username"]    =   self.data_access_options["Username"]
66             auth["AuthString"]  =   self.data_access_options["AuthString"]
67             
68         elif self.data_access_options["authType"]   ==  "session":
69             auth["AuthMethod"]  =   "session"
70             auth["session"]     =   self.data_access_options["session"]
71             
72         elif self.data_access_options["authType"]   ==  "gpg":
73             auth["AuthMethod"]  =   "gpg"
74             auth["name"]        =   self.data_access_options["name"]
75             auth["signature"]   =   self.data_access_options["signature"]
76         
77          
78         srv = xmlrpclib.Server(self.data_access_options["server"], allow_none = 1)
79         
80         
81         ##On gère en fonction des methodes
82         
83         if self.methode=="GetSites":
84             self.geo_methode="site_id";
85             try :
86                 if len(self.return_fields)==0:
87                     data =  srv.GetSites(auth, self.parameters) 
88                 else :
89                     data = srv.GetSites(auth, self.parameters, self.return_fields)
90             except:
91                     return self.parameters
92         else:
93             return 2
94         
95         ###on recupère la liste des localisation
96         
97         ##si il y a moins de 8 resultats, on recupère seulement la localisation de ceux-ci
98         list=[]
99         try:
100             sites_id = srv.GetSites(auth,{},["site_id","latitude","longitude"])
101         except :
102             return 3
103         try:
104             for i in data:
105                 for j in sites_id:
106                     if i["site_id"]==j["site_id"]:
107                         try:
108                             pos=[{"latitude": j["latitude"], "longitude": j["longitude"]}]
109                         except:
110                             return 4
111                         i["ape_position"] = pos
112                         list.append(i)
113         except:
114             return 5    
115         return list
116         
117
118     
119     def make_data(self,timestamp):
120         try :
121             self.split()
122         except:
123             return 43
124         try :
125             if self.data_access_methode=="xmlrpc":
126                 return self.get_event_list_from_xmlrpc(timestamp)
127         except: 
128             return 67
129