Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #37 from sopel/issue-36-fix-pylint-violations
Browse files Browse the repository at this point in the history
Fix Pylint violations

[Closes #36]
  • Loading branch information
sopel committed Apr 15, 2013
2 parents cd7e864 + acca89b commit f488ac5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
19 changes: 12 additions & 7 deletions botocross/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def create_arn(iam, service, region, resource):
account = accountInfo.describe()
return 'arn:aws:' + service + ':' + region + ':' + account.id + ':' + resource

# TODO: refactor to argparse custom action for inline usage!
# REVIEW: refactor to argparse custom action for inline usage?!
def build_filter(filter_args, exclude_args):
return {'filters': build_filter_params(filter_args), 'excludes': build_filter_params(exclude_args)}

Expand All @@ -66,7 +66,7 @@ def build_filter_params(filter_args):

def build_common_parser():
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-v", "--verbose", action='store_true') # TODO: drop in favor of a log formatter?!
parser.add_argument("-v", "--verbose", action='store_true') # REVIEW: drop in favor of a log formatter?!
parser.add_argument("--access_key_id", dest='aws_access_key_id', help="Your AWS Access Key ID")
parser.add_argument("--secret_access_key", dest='aws_secret_access_key', help="Your AWS Secret Access Key")
parser.add_argument("-l", "--log", dest='log_level', default='WARNING',
Expand All @@ -81,10 +81,14 @@ def build_region_parser():

def build_filter_parser(resource_name, add_ids=True):
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-f", "--filter", action="append", help="A {0} filter. [can be used multiple times]".format(resource_name))
parser.add_argument("-x", "--exclude", action="append", help="A {0} filter (matching ones are excluded). [can be used multiple times]".format(resource_name))
parser.add_argument("-f", "--filter", action="append",
help="A {0} filter. [can be used multiple times]".format(resource_name))
parser.add_argument("-x", "--exclude", action="append",
help="A {0} filter (matching ones are excluded). [can be used multiple times]"
.format(resource_name))
if add_ids:
parser.add_argument("-i", "--id", dest="resource_ids", action="append", help="A {0} id. [can be used multiple times]".format(resource_name))
parser.add_argument("-i", "--id", dest="resource_ids", action="append",
help="A {0} id. [can be used multiple times]".format(resource_name))
return parser

def parse_credentials(args):
Expand All @@ -103,17 +107,18 @@ def filter_regions(regions, region):
regions = filter(lambda x: is_region_selected(x, region), regions)
return regions

# TODO: remove this S3 legacy induced partial duplication, if possible.
# REVIEW: remove this S3 legacy induced partial duplication, if possible.
def is_region_selected_s3(region, name):
from botocross.s3 import RegionMap
return True if RegionMap[region].find(name) != -1 else False

# TODO: remove this S3 legacy induced partial duplication, if possible.
# REVIEW: remove this S3 legacy induced partial duplication, if possible.
def filter_regions_s3(regions, region):
if region:
botocross_log.info("... (filtered by S3 region '" + region + "')")
regions = filter(lambda x: is_region_selected_s3(x, region), regions)
return regions

# pylint: disable=R0903
class ExitCodes:
(OK, FAIL) = range(0, 2)
8 changes: 5 additions & 3 deletions botocross/ec2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ def expire_snapshots(ec2, volume_ids, backup_set, backup_retention, no_origin_sa
snapshot_filters['description'] = CREATED_BY_BOTO_EBS_SNAPSHOT_SCRIPT_SIGNATURE + volume_id
log.debug(snapshot_filters)
snapshots = ec2.get_all_snapshots(owner='self', filters=snapshot_filters)
log.info("Deleting snapshots of " + volume_id + " (set '" + backup_set + "', " + str(len(snapshots)) + " available, retaining " + str(backup_retention) + "):")
log.info("Deleting snapshots of " + volume_id + " (set '" + backup_set + "', " + str(len(snapshots)) +
" available, retaining " + str(backup_retention) + "):")
# While snapshots are apparently returned in oldest to youngest order, this isn't documented;
# therefore an explicit sort is performed to ensure this regardless.
num_snapshots = len(snapshots);
num_snapshots = len(snapshots)
for snapshot in sorted(snapshots, key=attrgetter('start_time')):
log.debug(snapshot.start_time)
if num_snapshots <= backup_retention:
Expand Down Expand Up @@ -101,7 +102,8 @@ def expire_images(ec2, instance_ids, backup_set, backup_retention, no_origin_saf
image_filters['description'] = CREATED_BY_BOTO_EC2_IMAGE_SCRIPT_SIGNATURE + instance_id
log.debug(image_filters)
images = ec2.get_all_images(owners=['self'], filters=image_filters)
log.info("Deregistering images of " + instance_id + " (set '" + backup_set + "', " + str(len(images)) + " available, retaining " + str(backup_retention) + "):")
log.info("Deregistering images of " + instance_id + " (set '" + backup_set + "', " + str(len(images)) +
" available, retaining " + str(backup_retention) + "):")
# While images are apparently returned in oldest to youngest order, this isn't documented;
# therefore an explicit sort is performed to ensure this regardless.
num_images = len(images)
Expand Down
18 changes: 8 additions & 10 deletions botocross/iam/accountinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import botocross.iam
import logging

# pylint: disable=R0903
class AccountInfo:
"""
Represents an AWS Account
Expand All @@ -31,15 +32,14 @@ class AccountInfo:
def __init__(self, iam_connection):
self.connection = iam_connection
self.log = logging.getLogger('boto_cli.iam.AccountInfo')
self.user = None
# populate those attributes not leaked via the exception, if user has no permission for iam:ListAccountAliases
self.alias = botocross.iam.RESOURCE_UNAUTHORIZED
self.id = None

def __repr__(self):
return '<AccountInfo - alias:%s id:%s>' % (self.alias, self.id)

def describe(self, user=None):
self.account = {}
try:
alias = self.connection.get_account_alias()
aliases = alias['list_account_aliases_response']['list_account_aliases_result']['account_aliases']
Expand All @@ -55,13 +55,12 @@ def describe(self, user=None):
raise
self.log.debug(e.error_message)
try:
# TODO: there should be a better way to retrieve the account id, which is 'leaked in the exception anyway
# REVIEW: there should be a better way to retrieve the account id, which is 'leaked in the exception anyway
# eventually; see http://stackoverflow.com/questions/10197784 for a respective question.
if not self.user:
from userinfo import UserInfo
userInfo = UserInfo(self.connection)
self.user = userInfo.describe()
self.id = self.user.arn.replace('arn:aws:iam::', '').partition(':')[0]
if not user:
userInfo = botocross.iam.userinfo.UserInfo(self.connection)
user = userInfo.describe()
self.id = user.arn.replace('arn:aws:iam::', '').partition(':')[0]
except boto.exception.BotoServerError, e:
# NOTE: given some information can be deduced from the exception still, the lack of permissions is
# considered a normal condition still and the exception handled/logged accordingly.
Expand All @@ -77,7 +76,6 @@ def describe(self, user=None):
try:
iam = boto.connect_iam()
accountInfo = AccountInfo(iam)
account = accountInfo.describe()
print account
print accountInfo.describe()
except boto.exception.BotoServerError, e:
logging.exception(e.error_message)
25 changes: 14 additions & 11 deletions botocross/iam/userinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import botocross.iam
import logging

# pylint: disable=R0902,R0903
class UserInfo:
"""
Represents an AWS User
Expand All @@ -34,20 +35,23 @@ def __init__(self, iam_connection):
# populate those attributes not leaked via the exception, if user has no permission for iam:GetUser
self.path = botocross.iam.RESOURCE_UNAUTHORIZED
self.create_date = botocross.iam.RESOURCE_UNAUTHORIZED
self.id = botocross.iam.RESOURCE_UNAUTHORIZED # TODO: could be deduced from credentials in use instead.
self.id = botocross.iam.RESOURCE_UNAUTHORIZED # REVIEW: could be deduced from credentials in use instead.
self.arn = None
self.name = None

def __repr__(self):
return '<UserInfo - path:%s create_date:%s id:%s arn:%s name:%s>' % (self.path, self.create_date, self.id, self.arn, self.name)
template = '<UserInfo - path:%s create_date:%s id:%s arn:%s name:%s>'
return template % (self.path, self.create_date, self.id, self.arn, self.name)

def describe(self):
try:
user = self.connection.get_user()
self.user = user['get_user_response']['get_user_result']['user']
self.path = self.user['path']
self.create_date = self.user['create_date']
self.id = self.user['user_id']
self.arn = self.user['arn']
self.name = self.user['user_name']
response = self.connection.get_user()
user = response['get_user_response']['get_user_result']['user']
self.path = user['path']
self.create_date = user['create_date']
self.id = user['user_id']
self.arn = user['arn']
self.name = user['user_name']
except boto.exception.BotoServerError, e:
# NOTE: given some information can be deduced from the exception still, the lack of permissions is
# considered a normal condition still and the exception handled/logged accordingly.
Expand All @@ -64,7 +68,6 @@ def describe(self):
try:
iam = boto.connect_iam()
userInfo = UserInfo(iam)
user = userInfo.describe()
print user
print userInfo.describe()
except boto.exception.BotoServerError, e:
logging.exception(e.error_message)
4 changes: 4 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[FORMAT]

# Maximum number of characters on a single line.
max-line-length=120

0 comments on commit f488ac5

Please sign in to comment.