renamed acc_process into account_process
[unfold.git] / portal / event.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
10 class Event:
11     def __init__(self, structure):
12         self.srv=""
13         self.structure = structure
14         self.authType = ""
15         self.server = ""
16         self.methode = ""
17         self.parameters = {}
18         self.return_fields = []
19         
20         self.auth = {}
21         self.print_method=""
22         self.print_options=""
23         self.geo_methode=""
24         self.id=""
25         
26         ##APE###
27         self.raw = ""
28         self.channel = ""
29         #######
30
31
32         
33         
34         self.data = self.make_data()
35         
36         
37     def split(self):
38         args = self.structure.split(";");
39         self.raw = args[0]
40         self.channel = args[1]
41         self.print_method = args[2]
42         self.print_options = ast.literal_eval(args[3])
43         self.authType = args[4]
44         self.server = ast.literal_eval(args[5])
45         self.server = self.server["server"]
46         self.methode = args[6]
47         self.parameters = ast.literal_eval(args[7])
48         self.return_fields = eval(args[8])
49         
50     def makeAuth(self): 
51         if self.authType=="anonymous":
52             self.auth["AuthMethod"] = "anonymous"
53         elif type=="password":
54             self.auth["AuthMethod"] = "password"
55             self.auth["Username"] = "XXX"
56             self.auth["AuthString"] = "XXX"
57         elif self.authType=="session":
58             self.auth["AuthMethod"] = "session"
59             self.auth["session"] = "XXX"
60         elif self.authType=="gpg":
61             self.auth["AuthMethod"] = "gpg"
62             self.auth["name"] = "XXX"
63             self.auth["signature"] = "XXX"
64     
65     def include_geoposition(self, data):
66         list=[]
67         if len(data)<8:
68             for i in data:
69                 i["ape_position"] = self.get_geoposition(i) 
70                 list.append(i)
71             return list
72         else:
73             try:
74                 sites_id = self.srv.GetSites(self.auth,{},["site_id","latitude","longitude"])
75             except :
76                 return 45
77             try:
78                 for i in data:
79                     for j in sites_id:
80                         if i["site_id"]==j["site_id"]:
81                             try:
82                                 pos=[{"latitude": j["latitude"], "longitude": j["longitude"]}]
83                             except:
84                                 return 83
85                                                         #
86                             i["ape_position"] = pos
87                             list.append(i)
88                     
89             except:
90                 return 82      
91             return list
92         
93     def get_geoposition(self,i):
94         if self.geo_methode == "site_id":
95             id = i["site_id"]
96             try:
97                 geo_pos =  self.srv.GetSites(self.auth, {"site_id": id}, ["latitude","longitude"])
98             except:
99                 geo_pos = 43
100             return geo_pos
101         else:
102             return 42
103         
104
105     def get_event_list_from_xmlrpc(self):
106         self.srv = xmlrpclib.Server(self.server, allow_none = 1)
107         if self.methode=="GetSites":
108             self.geo_methode="site_id";
109             try :
110                 if len(self.return_fields)==0:
111                     return self.srv.GetSites(self.auth, self.parameters) 
112                 else :
113                     return self.srv.GetSites(self.auth, self.parameters, self.return_fields)
114             except:
115                     return 0
116         else:
117             return 0
118     
119     def make_data(self):
120         try :
121             self.split()
122             self.makeAuth()
123         except:
124             return 43
125         try : 
126             rototo = self.get_event_list_from_xmlrpc()
127         except: 
128             return 44
129         try:
130
131             return self.include_geoposition(rototo)
132         except:
133              return 47
134         
135