-
Notifications
You must be signed in to change notification settings - Fork 4
/
sdls
executable file
·91 lines (80 loc) · 2.71 KB
/
sdls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /usr/bin/env python3
# list files on Toshiba FlashAir SD card via wi-fi
# see https://www.flashair-developers.com/en/documents/api/
import os
import sys
import argparse
import datetime
import configparser
import flashair
import httpx
def fmt(size):
kb=size/1024
mb=size/1048576
gb=size/1073741824
s='{:,.2f} GB'.format(gb)
if gb < 1:
s='{:,.2f} MB'.format(mb)
if mb < 1:
s='{:,.2f} KB'.format(kb)
return s
card=flashair.card()
card.parser.add_argument('-d', '--directory', help='directory', default=card.def_directory)
card.parser.add_argument('-s', '--sort', help='sort output lines (alpha, size, date, none)',
choices=['a', 's', 'd', 'n'], default=card.def_sort)
card.setup()
card.get_files()
if len(card.files) == 0:
print('directory "'+card.dir+'" is empty')
raise SystemExit
sum=0
output=[]
for file, size, attrib, date in card.files:
if attrib & 16:
# this is a directory
output.append((file, '<DIR>', '', str(date), size, date))
else:
# this is a file
output.append((file, '{:,}'.format(size), fmt(size), str(date), size, date))
sum+=size
# sort
if card.sort == 'a':
# ignore case, like the printer
output.sort(key=lambda x: x[0].lower())
if card.sort == 's':
output.sort(key=lambda x: x[4])
if card.sort == 'd':
output.sort(key=lambda x: x[5])
# grab remaining space
r=httpx.get('http://'+card.address+'/command.cgi?op=140')
t=r.text.split('/')
empty=int(t[0])
total, sector=map(int, t[1].split(','))
empty*=sector
total*=sector
used=total-empty
space=empty
pct_used=(used/total)*100.0
pct_empty=(empty/total)*100.0
# add totals
output.append(('Dir Total:', '{:,}'.format(sum), fmt(sum)))
output.append(('Used:', '{:,}'.format(used), fmt(used), '{:.2f}%'.format(pct_used)))
output.append(('Free:', '{:,}'.format(space), fmt(space), '{:.2f}%'.format(pct_empty)))
# calculate column widths
width1=max(len(x[0]) for x in output)
width2=max(len(x[1]) for x in output)
width3=max(len(x[2]) for x in output)
# add dashed footer line
output.insert(-3, ('-'*width1, '-'*width2, '-'*width3))
# now print it all in neat columns
print(card.address, 'directory:', card.dir)
for data in output:
if len(data) == 3:
print('{0:{1}} {2:>{3}} {4:>{5}}'.format(data[0], width1,
data[1], width2,
data[2], width3))
else:
print('{0:{1}} {2:>{3}} {4:>{5}} {6:>{7}}'.format(data[0], width1,
data[1], width2,
data[2], width3,
data[3], 6))