forked from leo-vip/cockybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.py
145 lines (109 loc) · 3.4 KB
/
filesystem.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# coding: UTF-8
import logging
import os
import urllib2
import Config
import json
from utils import connect_path, getFile
__author__ = 'lei'
# base="/home/cocky"
class FileSystem:
def outErr(self):
logging.error("No Realyzed")
def exists(self, path):
self.outErr()
pass
def isfile(self, path):
self.outErr()
pass
def listdir(self, path):
self.outErr()
return []
pass
def getdownloadurl(self, path, name):
self.outErr()
return ""
class LocalFileSystem(FileSystem):
"""
config the #Config.base
"""
def exists(self, path):
return os.path.exists(connect_path(Config.base, path))
def isfile(self, path):
return os.path.isfile(connect_path(Config.base, path))
def listdir(self, path):
return os.listdir(connect_path(Config.base, path))
def getdownloadurl(self, path, name):
return connect_path(connect_path(Config.SITE_BOOK_DONWLOAD, path), name)
class LocalMetadataFileSystem(FileSystem):
#q = Auth(Config.access_key, Config.secret_key)
#bucket = BucketManager(q)
def __init__(self):
ff=open('metadata.json','r')
self.book_trees = json.load(ff)
def exists(self, path):
files=getFile(self.book_trees,self.getTruePaths(path))
return files != None
def isfile(self, path):
if path is None:
return False
if path.find('_-_') == -1:
return False
else:
return True
def listdir(self, path):
paths=self.getTruePaths(path)
if len(paths)!=0:
return getFile(self.book_trees, paths)
else:
return self.book_trees
def getTruePaths(self,tmp):
"""
:param tmp:
:return:
"""
paths=tmp.split('/')
paths=[p for p in paths if p!='']
return paths
def getdownloadurl(self, path, name):
tmp = connect_path(path,name)
files=getFile(self.book_trees, self.getTruePaths(tmp))
return [connect_path(Config.SITE_BOOK_DONWLOAD,connect_path(path, ee)) for ee in files]
class QiniuFileSystem(FileSystem):
#q = Auth(Config.access_key, Config.secret_key)
#bucket = BucketManager(q)
def __init__(self):
resp=urllib2.urlopen(connect_path(Config.SITE_BOOK_DONWLOAD,'metadata.json'))
if resp.getcode() ==200:
self.book_trees = json.loads(resp.read())
def outErr(self):
logging.error("No Realyzed")
def exists(self, path):
files=getFile(self.book_trees,self.getTruePaths(path))
#logging.info(len(files)!=0)
return len(files)!=0
def isfile(self, path):
if path.find('_-_') == -1:
return False
else:
return True
def listdir(self, path):
paths=self.getTruePaths(path)
if len(paths)!=0:
return getFile(self.book_trees, paths)
else:
return self.book_trees
def getTruePaths(self,tmp):
"""
:param tmp:
:return:
"""
paths=tmp.split('/')
paths=[p for p in paths if p!='']
return paths
def getdownloadurl(self, path, name):
tmp = connect_path(path,name)
files=getFile(self.book_trees,self.getTruePaths(tmp))
return [connect_path(Config.SITE_BOOK_DONWLOAD,connect_path(path, ee)) for ee in files]
if __name__ =='__main__':
pass