OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
file_loader.py
Go to the documentation of this file.
1 #------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2011 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #------------------------------------------------------------------------------
19 
20 from ost import gui
21 from ost import info
22 import ost
23 import sip
24 import re
25 
26 from PyQt4 import QtCore, QtGui, QtNetwork
27 from ost.gui import FileLoader
28 
29 
31  def __init__(self):
32  gui.RemoteSiteLoader.__init__(self)
33  self.networkmanager_=QtNetwork.QNetworkAccessManager()
34  self.downloads_=dict()
35  QtCore.QObject.connect(self.networkmanager_, QtCore.SIGNAL("finished(QNetworkReply*)"), self.DownloadFinished)
36 
37  def LoadById(self, id, selection=""):
38  self.ById(id, selection)
39 
40  def ById(self, ids, selection=""):
41  for id in re.findall('\w+', ids):
42  file_name=self.GetFileName(id)
43  file = QtCore.QFile(file_name)
44  if(file.size()==0):
45  url = QtCore.QUrl(self.GetUrl(id))
46  request = QtNetwork.QNetworkRequest(url)
47  reply = self.networkmanager_.get(request)
48  self.downloads_[reply]=[id,selection]
49  else:
50  gui.FileLoader.LoadObject(str(file_name),str(selection))
51  return None
52 
53  def IsImg(self):
54  return False
55 
56  def IsDefault(self):
57  return False
58 
59  def GetRemoteSiteName(self):
60  pass
61 
62  def GetUrl(self,id):
63  pass
64 
65  def HandleError(self,message):
66  pass
67 
68  def DownloadFinished(self, reply):
69  file_name=self.GetFileName(self.downloads_[reply][0])
70  file = QtCore.QFile(file_name)
71  if(reply.error()!=QtNetwork.QNetworkReply.NoError or reply.bytesAvailable()==0):
72  self.HandleError(reply.errorString());
73  file.remove()
74  else:
75  if(self.downloads_.has_key(reply)):
76  if(file.open(QtCore.QIODevice.WriteOnly)):
77  file.write(reply.readAll());
78  file.close();
79  selection=self.downloads_[reply][1]
80  del(self.downloads_[reply])
81  gui.FileLoader.LoadObject(str(file_name),str(selection))
82 
84  EXT_NAME_ATTRIBUTE_NAME = "ExtName"
85  URL_ATTRIBUTE_NAME = "Url"
86  UP_CASE_ATTRIBUTE_NAME = "UpCase"
87  FILE_TYPE_ATTRIBUTE_NAME = "FileType"
88  TMP_URL_ATTRIBUTE_NAME= "TmpUrl"
89  IMG_ATTRIBUTE_NAME= "Img"
90  DEFAULT_ATTRIBUTE_NAME= "Default"
91  def __init__(self, name, url, up_case, file_type, tmp_url=None, img=False, default=False):
92  BaseRemoteLoader.__init__(self)
93  self.name_ = QtCore.QString(name)
94  self.url_ = QtCore.QString(url)
95  self.up_case_ = up_case
96  self.img_ = img
97  self.default_ = default
98  if file_type is not None:
99  self.file_type_ = QtCore.QString(file_type)
100  else:
101  self.file_type_ = None
102 
103  if tmp_url is not None:
104  self.tmp_url_ = QtCore.QString(tmp_url)
105  else:
106  self.tmp_url_ = tmp_url
107 
108  def IsImg(self):
109  return self.img_
110 
111  def IsDefault(self):
112  return self.default_
113 
114  def GetRemoteSiteName(self):
115  return self.name_
116 
117  def GetUrl(self, id):
118  formatted_id = id
119  if self.up_case_:
120  formatted_id = QtCore.QString(id).toUpper()
121  url = QtCore.QString(self.url_)
122  url.replace("${ID}",formatted_id)
123  return str(url)
124 
125  def GetFileName(self,id):
126  formatted_id = id
127  if self.up_case_:
128  formatted_id = QtCore.QString(id).toUpper()
129 
130  url = None
131  if self.tmp_url_ is not None:
132  url = self.tmp_url_ + QtCore.QDir.separator()
133  else:
134  url = QtCore.QDir.tempPath() + QtCore.QDir.separator()
135 
136  file_type = None
137  if self.file_type_ is None:
138  remote_url = QtCore.QString(self.GetUrl(id))
139  index = remote_url.lastIndexOf(".")
140  if(index >=0):
141  file_type = remote_url.right(remote_url.size()-index-1)
142  if file_type is None:
143  file_type = self.file_type_
144  if len(self.name_)>0:
145  return url+formatted_id +"_"+self.name_+"."+file_type
146  else:
147  return url+formatted_id+"."+file_type
148 
149  def HandleError(self, message):
150  ost.LogError(str("Could not download file\n"+message+""))
151 
152  def ToInfo(self,group):
153  group.SetAttribute(GenericLoader.EXT_NAME_ATTRIBUTE_NAME, str(self.name_))
154  group.SetAttribute(GenericLoader.URL_ATTRIBUTE_NAME, str(self.url_))
155  if self.up_case_:
156  group.SetAttribute(GenericLoader.UP_CASE_ATTRIBUTE_NAME, str(self.up_case_))
157  if self.file_type_ is not None:
158  group.SetAttribute(GenericLoader.FILE_TYPE_ATTRIBUTE_NAME, str(self.file_type_))
159  if self.tmp_url_ is not None:
160  group.SetAttribute(GenericLoader.TMP_URL_ATTRIBUTE_NAME, str(self.tmp_url_))
161  if self.img_ is not None:
162  group.SetAttribute(GenericLoader.IMG_ATTRIBUTE_NAME, str(int(self.img_)))
163  if self.default_ is not None:
164  group.SetAttribute(GenericLoader.DEFAULT_ATTRIBUTE_NAME, str(int(self.default_)))
165 
166  @staticmethod
167  def FromInfo(group):
168  name = None
169  url = None
170  up_case = False
171  file_type = None
172  tmp_url = None
173  img = False
174  default = False
175 
176  if group.HasAttribute(GenericLoader.EXT_NAME_ATTRIBUTE_NAME):
177  name = QtCore.QString(group.GetAttribute(GenericLoader.EXT_NAME_ATTRIBUTE_NAME))
178 
179  if group.HasAttribute(GenericLoader.URL_ATTRIBUTE_NAME):
180  url = QtCore.QString(group.GetAttribute(GenericLoader.URL_ATTRIBUTE_NAME))
181 
182  if group.HasAttribute(GenericLoader.UP_CASE_ATTRIBUTE_NAME):
183  up_case = bool(group.GetAttribute(GenericLoader.UP_CASE_ATTRIBUTE_NAME))
184 
185  if group.HasAttribute(GenericLoader.FILE_TYPE_ATTRIBUTE_NAME):
186  file_type = QtCore.QString(group.GetAttribute(GenericLoader.FILE_TYPE_ATTRIBUTE_NAME))
187 
188  if group.HasAttribute(GenericLoader.TMP_URL_ATTRIBUTE_NAME):
189  tmp_url = QtCore.QString(group.GetAttribute(GenericLoader.TMP_URL_ATTRIBUTE_NAME))
190 
191  if group.HasAttribute(GenericLoader.IMG_ATTRIBUTE_NAME):
192  img = bool(int(group.GetAttribute(GenericLoader.IMG_ATTRIBUTE_NAME)))
193 
194  if group.HasAttribute(GenericLoader.DEFAULT_ATTRIBUTE_NAME):
195  default = bool(int(group.GetAttribute(GenericLoader.DEFAULT_ATTRIBUTE_NAME)))
196 
197  return GenericLoader(name, url, up_case, file_type, tmp_url, img, default)