289 @type str |
291 @type str |
290 @return tuple containg the directory listing |
292 @return tuple containg the directory listing |
291 @rtype tuple of str |
293 @rtype tuple of str |
292 @exception IOError raised to indicate an issue with the device |
294 @exception IOError raised to indicate an issue with the device |
293 """ |
295 """ |
294 commands = [ |
296 if self.__repl.isMicrobit(): |
295 "import os as __os_", |
297 # BBC micro:bit does not support directories |
296 "print(__os_.listdir('{0}'))".format(dirname), |
298 commands = [ |
297 "del __os_", |
299 "import os as __os_", |
298 ] |
300 "print(__os_.listdir())", |
|
301 "del __os_", |
|
302 ] |
|
303 else: |
|
304 commands = [ |
|
305 "import os as __os_", |
|
306 "print(__os_.listdir('{0}'))".format(dirname), |
|
307 "del __os_", |
|
308 ] |
299 out, err = self.execute(commands) |
309 out, err = self.execute(commands) |
300 if err: |
310 if err: |
301 raise IOError(self.__shortError(err)) |
311 raise IOError(self.__shortError(err)) |
302 return ast.literal_eval(out.decode("utf-8")) |
312 return ast.literal_eval(out.decode("utf-8")) |
303 |
313 |
315 false) or the complete stat() tuple. 'None' is returned in case the |
325 false) or the complete stat() tuple. 'None' is returned in case the |
316 directory doesn't exist. |
326 directory doesn't exist. |
317 @rtype tuple of (str, tuple) |
327 @rtype tuple of (str, tuple) |
318 @exception IOError raised to indicate an issue with the device |
328 @exception IOError raised to indicate an issue with the device |
319 """ |
329 """ |
320 commands = [ |
330 if self.__repl.isMicrobit(): |
321 "import os as __os_", |
331 # BBC micro:bit does not support directories |
322 "\n".join([ |
332 commands = [ |
323 "def is_visible(filename):", |
333 "import os as __os_", |
324 " return filename[0] != '.' and filename[-1] != '~'", |
334 "\n".join([ |
325 ]), |
335 "def is_visible(filename):", |
326 "\n".join([ |
336 " return filename[0] != '.' and filename[-1] != '~'", |
327 "def stat(filename):", |
337 ]), |
328 " try:", |
338 "\n".join([ |
329 " rstat = __os_.lstat(filename)", |
339 "def stat(filename):", |
330 " except:", |
340 " size = __os_.size(filename)", |
331 " rstat = __os_.stat(filename)", |
341 " return (0, 0, 0, 0, 0, 0, size, 0, 0, 0)" |
332 " return tuple(rstat)", |
342 ]), |
333 ]), |
343 "\n".join([ |
334 "\n".join([ |
344 "def listdir_stat():", |
335 "def listdir_stat(dirname):", |
345 " files = __os_.listdir()", |
336 " try:", |
346 " return list((f, stat(f)) for f in files if" |
337 " files = __os_.listdir(dirname)", |
347 " is_visible(f))", |
338 " except OSError:", |
348 ]), |
339 " return None", |
349 "print(listdir_stat())", |
340 " if dirname in ('', '/'):", |
350 "del __os_, stat, listdir_stat, is_visible", |
341 " return list((f, stat(f)) for f in files if" |
351 ] |
342 " is_visible(f))", |
352 else: |
343 " return list((f, stat(dirname + '/' + f)) for f in files" |
353 commands = [ |
344 " if is_visible(f))", |
354 "import os as __os_", |
345 ]), |
355 "\n".join([ |
346 "print(listdir_stat('{0}'))".format(dirname), |
356 "def is_visible(filename):", |
347 "del __os_, stat, listdir_stat", |
357 " return filename[0] != '.' and filename[-1] != '~'", |
348 ] |
358 ]), |
|
359 "\n".join([ |
|
360 "def stat(filename):", |
|
361 " try:", |
|
362 " rstat = __os_.lstat(filename)", |
|
363 " except:", |
|
364 " rstat = __os_.stat(filename)", |
|
365 " return tuple(rstat)", |
|
366 ]), |
|
367 "\n".join([ |
|
368 "def listdir_stat(dirname):", |
|
369 " try:", |
|
370 " files = __os_.listdir(dirname)", |
|
371 " except OSError:", |
|
372 " return None", |
|
373 " if dirname in ('', '/'):", |
|
374 " return list((f, stat(f)) for f in files if" |
|
375 " is_visible(f))", |
|
376 " return list((f, stat(dirname + '/' + f))" |
|
377 " for f in files if is_visible(f))", |
|
378 ]), |
|
379 "print(listdir_stat('{0}'))".format(dirname), |
|
380 "del __os_, stat, listdir_stat, is_visible", |
|
381 ] |
349 out, err = self.execute(commands) |
382 out, err = self.execute(commands) |
350 if err: |
383 if err: |
351 raise IOError(self.__shortError(err)) |
384 raise IOError(self.__shortError(err)) |
352 fileslist = ast.literal_eval(out.decode("utf-8")) |
385 fileslist = ast.literal_eval(out.decode("utf-8")) |
353 if fileslist is None: |
386 if fileslist is None: |