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

Source Code for Module caldavclientlibrary.browser.commands.ls

  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.caldav.definitions import csxml 
 20  from caldavclientlibrary.protocol.webdav.definitions import davxml 
 21  from caldavclientlibrary.protocol.url import URL 
 22  import os 
 23  import getopt 
 24  import shlex 
 25   
26 -class Cmd(Command):
27
28 - def __init__(self):
29 super(Command, self).__init__() 30 self.cmds = ("ls",)
31
32 - def execute(self, name, options):
33 34 longlist = False 35 path = None 36 displayname = False 37 ctag = False 38 etag = False 39 synctoken = False 40 41 opts, args = getopt.getopt(shlex.split(options), 'acdels') 42 43 for name, _ignore_value in opts: 44 45 if name == "-a": 46 pass 47 elif name == "-c": 48 ctag = True 49 longlist = True 50 elif name == "-d": 51 displayname = True 52 longlist = True 53 elif name == "-e": 54 etag = True 55 longlist = True 56 elif name == "-l": 57 longlist = True 58 elif name == "-s": 59 synctoken = True 60 longlist = True 61 else: 62 print "Unknown option: %s" % (name,) 63 print self.usage(name) 64 raise WrongOptions 65 66 if len(args) > 1: 67 print "Wrong number of arguments: %d" % (len(args),) 68 print self.usage(name) 69 raise WrongOptions 70 elif args: 71 path = args[0] 72 if not path.startswith("/"): 73 path = os.path.join(self.shell.wd, path) 74 else: 75 path = self.shell.wd 76 if not path.endswith("/"): 77 path += "/" 78 resource = URL(url=path) 79 80 props = (davxml.resourcetype,) 81 if longlist: 82 props += (davxml.getcontentlength, davxml.getlastmodified,) 83 if ctag: 84 props += (csxml.getctag,) 85 if etag: 86 props += (davxml.getetag,) 87 if synctoken: 88 props += (davxml.synctoken,) 89 results = self.shell.account.session.getPropertiesOnHierarchy(resource, props) 90 items = results.keys() 91 items.sort() 92 lines = [] 93 for rurl in items: 94 if rurl == path: 95 continue 96 line = [] 97 if longlist: 98 props = results[rurl] 99 size = props.get(davxml.getcontentlength, "-") 100 if not size: 101 size = "0" 102 line.append("%s" % (size,)) 103 modtime = props.get(davxml.getlastmodified, "-") 104 line.append(modtime) 105 line.append(rurl[len(path):]) 106 if displayname: 107 line.append("name:'%s'" % (props.get(davxml.displayname, '-'),)) 108 if ctag: 109 line.append("ctag:'%s'" % (props.get(csxml.getctag, '-'),)) 110 if etag: 111 line.append("etag:'%s'" % (props.get(davxml.getetag, '-'),)) 112 if synctoken: 113 line.append("sync:'%s'" % (props.get(davxml.synctoken, '-'),)) 114 else: 115 line.append(rurl[len(path):]) 116 lines.append(line) 117 118 if lines: 119 # Get column widths 120 widths = [0] * len(lines[0]) 121 for line in lines: 122 for ctr, col in enumerate(line): 123 widths[ctr] = max(widths[ctr], len(col)) 124 125 # Write out each one 126 for line in lines: 127 for ctr, col in enumerate(line): 128 if ctr in (0, 1) and longlist: 129 print col.rjust(widths[ctr] + 2), 130 else: 131 print col.ljust(widths[ctr] + 2), 132 print 133 return True
134
135 - def complete(self, text):
136 return self.shell.wdcomplete(text)
137
138 - def usage(self, name):
139 return """Usage: %s [OPTIONS] [PATH] 140 PATH is a relative or absolute path. 141 142 Options: 143 -c long listing + CS:getctag 144 -d long listing + DAV:displayname 145 -e long listing + DAV:getetag 146 -l long listing 147 -s long listing + DAV:sync-token 148 """ % (name,)
149
150 - def helpDescription(self):
151 return "List the contents of a directory."
152