37,738
社区成员
发帖
与我相关
我的任务
分享import sys
import pygtk
import CloudSync -------------------------------------------> 这是与S3的交互模块
import face_1_test ------------------------------------------ >这是主界面模块
import gtk ------------------------------------------ >只有把这句话注释掉,才能交互成功,但是会导致界面无法显示
import xml.etree.ElementTree as ET
class landing_face:
def __init__(self):
builder=gtk.Builder()
builder.add_from_file("landing.glade")
self.landing_window=builder.get_object("landing_window")
#entry
self.entry_user=builder.get_object("entry_user")
self.entry_code=builder.get_object("entry_code")
self.entry_code.set_visibility(False)
#button
self.button_land=builder.get_object("button_land")
self.button_quit=builder.get_object("button_quit")
self.entry_code.connect("activate",self.on_landing) #combine <enter>
#connect_signals
connect_landing_face={"on_landing_face_destroy":gtk.main_quit,
"on_landing":self.on_landing,
"on_quit":self.on_quit
}
builder.connect_signals(connect_landing_face)
def on_landing(self,action):
string_user = self.entry_user.get_text()
string_code = self.entry_code.get_text()
query_id = "WKy3rMzOWPouVOxK1p3Ar1C2uRBwa2FBXnCw"
secret_key = "uKDWFs7Ul6OicBEZUE5sCYycnHcTedCMMsxqQ"
host = "10.3.1.124"
test = CloudSync.CloudSync(query_id, secret_key, True, host)
test.prepareSync()
landing_id = test.serviceID
landing_code = test.serviceKey
if string_user == landing_id and string_code == landing_code:
file_object = open('/python_object/onConfig_SyncFolder.xml')
try:
all_text = file_object.read()
finally:
file_object.close()
test.saveSyncConfig(file_object)
print "landing success!"
self.landing_window.hide()
edit = face_1_test.face_1()
edit.main_window.show()
else:
landing_dlg=gtk.Dialog("name or code wrong",self.landing_window,gtk.DIALOG_DESTROY_WITH_PARENT,(gtk.STOCK_OK,gtk.RESPONSE_ACCEPT))
label = gtk.Label("fail,please try again ")
landing_dlg.vbox.pack_start(label)
label.show()
response = landing_dlg.run()
if response == gtk.RESPONSE_ACCEPT:
landing_dlg.destroy()
self.entry_user.set_text("")
self.entry_code.set_text("")
print "please try again!"
def on_quit(self,action):
gtk.main_quit()
if __name__=="__main__":
edit = landing_face()
edit.landing_window.show()
gtk.main()
与S3交互的代码如下:
#!/usr/bin/python
import boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key
class CloudSync:
'''Represents a synchronization entity.'''
def __init__(self, serviceID, serviceKey, is_walrus=False, serviceEndpoint='s3.amazonaws.com'):
'''
Initializes the data.
'''
# service ID and Key used to obtain a s3/walrus connection
self.serviceID = serviceID
self.serviceKey = serviceKey
# the server address
self.serviceEndpoint = serviceEndpoint
# whether this connection is based on walrus
self.is_walrus = is_walrus
# the S3/Walrus Connection
self.connection = None
# Bucket Name, represents the bucket where the sync data is stored
self.bucketName = "neocloudsync_sun"
self.bucket = None
# User's device Name. All the user's content have prefix <deviceName>.
self.deviceName="device1"
# AutoSync's folder Name
self.folderName="neoAutoSync/"
# Config file Name(Stored on cloud storage server)
self.configFilename="neosync_config.xml"
# Content of configuration (List)
self.syncConfig = None
# the file name of the bookmark need to be synchronized
self.fileBookmark = None
# the file in sync operation
self.fileSyncCurrent = None
# list of files which need to be synchronized
self.fileSyncNeeded = None
# list of files which get synchronized
self.fileSyncDone = None
def prepareSync(self):
'''some preparation before sync'''
self.obtainConn()
self.checkSyncFolder()
def obtainConn(self):
'''
Get connection to Cloud Storage Server.
Called at initialization
'''
try:
if(self.is_walrus):
self.obtainConn_walrus()
else:
self.connection = S3Connection(self.serviceID, self.serviceKey)
except Exception as e:
print("Cannot get a connection to storage server. \nDetails: %s\n" % e)
#ccInfo-Done
def obtainConn_walrus(self):
'''
Used by self.getConnection() to get
connection to Walrus Cloud Storage Server.
'''
calling_format=boto.s3.connection.OrdinaryCallingFormat()
self.connection = boto.s3.connection.S3Connection(aws_access_key_id=self.serviceID,
aws_secret_access_key=self.serviceKey,
is_secure=False,
host=self.serviceEndpoint,
port=8773,
calling_format=calling_format,
path="/services/Walrus")
#ccInfo-Done
def checkSyncFolder(self):
'''
Called at initialization to ensure the existence of sync bucket and folder
Check the bucket and folder used by neoShine cloud sync system,
if the required bucket and folder do not exist, try to create them,
if something error, return False else return True
'''
try:
if(not self.bucketName.islower()):
print("Bucket name Uppercase not allowed, convert to lower")
self.bucketName = self.bucketName.lower()
b = self.connection.lookup(self.bucketName)
except Exception as e:
print("Error.\nDetails: %s" % e)
if(b==None):
print("bucket does not exits, trying to create it")
try:
self.bucket = self.connection.create_bucket(self.bucketName)
print("Bucket created successfully")
except Exception as e:
print("Cannot create a bucket.\nDetails: %s" % e)
return False
else:
self.bucket = b
print("bucket name is: %s" % b)
# create the sync folder
k = Key(self.bucket)
k.key = self.folderName
try:
k.set_contents_from_string('')
return True
except Exception as e:
print("Cannot create folder for sync: %s.\n Details: %s" % (fileName,e))
return False
#ccInfo-Done
def getSyncStatus(self):
try:
f = self.bucket.get_all_keys()
for ff in f:
#s = ff.get_metadata('md5-hash')
print ff
except Exception as e:
print("Cannot get the bucket. \nDetails: %s\n" % e)
def zz_addSyncItems(self):
print("This is addSyncItems!")
def zz_removeSyncItems():
print("This is removeSyncItems!")
def getSyncConfig(self):
'''
Get sync config file from cloud storage server to a local string
'''
k = Key(self.bucket) # Specify which bucket to use
k.key = self.configFilename # The key's name
try:
str = k.get_contents_as_string()
self.syncConfiguration = str
return str
#print("The content of Config file is: %s, size: %s" % (str,k.size))
###########################
# Call self.parseSyncFiles
###########################
#return self.syncConfiguration
except Exception as e:
print("Cannot get file: %s\n Details: %s" % (k.key, e))
def saveSyncConfig(self, configStr):
'''
Save sync config string to cloud storage server
'''
k = Key(self.bucket) # Specify which bucket to use
k.key = self.configFilename # The key's name
try:
k.set_contents_from_string(configStr)
except Exception as e:
print("Cannot save to Server!\n Details: %s" % (e))
return 1
return 0
def zz_sync2Server():
print("This is sync2Server!")
def __del__(self):
'''I am dying.'''
print("This is the del method")
# Below is some methods for Debug
def sysInfo(self):
if(self.is_walrus):
print("This is a Walrus Connection!")
else:
print("This is a S3 Connection!")报的错误如下显示: