rass_quicktest.py

#!/usr/bin/python
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##
#  Demo for using the RASS python client library.

import time
import rawsc
import rawsc.service
from rawsc import rass
import rawsc.rass.service
try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

USER = "XXX"
PWD = "XXX"
SERVER = "rass.cdn01.rambla.be"

TGT_FILENAME = "white_noise.mp4"
LOCAL_FILE = "../test_resources/bla.mp4"
DOWNLOADS_DIR = "path_to_writable_dir"

try:
    # Create a Rass instance, passing it your login credentials and the base service uri
    client = rawsc.rass.service.RassService(username=USER, password=PWD, server=SERVER)

    # get list of dirs
    qry = rawsc.Query()
    qry["kind"] = "dir"
    feed = client.getDirFeed(uri = "/dir/", query = qry)
    print "Retrieved feed of directories (kind=dir):"
    for e in feed.entry:
        print "\nDir Entry URI: " + str(e.id.text)
        print "name: " + str(e.content.params.name.text)
        print "size: " + str(e.content.params.size.text)
        print "updated: " + str(e.content.params.updated.text)

    # create new dir
    new_dir_entry = client.createDir(path = "abas9")
    print "\nCreated new dir: %s" % new_dir_entry.id.text
    print "Dir kind attr: %s" % new_dir_entry.kind
    print "Dir path attr: %s" % new_dir_entry.path

    # create new file, by PUTting it (creation fails if file already exists)
    new_file_entry = client.createItem(path = new_dir_entry.content.params.name.text + "/" + TGT_FILENAME + "/", local_path = LOCAL_FILE)
    print "\nCreated new item: %s" % new_file_entry.id.text
    print "Item kind attr: %s" % new_file_entry.kind
    print "Item path attr: %s" % new_file_entry.path

    # create new file, by POSTing it (creation succeeds if file already exists: filename is suffixed)
    new_file_entry = client.postItem(dirpath = new_dir_entry.content.params.name.text, filename = TGT_FILENAME, local_path = LOCAL_FILE)
    print "\nCreated new item: %s" % new_file_entry.id.text
    print "Item kind attr: %s" % new_file_entry.kind
    print "Item path attr: %s" % new_file_entry.path

    # download all files from a dir
    qry = rawsc.Query()
    qry["kind"] = "file"
    feed = client.getDirFeedFromPath(query = qry, path = new_dir_entry.path)
    print "\nRetrieved feed of items: %s" % feed.id.text
    for e in feed.entry:
        if rawsc.get_enclosure_link(e): # if the entry refers to a file, the enclosure link will contain the file location (= URL) on the CDN
            client.getItemFile(uri = rawsc.get_enclosure_link(e), local_path = DOWNLOADS_DIR + e.content.params.name.text )
            print "downloaded item (%s) to local path = %s" % (e.id.text, DOWNLOADS_DIR + e.content.params.name.text)

    # delete file
    client.delete(uri = new_file_entry.id.text)
    print "\nDeleted item = %s" % new_file_entry.path
    
    # delete dir
    client.delete(uri = new_dir_entry.id.text)
    print "\nDeleted dir = %s" % new_dir_entry.path

except rawsc.service.Error, e:
    print "Error Response from server, contents = %s.\n" % str(e)
    error_dict = e[0] # error dict inside the first tuple elem
    print "status code : " + str(error_dict["status"]) + "\n"
    if error_dict.has_key("body"):
        print "error body : " + error_dict["body"] + "\n"
except rawsc.RawscException, e:
    print "RawscException caught, reason = %s.\n" % str(e)
except StandardError, e:
    print "StandardError caught, reason = %s.\n" % str(e)
except:
    print "unhandled exception caught\n"


Generated on Mon Mar 28 15:03:39 2011 for rawsc by  doxygen 1.5.3