File: file_loader.py

package info (click to toggle)
openstructure 2.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 205,228 kB
  • sloc: cpp: 188,129; python: 35,361; ansic: 34,298; fortran: 3,275; sh: 286; xml: 146; makefile: 29
file content (196 lines) | stat: -rw-r--r-- 6,920 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#------------------------------------------------------------------------------
# This file is part of the OpenStructure project <www.openstructure.org>
#
# Copyright (C) 2008-2020 by the OpenStructure authors
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3.0 of the License, or (at your option)
# any later version.
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#------------------------------------------------------------------------------

from ost import gui
from ost import info
import ost
import re
from PyQt5 import QtCore, QtGui, QtNetwork
from ost.gui import FileLoader

"""
class BaseRemoteLoader(gui.RemoteSiteLoader):
  def __init__(self):
    gui.RemoteSiteLoader.__init__(self)
    self.networkmanager_=QtNetwork.QNetworkAccessManager()
    self.downloads_=dict()
    QtCore.QObject.connect(self.networkmanager_, QtCore.SIGNAL("finished(QNetworkReply*)"), self.DownloadFinished)

  def LoadById(self, id, selection=""):
    self.ById(id, selection)
  
  def ById(self, ids, selection=""):
    for id in re.findall('\w+', ids):
      file_name=self.GetFileName(id)
      file = QtCore.QFile(file_name)
      if(file.size()==0):
          url = QtCore.QUrl(self.GetUrl(id))
          request = QtNetwork.QNetworkRequest(url)
          reply = self.networkmanager_.get(request)
          self.downloads_[reply]=[id,selection]
      else:
        gui.FileLoader.LoadObject(str(file_name),str(selection))
    return None
    
  def IsImg(self):
    return False
    
  def IsDefault(self):
    return False

  def GetRemoteSiteName(self):
    pass  
  
  def GetUrl(self,id):
    pass
      
  def HandleError(self,message):
    pass
    
  def DownloadFinished(self, reply):
    file_name=self.GetFileName(self.downloads_[reply][0])
    file = QtCore.QFile(file_name)
    if(reply.error()!=QtNetwork.QNetworkReply.NoError or reply.bytesAvailable()==0):
      self.HandleError(reply.errorString());
      file.remove()
    else:
      if(self.downloads_.has_key(reply)):
        if(file.open(QtCore.QIODevice.WriteOnly)):
           file.write(reply.readAll());
        file.close();
        selection=self.downloads_[reply][1]
        del(self.downloads_[reply])
        gui.FileLoader.LoadObject(str(file_name),str(selection))

class GenericLoader(BaseRemoteLoader):
  EXT_NAME_ATTRIBUTE_NAME = "ExtName"
  URL_ATTRIBUTE_NAME = "Url"
  UP_CASE_ATTRIBUTE_NAME = "UpCase"
  FILE_TYPE_ATTRIBUTE_NAME = "FileType"
  TMP_URL_ATTRIBUTE_NAME= "TmpUrl"
  IMG_ATTRIBUTE_NAME= "Img"
  DEFAULT_ATTRIBUTE_NAME= "Default"
  def __init__(self, name, url, up_case, file_type, tmp_url=None, img=False, default=False):
    BaseRemoteLoader.__init__(self)
    self.name_ = QtCore.QString(name)
    self.url_ = QtCore.QString(url)
    self.up_case_ = up_case
    self.img_ = img
    self.default_ = default
    if file_type is not None:
      self.file_type_ = QtCore.QString(file_type)
    else:
      self.file_type_ = None
    
    if tmp_url is not None:
      self.tmp_url_ = QtCore.QString(tmp_url)
    else:
      self.tmp_url_ = tmp_url
    
  def IsImg(self):
    return self.img_    

  def IsDefault(self):
    return self.default_
  
  def GetRemoteSiteName(self):
    return self.name_
  
  def GetUrl(self, id):
    formatted_id = id
    if self.up_case_:
      formatted_id = QtCore.QString(id).toUpper()
    url = QtCore.QString(self.url_)
    url.replace("${ID}",formatted_id)
    return str(url)
    
  def GetFileName(self,id):
    formatted_id = id
    if self.up_case_:
      formatted_id = QtCore.QString(id).toUpper()
    
    url = None
    if self.tmp_url_ is not None:
      url = self.tmp_url_ + QtCore.QDir.separator()
    else:
      url = QtCore.QDir.tempPath() + QtCore.QDir.separator()
    
    file_type = None 
    if self.file_type_ is None:
      remote_url = QtCore.QString(self.GetUrl(id))
      index = remote_url.lastIndexOf(".")
      if(index >=0):
        file_type = remote_url.right(remote_url.size()-index-1)
    if file_type is None:
      file_type = self.file_type_
    if len(self.name_)>0:
      return url+formatted_id +"_"+self.name_+"."+file_type
    else:
      return url+formatted_id+"."+file_type
  
  def HandleError(self, message):
    ost.LogError(str("Could not download file\n"+message+""))
   
  def ToInfo(self,group):
    group.SetAttribute(GenericLoader.EXT_NAME_ATTRIBUTE_NAME, str(self.name_))
    group.SetAttribute(GenericLoader.URL_ATTRIBUTE_NAME, str(self.url_))
    if self.up_case_:
      group.SetAttribute(GenericLoader.UP_CASE_ATTRIBUTE_NAME, str(self.up_case_))
    if self.file_type_ is not None:
      group.SetAttribute(GenericLoader.FILE_TYPE_ATTRIBUTE_NAME, str(self.file_type_))
    if self.tmp_url_ is not None:
      group.SetAttribute(GenericLoader.TMP_URL_ATTRIBUTE_NAME, str(self.tmp_url_))
    if self.img_ is not None:
      group.SetAttribute(GenericLoader.IMG_ATTRIBUTE_NAME, str(int(self.img_)))
    if self.default_ is not None:
      group.SetAttribute(GenericLoader.DEFAULT_ATTRIBUTE_NAME, str(int(self.default_)))
      
  @staticmethod
  def FromInfo(group):
    name = None
    url = None
    up_case = False
    file_type = None
    tmp_url = None
    img = False
    default = False
    
    if group.HasAttribute(GenericLoader.EXT_NAME_ATTRIBUTE_NAME):
      name = QtCore.QString(group.GetAttribute(GenericLoader.EXT_NAME_ATTRIBUTE_NAME))
    
    if group.HasAttribute(GenericLoader.URL_ATTRIBUTE_NAME):
      url = QtCore.QString(group.GetAttribute(GenericLoader.URL_ATTRIBUTE_NAME))
      
    if group.HasAttribute(GenericLoader.UP_CASE_ATTRIBUTE_NAME):
      up_case = bool(group.GetAttribute(GenericLoader.UP_CASE_ATTRIBUTE_NAME))  
    
    if group.HasAttribute(GenericLoader.FILE_TYPE_ATTRIBUTE_NAME):
      file_type = QtCore.QString(group.GetAttribute(GenericLoader.FILE_TYPE_ATTRIBUTE_NAME))
      
    if group.HasAttribute(GenericLoader.TMP_URL_ATTRIBUTE_NAME):
      tmp_url = QtCore.QString(group.GetAttribute(GenericLoader.TMP_URL_ATTRIBUTE_NAME))
    
    if group.HasAttribute(GenericLoader.IMG_ATTRIBUTE_NAME):
      img = bool(int(group.GetAttribute(GenericLoader.IMG_ATTRIBUTE_NAME)))
      
    if group.HasAttribute(GenericLoader.DEFAULT_ATTRIBUTE_NAME):
      default = bool(int(group.GetAttribute(GenericLoader.DEFAULT_ATTRIBUTE_NAME)))

    return GenericLoader(name, url, up_case, file_type, tmp_url, img, default)
"""