S3SyncClient.py :  » RSS » PenguinTV » PenguinTV-4.1.0 » penguintv » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » RSS » PenguinTV 
PenguinTV » PenguinTV 4.1.0 » penguintv » S3SyncClient.py
import logging
import threading

import gettext
_=gettext.gettext

from SqliteSyncClient import SqliteSyncClient
from amazon import S3

#BUCKET_NAME will be prepended with access key
BUCKET_NAME_SUF = '-penguintv-article-sync-db'
KEYNAME = 'penguintv-syncdb-1'
STAMP_KEYNAME = 'penguintv-syncdb-timestamp'

class S3SyncClient(SqliteSyncClient):
  def __init__(self):
    SqliteSyncClient.__init__(self)
    self.__transfer_lock = threading.Lock()
    
    self._username = ""
    self._bucket = self._username.lower() + BUCKET_NAME_SUF
    self._conn = None
    
  def get_parameters(self):
    return [
      (_("Access Key"), "username", "", False),
      (_("Secret Key"), "password", "", True)
      ]
    
  def set_username(self, username):
    SqliteSyncClient.set_username(self, username)
    self._bucket = self._username.lower() + BUCKET_NAME_SUF

  def _do_authenticate(self):
    self._conn = S3.AWSAuthConnection(self._username, self._password)
    #the only way to "authenticate" is to list buckets.  if list is
    #empty, try creating the bucket.  success?  it worked!  failure? 
    #bad keys
    
    buckets = [x.name for x in self._conn.list_all_my_buckets().entries]
    if len(buckets) > 0:
      if self._bucket not in buckets:
        #try creating our bucket
        response = \
           self._conn.create_located_bucket(self._bucket, S3.Location.DEFAULT)
        if response.http_response.status == 200:
          return True
        else:
          return False
      else:
        return True
    
    response = \
         self._conn.create_located_bucket(self._bucket, S3.Location.DEFAULT)

    if response.http_response.status == 200:
      return True
    else:
      return False
      
  def _set_server_timestamp(self, timestamp):
    assert self._authenticated
    #logging.debug("TIMESTAMPING SUBMISSION: %i" % timestamp)
    resp = self._conn.put(self._bucket, STAMP_KEYNAME, str(timestamp))
    if resp.http_response.status != 200:
      logging.error("error submitting timestamp")
      return False
    return True
    
  def _get_server_timestamp(self):
    assert self._authenticated
    self.__transfer_lock.acquire()
    resp = self._conn.get(self._bucket, STAMP_KEYNAME)
    if resp.http_response.status == 404:
      self._conn.put(self._bucket, STAMP_KEYNAME, "0")
      self.__transfer_lock.release()
      return 0
    elif resp.http_response.status != 200:
      logging.error("couldn't get last submit time: %s" % resp.message)
      self.__transfer_lock.release()
      return 0
    self.__transfer_lock.release()
    
    return int(resp.object.data)

  def _db_exists(self):
    response = self._conn.list_bucket(self._bucket)
    if response.http_response.status != 200:
      return False
    
    for entry in response.entries:
      if entry.key == KEYNAME:
        return True
    return False
    
  def _do_download_db(self):
    self.__transfer_lock.acquire()
    response = self._conn.get(self._bucket, KEYNAME)
    self.__transfer_lock.release()
    if response.http_response.status != 200:
      return None
    return response.object.data

  def _upload_db(self, fp):
    self.__transfer_lock.acquire()
    response = self._conn.put(self._bucket, KEYNAME, fp.read())
    self.__transfer_lock.release()
    return response.http_response.status == 200
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.