VERSION 0.1.4 faire un .deb
[masstransit] / src / masstransit.py
1 #!/usr/bin/python
2 import pygtk
3 pygtk.require("2.0")
4 import gtk
5 import urllib2
6 import HTMLParser
7 import hildon
8 import ConfigParser
9
10 from portrait import FremantleRotation
11
12 #main_window = mainWindow # your main hildon.StackableWindow
13 app_name = 'NameOfYourApp' # the name of your app
14 app_version = '1.0' # the version number of your app
15 initial_mode = FremantleRotation.AUTOMATIC
16
17 class LigneHoraire(object):
18     def __init__(self, code_mission, heure_de_passage):
19         self.code_mission = code_mission
20         self.heure_de_passage = heure_de_passage
21     
22     def add_to_treestore(self, treestore):
23         print treestore
24         treestore.append(None, [self.code_mission, self.heure_de_passage])
25     
26     
27
28 class tableParser(HTMLParser.HTMLParser):
29     def __init__(self):
30         HTMLParser.HTMLParser.__init__(self)
31         self.table_horaires3 = False
32         self.code_de_mission = False
33         self.a_code_de_mission = False
34         self.heure_de_passage = False
35         self.liste_train = []
36         self.liste_horaire = []
37         self.list_ligne_horaire = []
38         
39     def handle_starttag(self, tag, attrs):
40         if (tag == 'table' and (dict(attrs)['class'] == 'horaires3')):
41             self.table_horaires3 = True
42         
43         elif self.table_horaires3 and tag == 'td':
44             try:
45                 self.code_de_mission = (
46                     dict(attrs)['headers'] == 'Code_de_mission')
47                 self.heure_de_passage = (
48                     dict(attrs)['headers'] == 'Heure_de_passage')
49             except KeyError:
50                 if dict(attrs).has_key('headers'):
51                     raise
52                 else:
53                     pass
54         else:
55             self.a_code_de_mission = (tag == 'a' and self.code_de_mission)
56         
57     def handle_data(self, data):
58         if self.a_code_de_mission:
59             self.liste_train.append(data.strip())
60         if self.heure_de_passage:
61             self.liste_horaire.append(data.strip())
62             
63     def handle_endtag(self,tag):
64         self.a_code_de_mission ^= (self.a_code_de_mission and tag == 'a')
65         self.heure_de_passage ^= (self.heure_de_passage and tag == 'td')
66     
67     def get_list_ligne_horaire(self):
68         print 'get_list_ligne_horaire'
69         z = 0
70         print self.liste_train
71         for i in self.liste_train:
72             self.list_ligne_horaire.append(LigneHoraire(code_mission=i, heure_de_passage=self.liste_horaire[z]))
73             z += 1
74         return self.list_ligne_horaire
75     
76     
77
78 class Trajet(object):
79     def __init__(self, gare_source, gare_dest):
80         self.gare_source = gare_source
81         self.gare_dest = gare_dest
82         self.parse() 
83     def get_liste_train(self):
84         return self.p.liste_train 
85     def get_liste_horaire(self): 
86         return self.p.liste_horaire
87     def parse(self):
88         self.p = tableParser()
89         #print "URL:" 
90         #print 'http://www.transilien.com/web/ITProchainsTrainsAvecDest.do?codeTr3aDepart='+self.gare_source.shortname+'&codeTr3aDest='+self.gare_dest.shortname+'&urlModule=/site/pid/184&gareAcc=true'
91         rsrc = urllib2.urlopen('http://www.transilien.com/web/ITProchainsTrainsAvecDest.do?codeTr3aDepart='+self.gare_source.shortname+'&codeTr3aDest='+self.gare_dest.shortname+'&urlModule=/site/pid/184&gareAcc=true')
92         self.p.feed(rsrc.read())
93         print "parsing ok"
94         
95     def refresh_treestore(self, treestore):
96         print 'refresh'
97         print treestore
98         
99         treestore.clear()
100         liste_ligne_horaire = self.p.get_list_ligne_horaire()
101         print liste_ligne_horaire
102         for i in liste_ligne_horaire:
103             print i
104             i.add_to_treestore(treestore)
105
106 class ConfFile(object):
107     def __init__(self, fichier):
108         self.c = ConfigParser.ConfigParser()
109         self.c.read(fichier)
110     def get_short_name(self, longname):
111         return self.c.get('ListeDesGares', longname)
112     def get_liste_des_gares(self):
113         return self.c.items('ListeDesGares') 
114
115 class LongNameGare(object):
116     def __init__(self, longname):
117         self.longname = longname
118     def get_gare(self, conffile):
119         short_name = conffile.get_short_name(self.longname)
120         return Gare(short_name)
121
122 class Gare(object):
123     def __init__(self, shortname):
124         self.shortname = shortname
125
126
127
128
129 class TransilienUI:
130     def __init__(self):
131         mainWindow = hildon.Window()
132         mainWindow.set_title("Horaires des Prochains Trains")
133         mainWindow.connect("destroy", self.on_mainWindow_destroy)
134
135         rotation_object = FremantleRotation(app_name, mainWindow, app_version, initial_mode)
136         refreshButton = hildon.Button(gtk.HILDON_SIZE_AUTO_WIDTH | gtk.HILDON_SIZE_FINGER_HEIGHT,
137                               hildon.BUTTON_ARRANGEMENT_HORIZONTAL, "Actualiser")
138         refreshButton.connect("clicked", self.on_refreshButton_clicked)
139
140         self.treestore = gtk.TreeStore(str, str)
141         self.treeview = gtk.TreeView(self.treestore)
142
143         self.tvcolumn_train = gtk.TreeViewColumn('Train', gtk.CellRendererText(), text=0)
144         self.treeview.append_column(self.tvcolumn_train)
145         
146         self.tvcolumn_horaire = gtk.TreeViewColumn('Horaire', gtk.CellRendererText(), text=1)
147         self.treeview.append_column(self.tvcolumn_horaire)
148
149
150         picker_button_source = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,                            hildon.BUTTON_ARRANGEMENT_VERTICAL)
151         picker_button_source.set_title("Gare de Depart")
152         self.combo_source = hildon.TouchSelectorEntry(text=True)
153         self.combo_dest = hildon.TouchSelectorEntry(text=True)
154
155         for i in ConfFile('example.cfg').get_liste_des_gares():
156             self.combo_source.append_text(i[0])
157             self.combo_dest.append_text(i[0])
158         picker_button_source.set_selector(self.combo_source)
159         
160
161         picker_button_dest = hildon.PickerButton(gtk.HILDON_SIZE_AUTO,                            hildon.BUTTON_ARRANGEMENT_VERTICAL)
162         picker_button_dest.set_title("Gare d'arrivee")
163         picker_button_dest.set_selector(self.combo_dest)
164         
165         vBox = gtk.VBox()
166         hBox = gtk.HBox()
167         vBox.pack_start(hBox)
168         hBox.pack_start(picker_button_source)
169         hBox.pack_start(picker_button_dest)
170         vBox.pack_start(self.treeview)
171         vBox.pack_start(refreshButton)
172         
173
174         mainWindow.add(vBox)
175         mainWindow.show_all()
176
177     def on_mainWindow_destroy(self, widget):
178         gtk.main_quit()
179
180     def on_refreshButton_clicked(self, widget):
181         gare_source = LongNameGare(self.combo_source.get_current_text()).get_gare(ConfFile('example.cfg'))
182         gare_dest = LongNameGare(self.combo_dest.get_current_text()).get_gare(ConfFile('example.cfg'))
183         trajet = Trajet(gare_source , gare_dest)
184         print trajet
185         print self.treestore
186         trajet.refresh_treestore(self.treestore)
187
188
189 if __name__ == "__main__":
190     TransilienUI()
191     gtk.main()