# =========================================================================
# Copyright (C) 2016 Yunify, Inc.
# -------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this work except in compliance with the License.
# You may obtain a copy of the License in the LICENSE file, or at:
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========================================================================

import time

from qingcloud.qingstor.connection import QSConnection

from .base import BaseCommand

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

# Format used to pretty print directories.
format_directory = " " * 30

class LsCommand(BaseCommand):

    command = "ls"
    usage = "%(prog)s <qs-path> [-c <conf_file> -r <recusively> -p <page_size>]"

    @classmethod
    def add_extra_arguments(cls, parser):
        parser.add_argument(
            "-z",
            "--zone",
            dest="zone",
            help="List buckets located in this zone"
        )

        parser.add_argument(
            "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"
        )

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

    @classmethod
    def get_connection(cls, conf, options):
        if options.qs_path == "qs://":
            host = ENDPOINT
        else:
            host = "%s.%s" % (conf["zone"], ENDPOINT)

        return QSConnection(
            qy_access_key_id=conf["qy_access_key_id"],
            qy_secret_access_key=conf["qy_secret_access_key"],
            host=host
        )

    @classmethod
    def list_buckets(cls, options):
        headers = {}
        if options.zone:
            headers["Location"] = options.zone
        resp = cls.conn.make_request("GET", headers=headers)
        if resp.status == HTTP_OK:
            body = json_loads(resp.read())
            buckets = body["buckets"]
            for bucket in sorted(buckets, key = lambda x: x["name"]):
                print(bucket["name"])
        resp.close()

    @classmethod
    def print_to_console(cls, keys, dirs):
        for d in sorted(dirs):
            print("Directory" + format_directory + d)
        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"))
            if key["mime_type"] == "qs-directory":
                print(created_time + format_size(key["size"]).rjust(12) + \
                    " " * 4 + key["key"] + "  (qs-directory)")
            else:
                print(created_time + format_size(key["size"]).rjust(12) + \
                    " " * 4 + key["key"])

    @classmethod
    def list_keys(cls, options):
            bucket, prefix = cls.validate_qs_path(options.qs_path)

            params = {}
            if prefix != "":
                params["prefix"] = prefix
            if options.recursive is False:
                params["delimiter"] = "/"
            if options.page_size is not None:
                params["limit"] = str(options.page_size)

            marker = ""
            while True:
                keys, marker, dirs = cls.list_multiple_keys(
                    bucket,
                    marker,
                    params=params
                )

                cls.print_to_console(keys, dirs)
                if marker == "":
                    break

    @classmethod
    def send_request(cls, options):
        if options.qs_path == "qs://":
            cls.list_buckets(options)
        else:
            cls.list_keys(options)

@@ -3574,28 +3574,24 @@
 s):%0A        
-    
 bucket, pref
@@ -3638,28 +3638,24 @@
 h)%0A%0A        
-    
 params = %7B%7D%0A
@@ -3662,20 +3662,16 @@
         
- 
-   
 if prefi
@@ -3675,28 +3675,24 @@
 efix != %22%22:%0A
-    
             
@@ -3717,36 +3717,32 @@
  prefix%0A        
-  
-  
 if options.recur
@@ -3760,36 +3760,32 @@
 se:%0A            
-    
 params%5B%22delimite
@@ -3798,28 +3798,24 @@
 %22/%22%0A        
-   
- 
 if options.p
@@ -3832,28 +3832,24 @@
 s not None:%0A
-    
             
@@ -3890,20 +3890,16 @@
 _size)%0A%0A
-    
         
@@ -3918,20 +3918,16 @@
         
-    
 while Tr
@@ -3930,20 +3930,16 @@
 e True:%0A
-    
         
@@ -4007,20 +4007,16 @@
     
-    
 bucket,%0A
     
@@ -4011,20 +4011,16 @@
 bucket,%0A
-    
         
@@ -4047,28 +4047,24 @@
             
-  
-  
 params=param
@@ -4073,34 +4073,27 @@
             
-    )%0A%0A   
+)%0A%0A
             
@@ -4088,25 +4088,24 @@
             
- 
 cls.print_to
@@ -4137,20 +4137,16 @@
         
-    
 if marke
@@ -4150,28 +4150,24 @@
 rker == %22%22:%0A
-    
             

Fix Indent