| 166 | |
| 167 | # algo ls <path> |
| 168 | def ls(self, path, client, longlist=False): |
| 169 | # by default list user's hosted data |
| 170 | listing = "" |
| 171 | if path is None: |
| 172 | path = "data://" |
| 173 | |
| 174 | file = path.split('/') |
| 175 | if file[-1] != '': |
| 176 | # path is a file, list parent |
| 177 | directory = path[:-len(file[-1])] |
| 178 | f = client.dir(directory) |
| 179 | |
| 180 | response = client.getHelper(f.url, **{}) |
| 181 | if response.status_code != 200: |
| 182 | raise DataApiError("failed to get file info: " + str(response.content)) |
| 183 | |
| 184 | responseContent = response.content |
| 185 | if isinstance(responseContent, six.binary_type): |
| 186 | responseContent = responseContent.decode() |
| 187 | |
| 188 | content = json.loads(responseContent) |
| 189 | |
| 190 | if 'files' in content: |
| 191 | f = client.file(path) |
| 192 | for file_info in content['files']: |
| 193 | if file_info['filename'] == file[-1]: |
| 194 | f.set_attributes(file_info) |
| 195 | |
| 196 | if longlist: |
| 197 | listing += f.last_modified.strftime("%Y-%m-%d %H:%M:%S") + ' ' |
| 198 | listing += str(f.size) + ' ' |
| 199 | listing += f.path + "\n" |
| 200 | else: |
| 201 | listing += f.path + "\n" |
| 202 | else: |
| 203 | # path is a directory |
| 204 | if longlist: |
| 205 | listingDir = client.dir(path) |
| 206 | for f in listingDir.dirs(): |
| 207 | listing += f.path + "/\n" |
| 208 | for f in listingDir.files(): |
| 209 | listing += f.last_modified.strftime("%Y-%m-%d %H:%M:%S") + ' ' |
| 210 | listing += str(f.size) + ' ' |
| 211 | listing += f.path + "\n" |
| 212 | |
| 213 | else: |
| 214 | listingDir = client.dir(path) |
| 215 | for f in listingDir.dirs(): |
| 216 | listing += f.path + "/\n" |
| 217 | for f in listingDir.files(): |
| 218 | listing += f.path + "\n" |
| 219 | |
| 220 | return listing |
| 221 | |
| 222 | # algo cat <file> |
| 223 | def cat(self, path, client): |