-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.py
78 lines (63 loc) · 2.22 KB
/
file.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
import os
class Location:
"""
Represents a directory either local or remote
Takes a string to init
A local Location is simply defined by the path (absolute or relative)
A remote location is written [user@]host:directory
The user, host and directory attribute will be parsed
"""
def __init__(self, location_str: str):
self.location_str = location_str
self.user, self.host, self.directory = self.parse_location(location_str)
@staticmethod
def parse_location(location_str: str):
if ":" in location_str:
user_host, *directory = location_str.split(':')
if "@" in user_host:
try:
user, host = user_host.split('@')
except ValueError:
print("Unexpected syntax: @ in username or host")
raise
else:
user = None
host = user_host
directory = ':'.join(directory)
else:
user = host = None
directory = location_str
while directory.endswith('/'):
directory = directory[:-1]
return user, host, directory
@property
def is_remote(self):
return self.host is not None
@property
def is_local(self):
return self.host is None
def __eq__(self, l2):
if self.is_local:
return l2.is_local and self.directory == l2.directory
return self.user, self.host, self.directory == \
l2.user, l2.host, l2.directory
def __hash__(self):
return hash((self.user, self.host, self.directory))
class File:
"""
Represents a file
It has a base location, a directory and a name
"""
def __init__(self, loc: Location, path: str):
self.loc = loc
self.dir, self.name = os.path.split(path)
self.path = path
def __repr__(self):
if self.loc.is_remote:
return f"Remote file on <{self.loc.host}:{self.loc.directory}>" \
f" {self.path}"
return f"Local file: {self.loc.directory}#{self.path}"
def __eq__(self, f2):
return (self.loc, self.path) == (f2.loc, f2.path)
def __hash__(self):
return hash((self.loc, self.path))