Skip to content

Commit

Permalink
style: Use QingStor Devel code style
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Jan 12, 2017
1 parent 1d2116c commit e175893
Show file tree
Hide file tree
Showing 24 changed files with 274 additions and 150 deletions.
7 changes: 7 additions & 0 deletions .style.yapf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[style]
based_on_style = pep8
COLUMN_LIMIT = 80
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF = True
SPLIT_BEFORE_FIRST_ARGUMENT = True
COALESCE_BRACKETS = True
DEDENT_CLOSING_BRACKETS = True
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ build: clean

format:
@echo "format code with google style"
yapf -i -r ./qingstor ./tests --style google
yapf -i -r ./qingstor ./tests
@echo "ok"
51 changes: 31 additions & 20 deletions qingstor/qsctl/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
uni_print,
to_unix_path,
is_pattern_match,
validate_bucket_name,)
validate_bucket_name,
)


class BaseCommand(object):
Expand All @@ -47,7 +48,8 @@ def add_common_arguments(cls, parser):
action="store",
type=str,
default="~/.qingstor/config.yaml",
help="Configuration file")
help="Configuration file"
)

@classmethod
def add_extra_arguments(cls, parser):
Expand All @@ -62,7 +64,8 @@ def get_argument_parser(cls):
parser = argparse.ArgumentParser(
prog='qsctl %s' % cls.command,
usage=cls.usage,
description=cls.description)
description=cls.description
)
cls.add_common_arguments(parser)
cls.add_extra_arguments(parser)
cls.add_transfer_arguments(parser)
Expand All @@ -77,8 +80,10 @@ def get_client(cls, conf):
def get_buckets(cls):
resp = cls.client.list_buckets()
if resp.status_code != HTTP_OK:
print("Error: Please check your configuration and you have "
"enough permission to access qingstor service.")
print(
"Error: Please check your configuration and you have "
"enough permission to access qingstor service."
)
sys.exit()
return resp["buckets"]

Expand All @@ -101,25 +106,31 @@ def validate_bucket(cls, bucket):
current_bucket = cls.client.Bucket(bucket, cls.bucket_map[bucket])
resp = current_bucket.head()
if resp.status_code != HTTP_OK:
print("Error: Please check if you have enough"
" permission to access bucket <%s>." % bucket)
print(
"Error: Please check if you have enough"
" permission to access bucket <%s>." % bucket
)
sys.exit(-1)

@classmethod
def validate_local_path(cls, path):
dirname = os.path.dirname(path)
if dirname != "":
if os.path.isfile(dirname):
print("Error: File with the same name '%s' already exists" %
dirname)
print(
"Error: File with the same name '%s' already exists" %
dirname
)
sys.exit(-1)
elif not os.path.isdir(dirname):
try:
os.makedirs(dirname)
print("Directory '%s' created" % dirname)
except OSError as e:
print("Error: Failed to create directory '%s': %s" %
(dirname, e))
print(
"Error: Failed to create directory '%s': %s" %
(dirname, e)
)
sys.exit(-1)

@classmethod
Expand Down Expand Up @@ -170,26 +181,26 @@ def remove_multiple_keys(cls, bucket, prefix="", options=None):
marker = ""
while True:
keys, marker, _ = cls.list_multiple_keys(
bucket, marker=marker, prefix=prefix)
bucket, marker=marker, prefix=prefix
)
for item in keys:
key = item["key"] if sys.version > "3" else item["key"].encode(
'utf8')
'utf8'
)
if cls.confirm_key_remove(key[len(prefix):], options):
cls.remove_key(bucket, key)
if marker == "":
break

@classmethod
def list_multiple_keys(cls,
bucket,
prefix="",
delimiter="",
marker="",
limit=200):
def list_multiple_keys(
cls, bucket, prefix="", delimiter="", marker="", limit=200
):
cls.validate_bucket(bucket)
current_bucket = cls.client.Bucket(bucket, cls.bucket_map[bucket])
resp = current_bucket.list_objects(
marker=marker, prefix=prefix, delimiter=delimiter, limit=limit)
marker=marker, prefix=prefix, delimiter=delimiter, limit=limit
)
keys = resp["keys"]
dirs = resp["common_prefixes"]
next_marker = resp["next_marker"]
Expand Down
3 changes: 2 additions & 1 deletion qingstor/qsctl/commands/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ class CpCommand(TransferCommand):
command = "cp"
usage = (
"%(prog)s <source-path> <dest-path> [-c <conf_file> "
"-r <recusively> --exclude <pattern value> --include <pattern value>]")
"-r <recusively> --exclude <pattern value> --include <pattern value>]"
)
41 changes: 25 additions & 16 deletions qingstor/qsctl/commands/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
import sys
import time

from qingstor.sdk.config import Config
from qingstor.sdk.service.qingstor import QingStor

from .base import BaseCommand

from ..utils import format_size, json_loads
from ..utils import format_size
from ..constants import HTTP_OK

# Format used to pretty print directories.
Expand All @@ -40,25 +37,29 @@ def add_extra_arguments(cls, parser):
"-z",
"--zone",
dest="zone",
help="List buckets located in this zone")
help="List buckets located in this zone"
)

parser.add_argument(
"qs_path", nargs="?", default="qs://", help="The qs-path to list")
"qs_path", nargs="?", default="qs://", help="The qs-path to list"
)

parser.add_argument(
"-p",
"--page-size",
dest="page_size",
type=int,
default=20,
help="The number of results to return in each response")
help="The number of results to return in each response"
)

parser.add_argument(
"-r",
"--recursive",
action="store_true",
dest="recursive",
help="Recursively list keys")
help="Recursively list keys"
)
return parser

@classmethod
Expand All @@ -72,8 +73,10 @@ def list_buckets(cls, options):
for bucket in sorted(buckets, key=lambda x: x["name"]):
print(bucket["name"])
else:
print("Error: Please check if you have "
"enough permission to access QingStor.")
print(
"Error: Please check if you have "
"enough permission to access QingStor."
)
sys.exit(-1)

@classmethod
Expand All @@ -83,13 +86,18 @@ def print_to_console(cls, keys, dirs):
for key in sorted(keys, key=lambda x: x["key"]):
created_time = time.strftime(
"%Y-%m-%d %X UTC",
time.strptime(key["created"], "%Y-%m-%dT%H:%M:%S.000Z"))
time.strptime(key["created"], "%Y-%m-%dT%H:%M:%S.000Z")
)
if key["mime_type"] == "application/x-directory":
print(created_time + format_size(key["size"]).rjust(12) + " " *
4 + key["key"] + " (application/x-directory)")
print(
created_time + format_size(key["size"]).rjust(12) + " " * 4
+ key["key"] + " (application/x-directory)"
)
else:
print(created_time + format_size(key["size"]).rjust(12) + " " *
4 + key["key"])
print(
created_time + format_size(key["size"]).rjust(12) + " " * 4
+ key["key"]
)

@classmethod
def list_keys(cls, options):
Expand All @@ -106,7 +114,8 @@ def list_keys(cls, options):

while True:
keys, marker, dirs = cls.list_multiple_keys(
bucket, prefix, delimiter, marker, limit)
bucket, prefix, delimiter, marker, limit
)
cls.print_to_console(keys, dirs)
if marker == "":
break
Expand Down
3 changes: 2 additions & 1 deletion qingstor/qsctl/commands/mb.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def add_extra_arguments(cls, parser):
"-z",
"--zone",
dest="zone",
help="In which zone to create the bucket")
help="In which zone to create the bucket"
)
return parser

@classmethod
Expand Down
6 changes: 4 additions & 2 deletions qingstor/qsctl/commands/mv.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class MvCommand(TransferCommand):
command = "mv"
usage = (
"%(prog)s <source-path> <dest-path> [-c <conf_file> "
"-r <recusively> --exclude <pattern value> --include <pattern value>]")
"-r <recusively> --exclude <pattern value> --include <pattern value>]"
)

@classmethod
def clean_empty_dirs(cls, options):
Expand All @@ -41,6 +42,7 @@ def clean_empty_dirs(cls, options):

# Delete empty directory.
if not os.listdir(local_dir) and is_pattern_match(
key_path, options.exclude, options.include):
key_path, options.exclude, options.include
):
os.rmdir(local_dir)
print("Local directory '%s' deleted" % local_dir)
3 changes: 2 additions & 1 deletion qingstor/qsctl/commands/rb.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def add_extra_arguments(cls, parser):
"--force",
action="store_true",
dest="force",
help="Forcely delete a nonempty bucket")
help="Forcely delete a nonempty bucket"
)
return parser

@classmethod
Expand Down
24 changes: 16 additions & 8 deletions qingstor/qsctl/commands/rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,36 @@
class RmCommand(BaseCommand):

command = "rm"
usage = ("%(prog)s <qs_path> [-c <conf_file> -r <recursive> "
"--exclude <pattern value> --include <pattern value>]")
usage = (
"%(prog)s <qs_path> [-c <conf_file> -r <recursive> "
"--exclude <pattern value> --include <pattern value>]"
)

@classmethod
def add_extra_arguments(cls, parser):
parser.add_argument(
"qs_path", help="Key or keys under a specific prefix to be deleted")
"qs_path", help="Key or keys under a specific prefix to be deleted"
)

parser.add_argument(
"-r",
"--recursive",
action="store_true",
dest="recursive",
help="Recursively delete keys under a specific prefix")
help="Recursively delete keys under a specific prefix"
)

parser.add_argument(
"--exclude",
type=str,
help="Exclude all files or keys that match the specified pattern")
help="Exclude all files or keys that match the specified pattern"
)

parser.add_argument(
"--include",
type=str,
help="Do not exclude files or keys that match the specified pattern")
help="Do not exclude files or keys that match the specified pattern"
)
return parser

@classmethod
Expand All @@ -56,7 +62,9 @@ def send_request(cls, options):
else:
key = prefix
if key == "":
print("Error: You must give a correct and complete qs_path, "
"such as 'qs://testbucket/testfile'.")
print(
"Error: You must give a correct and complete qs_path, "
"such as 'qs://testbucket/testfile'."
)
sys.exit(-1)
cls.remove_key(bucket, key)
Loading

0 comments on commit e175893

Please sign in to comment.