Browse code

* S3/CloudFront.py: Cmd._get_dist_name_for_bucket() moved to CloudFront class.

git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/trunk@470 830e0280-6d2a-0410-9c65-932aecc39d9d

Michal Ludvig authored on 2011/03/30 18:29:26
Showing 2 changed files
... ...
@@ -1,3 +1,8 @@
1
+2011-03-30  Michal Ludvig  <mludvig@logix.net.nz>
2
+
3
+	* S3/CloudFront.py: Cmd._get_dist_name_for_bucket() moved to
4
+	  CloudFront class.
5
+
1 6
 2011-01-13  Michal Ludvig  <mludvig@logix.net.nz>
2 7
 
3 8
 	* s3cmd, S3/FileLists.py: Move file/object listing functions
... ...
@@ -190,6 +190,7 @@ class CloudFront(object):
190 190
 
191 191
 	## Maximum attempts of re-issuing failed requests
192 192
 	_max_retries = 5
193
+	dist_list = None
193 194
 
194 195
 	def __init__(self, config):
195 196
 		self.config = config
... ...
@@ -388,6 +389,21 @@ class CloudFront(object):
388 388
 		# Wait a few seconds. The more it fails the more we wait.
389 389
 		return (self._max_retries - retries + 1) * 3
390 390
 
391
+	def get_dist_name_for_bucket(self, uri):
392
+		if (uri.type == "cf"):
393
+			return uri
394
+		if (uri.type != "s3"):
395
+			raise ParameterError("CloudFront or S3 URI required instead of: %s" % arg)
396
+
397
+		debug("_get_dist_name_for_bucket(%r)" % uri)
398
+		if CloudFront.dist_list is None:
399
+			response = self.GetList()
400
+			CloudFront.dist_list = {}
401
+			for d in response['dist_list'].dist_summs:
402
+				CloudFront.dist_list[getBucketFromHostname(d.info['Origin'])[0]] = d.uri()
403
+			debug("dist_list: %s" % CloudFront.dist_list)
404
+		return CloudFront.dist_list[uri.bucket()]
405
+
391 406
 class Cmd(object):
392 407
 	"""
393 408
 	Class that implements CloudFront commands
... ...
@@ -408,34 +424,17 @@ class Cmd(object):
408 408
 			setattr(Cmd.options, option, value)
409 409
 
410 410
 	options = Options()
411
-	dist_list = None
412
-
413
-	@staticmethod
414
-	def _get_dist_name_for_bucket(uri):
415
-		cf = CloudFront(Config())
416
-		debug("_get_dist_name_for_bucket(%r)" % uri)
417
-		assert(uri.type == "s3")
418
-		if Cmd.dist_list is None:
419
-			response = cf.GetList()
420
-			Cmd.dist_list = {}
421
-			for d in response['dist_list'].dist_summs:
422
-				Cmd.dist_list[getBucketFromHostname(d.info['Origin'])[0]] = d.uri()
423
-			debug("dist_list: %s" % Cmd.dist_list)
424
-		return Cmd.dist_list[uri.bucket()]
425 411
 
426 412
 	@staticmethod
427 413
 	def _parse_args(args):
414
+		cf = CloudFront(Config())
428 415
 		cfuris = []
429 416
 		for arg in args:
430
-			uri = S3Uri(arg)
431
-			if uri.type == 's3':
432
-				try:
433
-					uri = Cmd._get_dist_name_for_bucket(uri)
434
-				except Exception, e:
435
-					debug(e)
436
-					raise ParameterError("Unable to translate S3 URI to CloudFront distribution name: %s" % uri)
437
-			if uri.type != 'cf':
438
-				raise ParameterError("CloudFront URI required instead of: %s" % arg)
417
+			try:
418
+				uri = cf.get_dist_name_for_bucket(S3Uri(arg))
419
+			except Exception, e:
420
+				debug(e)
421
+				raise ParameterError("Unable to translate S3 URI to CloudFront distribution name: %s" % arg)
439 422
 			cfuris.append(uri)
440 423
 		return cfuris
441 424