adskHttpSchemeResolver.py

adskHttpSchemeResolver.py
1 """
2 Custom file resolver derived from MPxFileResolver that handles
3 the URI 'htpp' scheme.
4 When this resolver is active, URI file paths using the 'http://<domain>/'
5 scheme will be processed using methods on this class.
6 Refer to MPxFileResolver for more information about custom file resolvers.
7 
8 To use, make sure that adskHttpSchemeResolver.py is in
9 your MAYA_PLUG_IN_PATH then do the following:
10 
11 # Load the plug-in
12 import maya.cmds
13 maya.cmds.loadPlugin("adskHttpSchemeResolver.py")
14 # Once loaded, Maya will call the resolver methods in this plug-in when
15 # a URI file path is encountered during file resolution processing
16 # (file open, setAttr, etc.)
17 # Unload the plug-in
18 maya.cmds.unloadPlugin("adskHttpSchemeResolver")
19 # Maya will no longer have access to this file
20 # resolver to handle URI file paths using the 'http:///' scheme
21 
22 """
23 
24 import sys
25 import maya.OpenMaya as OpenMaya
26 import maya.OpenMayaMPx as OpenMayaMPx
27 import maya.cmds as cmds
28 import urllib
29 import os.path
30 
31 
32 #Specify the location for downloaded file
33 kTempDir = cmds.internalVar(userTmpDir=True)
34 
35 # Set status messages on or off
36 kWantStatusOutput = True;
37 
38 def downloadProgress(data, dataSize, fileSize):
39  """ Display download progress - callback invoked by urllib.urlretrieve """
40  percent = 100.0*data*dataSize/fileSize
41  if percent > 100:
42  percent = 100
43  printStatus('Download progress: %.2f%%' % (percent))
44 
45 def printStatus(msg):
46  """ Print status output for diagnostic purposes (when enabled) """
47  if (kWantStatusOutput):
48  sys.stderr.write('%s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
49 
50 def printWarning(msg):
51  """ Print warning messages """
52  sys.stderr.write('Warning %s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
53 
54 
55 
56 # 'http' scheme resolver
57 class adskHttpSchemeResolver(OpenMayaMPx.MPxFileResolver):
58  """
59  This custom plug-in resolver handles the 'http' uri scheme.
60  This resolver will copy the file from its 'http' location
61  using standard url library to a temporary location. This temporary location
62  of the file is the fully qualified resolved path.
63  It also implements a crude caching system.
64  """
65 
66  kPluginURIScheme = "http"
67  kPluginResolverName = "adskHttpSchemeResolver"
68 
69  def __init__(self):
70  OpenMayaMPx.MPxFileResolver.__init__(self)
71 
72  def uriScheme(self):
73  return(self.kPluginURIScheme)
74 
75  def resolveURI(self,URI,mode):
76  # Determine temporary location that will be used for this file
77  uri = URI.asString()
78  tempFile = kTempDir + URI.getFileName()
79  result = u''
80 
81  # Check resolver mode, since not all modes require the file
82  # to be downloaded.
83 
84  # When mode is kNone, simply return the resolved path
85  if mode & OpenMayaMPx.MPxFileResolver.kNone:
86  result = tempFile;
87 
88  # When mode is kInput, Maya is expecting to use this
89  # file for input. Download the file to the temporary storage
90  # area if it hasn't already been done.
91  elif mode & OpenMayaMPx.MPxFileResolver.kInput:
92  if not os.path.exists(tempFile):
93  # If the file does not exist in the cache then go and
94  # download it. At this point we assume we have a well
95  # formed URI and that it exists on the web somewhere
96  # Any error here and the resolved file is simply not
97  # found. Would need to code for all the errors that
98  # could go wrong here in a production case (lost
99  # connections, server down, etc.)
100  printStatus('Downloading URI: %s to location: %s' % (uri, tempFile))
101 
102  data = urllib.urlretrieve(uri, tempFile, downloadProgress)
103  if os.path.exists(tempFile):
104  printStatus('Download complete')
105  else:
106  printWarning('Download failed for URI: %s to location: %s'
107  % (uri, tempFile))
108  result = tempFile
109  else:
110  printStatus('Download skipped, using cached version of URI: %s at location: %s'
111  % (uri, tempFile))
112  result = tempFile
113 
114  # Unexpected mode - simply return the resolved path
115  else:
116  printWarning('Unexpected resolve mode encountered: %s' % str(mode))
117  result = tempFile
118 
119  # Return the resolved path
120  return result
121 
122  def performAfterSaveURI(self,URI,resolvedFullPath):
123  uri = URI.asString()
124  printStatus('Uploading local file %s to URI location %s'
125  % (resolvedFullPath, uri))
126 
127  @staticmethod
128  # Creator for the proxy instance
129  def theCreator():
130  return OpenMayaMPx.asMPxPtr( adskHttpSchemeResolver() )
131 
132  @staticmethod
133  def className():
134  return 'adskHttpSchemeResolver'
135 
136 
137 
138 # Initialize the script plug-in
139 def initializePlugin(plugin):
140  pluginFn = OpenMayaMPx.MFnPlugin(plugin)
141  try:
142  pluginFn.registerURIFileResolver( adskHttpSchemeResolver.kPluginResolverName,
143  adskHttpSchemeResolver.kPluginURIScheme,
144  adskHttpSchemeResolver.theCreator )
145  except:
146  sys.stderr.write( "Failed to register custom resolver: %s for scheme: %s\n" %
147  (adskHttpSchemeResolver.kPluginResolverName,
148  adskHttpSchemeResolver.kPluginURIScheme ))
149  raise
150 
151 # Uninitialize the script plug-in
152 def uninitializePlugin(plugin):
153  pluginFn = OpenMayaMPx.MFnPlugin(plugin)
154  try:
155  pluginFn.deregisterURIFileResolver(adskHttpSchemeResolver.kPluginResolverName)
156  except:
157  sys.stderr.write(
158  "Failed to deregister custom file resolver: %s\n" %
159  adskHttpSchemeResolver.kPluginResolverName)
160  raise
161 
162 #-
163 # ==========================================================================
164 # Copyright (C) 2012 Autodesk, Inc. and/or its licensors. All
165 # rights reserved.
166 #
167 # The coded instructions, statements, computer programs, and/or related
168 # material (collectively the "Data") in these files contain unpublished
169 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
170 # licensors, which is protected by U.S. and Canadian federal copyright
171 # law and by international treaties.
172 #
173 # The Data is provided for use exclusively by You. You have the right
174 # to use, modify, and incorporate this Data into other products for
175 # purposes authorized by the Autodesk software license agreement,
176 # without fee.
177 #
178 # The copyright notices in the Software and this entire statement,
179 # including the above license grant, this restriction and the
180 # following disclaimer, must be included in all copies of the
181 # Software, in whole or in part, and all derivative works of
182 # the Software, unless such copies or derivative works are solely
183 # in the form of machine-executable object code generated by a
184 # source language processor.
185 #
186 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
187 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
188 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
189 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
190 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
191 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
192 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
193 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
194 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
195 # OR PROBABILITY OF SUCH DAMAGES.
196 #
197 # ==========================================================================
198 #+