-
Notifications
You must be signed in to change notification settings - Fork 3
/
pathtoid.py
executable file
·115 lines (91 loc) · 2.91 KB
/
pathtoid.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
109
110
111
112
113
# encoding: utf-8
import putio
import config
import os
def _unicode(s):
if isinstance(s, str):
try:
return s.decode("utf-8")
except UnicodeDecodeError:
raise HTTPError(400, "Non-utf8 argument")
assert isinstance(s, unicode)
return s
def _utf8(s):
if isinstance(s, unicode):
return s.encode("utf-8")
assert isinstance(s, str)
return s
sep = os.path.sep
sepsep = sep + sep
class PathToId:
def __init__(self):
self.cache = {sepsep:0, sep:0}
self.itemscache = {}
self.api = None
def encode_key(self, s):
return _utf8(s)
def invalidate_items_cache_by_path(self, path):
try:
item = self.find_item_by_path(path)
if item:
self.invalidate_items_cache(item)
except:
pass
def invalidate_items_cache(self, parent_id):
if parent_id in self.itemscache:
del self.itemscache[parent_id]
def get_items_cache(self, parent_id=0):
if parent_id in self.itemscache:
return self.itemscache[parent_id]
return False
def load_items(self, path=sep, parent_id=0):
try:
if self.get_items_cache(parent_id):
items = self.get_items_cache(parent_id)
else:
self.api = putio.Api(config.apikey, config.apisecret)
items = self.api.get_items(parent_id=parent_id)
self.itemscache[parent_id] = items
except: # raise PutioError("You have no items to show.") for empty folders ? wtf.
items = []
for i in items:
key = self.encode_key(_utf8(path) + sep + _utf8(i.name) )
self.cache[key] = i.id
def find_item_by_path(self, path):
# this is root
if path == sep:
return 0
if not path.startswith(sep):
raise ValueError('parse_fspath: You have to provide a full path')
parents = path.split(sep)[1:]
# check if they are cached or not...
k = sep
lparentid = 0
lparentpath = ''
for dir in parents:
self.load_items(k, self.cache[self.encode_key(k)])
k = k + sep + dir
print "key: >>>> ", k
self.load_items(k, self.cache[self.encode_key(k)])
return self.cache[self.encode_key(sep + _utf8(path) )]
if __name__ == '__main__':
# api = putio.Api(config.apikey, config.apisecret)
# items = api.get_items(parent_id=24, limit=300)
# for i in items:
# print ">", i.id, i.name.encode('utf-8')
p = PathToId()
print p.find_item_by_path('/123/9')
p.invalidate_items_cache_by_path('/123/8')
# p.invalidate_items_cache_by_path('/123/9')
# p.invalidate_items_cache_by_path('/123')
# print p.find_item_by_path('/123/9')
# print p.find_item_by_path('/123')
# p = PathToId()
# #p.load_items('/x', 11156055)
# p.load_items()
# print "============================="
# print p.find_item_by_path('/x/y/z/rtorrent.log')
# print p.find_item_by_path('/x/y/z')
#p.find_item_by_path('/x/y/z')
# s = 'çöğüışIĞÜÇÖ'.decode('utf-8')
# print s, type(s)