예제 #1
0
    def __init__(self):

        self.opener = urllib2.build_opener(
            shttp.StreamingHTTPHandler(), shttp.StreamingHTTPRedirectHandler(),
            shttp.StreamingHTTPSHandler(), urllib2.HTTPCookieProcessor())
        self.opener.addheaders = [('User-agent', 'openplm')]
        self.username = ""
        self.desktop = None
        self.documents = {}
        self.disable_menuitems()

        data = self.get_conf_data()
        if "server" in data:
            type(self).SERVER = data["server"]

        try:
            os.makedirs(self.PLUGIN_DIR, 0700)
        except os.error:
            pass
예제 #2
0
    def __init__(self, plugin, window):
        self._window = window
        self._plugin = plugin
        self._activate_id = 0
        
        self.opener = urllib2.build_opener(shttp.StreamingHTTPHandler(),
                                           shttp.StreamingHTTPRedirectHandler(),
                                           shttp.StreamingHTTPSHandler(),
                                           urllib2.HTTPCookieProcessor())
        self.opener.addheaders = [('User-agent', 'openplm')]
        self.username = ""

        self.insert_menu()
        self.update()

        try:
            os.makedirs(self.PLUGIN_DIR, 0700)
        except os.error:
            pass
예제 #3
0
  def write(self, content=None, blob=None, fp=None, mime_type=None, meta=None,
            **kwargs):
    """Writes contents to a file.

    Args:
      content: A byte string representing the contents of the file.
      blob: If content is not provided, a BlobKey pointing to the file.
      fp: If no content and no blob, pass an already-open file pointer.
          This file will be uploaded as multipart data directly to the
          blobstore and this File's blob keys will be automatically populated.
          This arg must be used instead of content for files over ~10 MB.
      mime_type: The MIME type of the file. If not provided, then the MIME type
          will be guessed based on the filename.
      meta: A dict of meta key-value pairs.
      **kwargs: Additional keyword args to be encoded in the request params.
    """
    self._file_data = None

    params = [('path', self.path)]
    if content is not None:
      params.append(('content', content))
    if blob is not None:
      params.append(('blob', blob))
    if meta is not None:
      params.append(('meta', json.dumps(meta)))
    if mime_type is not None:
      params.append(('mime_type', mime_type))

    # Extra keyword arguments.
    if self._file_kwargs:
      params.append(('file_params', json.dumps(self._file_kwargs)))
    if kwargs:
      params.append(('method_params', json.dumps(kwargs)))

    try:
      if fp:
        # If given a file pointer, create a multipart encoded request.
        path = FILE_API_PATH_BASE + FILE_NEWBLOB_API
        write_blob_url = self._titan_client.fetch_url(path).content
        params.append(('file', fp))
        content_generator, headers = encode.multipart_encode(params)
        # Make custom opener to support multipart POST requests.
        opener = urllib2.build_opener()
        opener.add_handler(streaminghttp.StreamingHTTPHandler())
        opener.add_handler(streaminghttp.StreamingHTTPSHandler())
        # Upload directly to the blobstore URL, avoiding authentication.
        request = urllib2.Request(write_blob_url, data=content_generator,
                                  headers=headers)
        response = opener.open(request)
        # Pull the blobkey out of the query params and fall through
        # to the POST to /_titan/file.
        url = response.geturl()
        response_params = urlparse.parse_qs(urlparse.urlparse(url).query)

        # Verify that "blob" was not passed in.
        assert not blob
        params.append(('blob', response_params['blob'][0]))
      path_param = {'path': self.path}
      url = '%s?%s' % (FILE_API_PATH_BASE, urllib.urlencode(path_param))
      payload = urllib.urlencode(params)
      response = self._titan_client.fetch_url(url, method='POST',
                                              payload=payload)
      self._verify_response(response)
    except urllib2.HTTPError, e:
      if e.code == 404:
        raise BadRemoteFileError('File does not exist: %s' % self.path)
      raise