Skip to content

Commit 5d3a0ed

Browse files
Sean RigbySean Rigby
Sean Rigby
authored and
Sean Rigby
committed
Cleaned up code styles per pycodestyle, pydocstyle and pylint
1 parent b68d233 commit 5d3a0ed

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

webotron/webotron.py

+43-16
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,66 @@
1-
import boto3
2-
import click
3-
from botocore.exceptions import ClientError
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
"""Webotron: Deploy websites with aws.
5+
6+
Webotron automates the process of deploying static websites
7+
-Configure AWS S3 buckets
8+
- Create them
9+
- Set them up for static website hosting
10+
- Deploy local files to them
11+
- Configure DNS with Route 53
12+
- COnfigure a Content Delivery Network and SSL with AWS Cloudfront
13+
"""
14+
415
from pathlib import Path
516
import mimetypes
617

18+
import boto3
19+
from botocore.exceptions import ClientError
20+
import click
21+
22+
723
session = boto3.Session(profile_name='pythonAutomation')
824
s3 = session.resource('s3')
925

26+
1027
@click.group()
1128
def cli():
12-
"Webotron deploys websites to AWS"
29+
"""Webotron deploys websites to AWS."""
1330
pass
1431

32+
1533
@cli.command('list-buckets')
1634
def list_buckets():
17-
'List all s3 buckets'
35+
"""List all s3 buckets."""
1836
for bucket in s3.buckets.all():
1937
print(bucket)
2038

39+
2140
@cli.command('list-bucket-objects')
2241
@click.argument('bucket')
2342
def list_bucket_objects(bucket):
24-
'List objects in an s3 bucket'
43+
"""List objects in an s3 bucket."""
2544
for obj in s3.Bucket(bucket).objects.all():
2645
print(obj)
2746

47+
2848
@cli.command('Setup-bucket')
2949
@click.argument('bucket')
3050
def setup_bucket(bucket):
31-
'Create and configure S3 bucket'
51+
"""Create and configure S3 bucket."""
3252
s3_bucket = None
3353

3454
try:
3555
s3_bucket = s3.create_bucket(
3656
Bucket=bucket,
3757
CreateBucketConfiguration={'LocationConstraint': session.region_name})
38-
except ClientError as e:
39-
if e.response['Error']['Code'] == 'BucketAlreadyOwnedByYou':
58+
59+
except ClientError as error:
60+
if error.response['Error']['Code'] == 'BucketAlreadyOwnedByYou':
4061
s3_bucket = s3.Bucket(bucket)
4162
else:
42-
raise e
63+
raise error
4364

4465
policy= """
4566
{
@@ -60,8 +81,7 @@ def setup_bucket(bucket):
6081
pol = s3_bucket.Policy()
6182
pol.put(Policy=policy)
6283

63-
ws = s3_bucket.Website()
64-
ws.put(WebsiteConfiguration=
84+
s3_bucket.Website().put(WebsiteConfiguration=
6585
{'ErrorDocument':{
6686
'Key': 'error.html'
6787
},
@@ -72,28 +92,35 @@ def setup_bucket(bucket):
7292

7393
return
7494

95+
7596
def upload_file(s3_bucket, path, key):
97+
"""Upload path to s3_bucket at key."""
7698
content_type = mimetypes.guess_type(key)[0] or 'text/plain'
7799
s3_bucket.upload_file(
78100
path,
79101
key,
80-
ExtraArgs={'ContentType': 'text/html'})
102+
ExtraArgs={'ContentType': content_type})
103+
81104

82105
@cli.command('Sync')
83106
@click.argument('pathname', type=click.Path(exists=True))
84107
@click.argument('bucket')
85108
def sync(pathname, bucket):
86-
"Sync contents of PATHNAME to BUCKET"
109+
"""Sync contents of PATHNAME to BUCKET."""
87110
s3_bucket = s3.Bucket(bucket)
88111

89112
root = Path(pathname).expanduser().resolve()
90113

114+
91115
def handle_directory(target):
92116
for p in target.iterdir():
93-
if p.is_dir(): handle_directory(p)
94-
if p.is_file(): upload_file(s3_bucket, str(p), str(p.relative_to(root)))
117+
if p.is_dir():\
118+
handle_directory(p)
119+
if p.is_file(): upload_file\
120+
(s3_bucket, str(p), str(p.relative_to(root)))
95121

96122
handle_directory(root)
97123

124+
98125
if __name__== '__main__':
99126
cli()

0 commit comments

Comments
 (0)