-
Notifications
You must be signed in to change notification settings - Fork 1
/
yadfs.py
executable file
·108 lines (91 loc) · 2.76 KB
/
yadfs.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3.5
import click
from client.client import Client
from utils.enums import Status, NodeType
import getpass
import datetime
import os
# CLI
@click.group(invoke_without_command=False)
@click.pass_context
def cli(ctx):
pass
@cli.command()
@click.argument('path', default="/")
def ls(path):
"""List directory contents"""
cl = Client()
dir_ls = cl.list_dir(path)
stat = dir_ls['status']
if stat == Status.ok:
for item, info in dir_ls['items'].items():
fr = "-rw-r--r--"
if info['type'] == NodeType.directory:
fr = "drwxr-xr-x"
date = datetime.datetime.fromtimestamp(info['date'])
date_format = '%b %d %H:%M' if date.year == datetime.datetime.today().year else '%b %d %Y'
print('%.11s %.10s %6sB %.15s %s' % (fr,
getpass.getuser(),
info['size'],
datetime.datetime.fromtimestamp(info['date']).strftime(date_format),
item))
else:
print(Status.description(stat))
@cli.command()
@click.argument('path')
def mkdir(path):
"""Create a directory"""
cl = Client()
res = cl.create_dir(path)
stat = res['status']
if stat != Status.ok:
print(Status.description(stat))
@cli.command()
@click.argument('local_path')
@click.argument('remote_path', default="/")
def upload(local_path, remote_path):
"""Create a file"""
cl = Client()
if os.path.isdir(local_path):
print("You can't upload directory as a file")
else:
res = cl.create_file(local_path, remote_path)
stat = res['status']
if stat != Status.ok:
print(Status.description(stat))
@cli.command()
@click.argument('path')
def rm(path):
"""Delete a file or directory"""
cl = Client()
res = cl.delete(path)
stat = res['status']
if stat != Status.ok:
print(Status.description(stat))
@cli.command()
@click.argument('path')
def status(path):
"""Check if path refers to file or directory"""
cl = Client()
res = cl.path_status(path)
print('%s is a %s' % (path, NodeType.description(res['type'])))
@cli.command()
@click.argument('path_from')
@click.argument('path_to')
def download(path_from, path_to):
"""Download a file"""
cl = Client()
res = cl.download_file(path_from, path_to)
# print(res)
stat = res['status']
if stat != Status.ok:
print(Status.description(stat))
@cli.command()
@click.argument('path')
def cat(path):
"""Print a file"""
cl = Client()
res = cl.get_file_content(path)
print(res)
if __name__ == '__main__':
cli()