Package caldavclientlibrary :: Package browser :: Package commands :: Module quota
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.browser.commands.quota

 1  ## 
 2  # Copyright (c) 2007-2016 Apple Inc. All rights reserved. 
 3  # 
 4  # Licensed under the Apache License, Version 2.0 (the "License"); 
 5  # you may not use this file except in compliance with the License. 
 6  # You may obtain a copy of the License at 
 7  # 
 8  # http://www.apache.org/licenses/LICENSE-2.0 
 9  # 
10  # Unless required by applicable law or agreed to in writing, software 
11  # distributed under the License is distributed on an "AS IS" BASIS, 
12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13  # See the License for the specific language governing permissions and 
14  # limitations under the License. 
15  ## 
16   
17  from caldavclientlibrary.browser.command import Command 
18  from caldavclientlibrary.browser.command import WrongOptions 
19  from caldavclientlibrary.protocol.url import URL 
20  import getopt 
21  import os 
22  import shlex 
23  from caldavclientlibrary.protocol.webdav.definitions import davxml 
24   
25 -class Cmd(Command):
26
27 - def __init__(self):
28 super(Command, self).__init__() 29 self.cmds = ("quota",)
30
31 - def execute(self, name, options):
32 33 opts, args = getopt.getopt(shlex.split(options), '') 34 35 for name, _ignore_value in opts: 36 37 print "Unknown option: %s" % (name,) 38 print self.usage(name) 39 raise WrongOptions 40 41 if len(args) > 1: 42 print "Wrong number of arguments: %d" % (len(args),) 43 print self.usage(name) 44 raise WrongOptions 45 46 path = args[0] if len(args) else self.shell.wd 47 if not path.startswith("/"): 48 path = os.path.join(self.shell.wd, path) 49 if not path.endswith("/"): 50 path += "/" 51 52 resource = URL(url=path) 53 results, bad = self.shell.account.session.getProperties(resource, (davxml.quota_available_bytes, davxml.quota_used_bytes)) 54 if davxml.quota_available_bytes in bad: 55 print "Could not retrieve DAV:quota-available-bytes property, status=%d" % (bad[davxml.quota_available_bytes],) 56 elif davxml.quota_used_bytes in bad: 57 print "Could not retrieve DAV:quota-used-bytes property, status=%d" % (bad[davxml.quota_used_bytes],) 58 else: 59 quota_available = int(results[davxml.quota_available_bytes]) 60 quota_used = int(results[davxml.quota_used_bytes]) 61 62 def _printSmartSize(size): 63 64 if size < 1024: 65 return "%d bytes" % (size,) 66 elif size < 1024 * 1024: 67 return "%.1f KB" % (size / 1024.0,) 68 elif size < 1024 * 1024 * 1024: 69 return "%.1f MB" % (size / (1024.0 * 1024.0),) 70 else: 71 return "%.1f GB" % (size / (1024.0 * 1024.0 * 1024.0),)
72 73 print "Used %s of %s (free: %s)" % ( 74 _printSmartSize(quota_used), 75 _printSmartSize(quota_used + quota_available), 76 _printSmartSize(quota_available), 77 ) 78 79 return True
80
81 - def complete(self, text):
82 return self.shell.wdcomplete(text)
83
84 - def usage(self, name):
85 return """Usage: %s [PATH] 86 PATH is a relative or absolute path. 87 88 """ % (name,)
89
90 - def helpDescription(self):
91 return "Checks quota on the specified PATH."
92