@@ -34,10 +34,14 @@ def __init__(
34
34
** kwargs : Any ,
35
35
):
36
36
super ().__init__ ()
37
- self .path = path
38
- self .title = title
39
- self .mimetype = mimetype
40
- self .hints = hints
37
+ if path :
38
+ kwargs ["path" ] = path
39
+ if title :
40
+ kwargs ["title" ] = title
41
+ if mimetype :
42
+ kwargs ["mimetype" ] = mimetype
43
+ if hints :
44
+ kwargs ["hints" ] = hints
41
45
for k , v in kwargs .items ():
42
46
setattr (self , k , v )
43
47
@@ -46,16 +50,16 @@ def should_index(self):
46
50
return self .get_mimetype ().startswith ("text/html" )
47
51
48
52
def get_path (self ) -> str :
49
- return self . path or ""
53
+ return getattr ( self , " path" , "" )
50
54
51
55
def get_title (self ) -> str :
52
- return self . title or ""
56
+ return getattr ( self , " title" , "" )
53
57
54
58
def get_mimetype (self ) -> str :
55
- return self . mimetype or ""
59
+ return getattr ( self , " mimetype" , "" )
56
60
57
61
def get_hints (self ) -> dict :
58
- return self . hints or {}
62
+ return getattr ( self , " hints" , {})
59
63
60
64
61
65
class StaticItem (Item ):
@@ -77,28 +81,34 @@ def __init__(
77
81
hints : Optional [dict ] = None ,
78
82
** kwargs : Any ,
79
83
):
84
+ if content :
85
+ kwargs ["content" ] = content
86
+ if fileobj :
87
+ kwargs ["fileobj" ] = fileobj
88
+ if filepath :
89
+ kwargs ["filepath" ] = filepath
80
90
super ().__init__ (
81
91
path = path , title = title , mimetype = mimetype , hints = hints , ** kwargs
82
92
)
83
- self .content = content
84
- self .fileobj = fileobj
85
- self .filepath = filepath
86
93
87
94
def get_contentprovider (self ) -> libzim .writer .ContentProvider :
88
95
# content was set manually
89
- if self .content is not None :
90
- return StringProvider (content = self .content , ref = self )
96
+ content = getattr (self , "content" , None )
97
+ if content is not None :
98
+ return StringProvider (content = content , ref = self )
91
99
92
100
# using a file-like object
93
- if self .fileobj :
101
+ fileobj = getattr (self , "fileobj" , None )
102
+ if fileobj :
94
103
return FileLikeProvider (
95
- fileobj = self . fileobj , ref = self , size = getattr (self , "size" , None )
104
+ fileobj = fileobj , ref = self , size = getattr (self , "size" , None )
96
105
)
97
106
98
107
# we had to download locally to get size
99
- if self .filepath :
108
+ filepath = getattr (self , "filepath" , None )
109
+ if filepath :
100
110
return FileProvider (
101
- filepath = self . filepath , ref = self , size = getattr (self , "size" , None )
111
+ filepath = filepath , ref = self , size = getattr (self , "size" , None )
102
112
)
103
113
104
114
raise NotImplementedError ("No data to provide`" )
@@ -140,15 +150,16 @@ def __init__(
140
150
title : Optional [str ] = None ,
141
151
mimetype : Optional [str ] = None ,
142
152
hints : Optional [dict ] = None ,
143
- * ,
144
- use_disk : bool = False ,
153
+ use_disk : Optional [bool ] = None ,
145
154
** kwargs : Any ,
146
155
):
156
+ if use_disk :
157
+ kwargs ["use_disk" ] = use_disk
147
158
super ().__init__ (
148
159
path = path , title = title , mimetype = mimetype , hints = hints , ** kwargs
149
160
)
150
161
self .url = urllib .parse .urlparse (url )
151
- self . use_disk = use_disk
162
+ use_disk = getattr ( self , " use_disk" , False )
152
163
153
164
# fetch headers to retrieve size and type
154
165
try :
@@ -177,7 +188,7 @@ def __init__(
177
188
except Exception :
178
189
# we couldn't retrieve size so we have to download resource to
179
190
target , self .size = self .download_for_size (
180
- self .url , on_disk = self . use_disk , tmp_dir = getattr (self , "tmp_dir" , None )
191
+ self .url , on_disk = use_disk , tmp_dir = getattr (self , "tmp_dir" , None )
181
192
)
182
193
# downloaded to disk and using a file path from now on
183
194
if use_disk :
@@ -187,11 +198,16 @@ def __init__(
187
198
self .fileobj = target
188
199
189
200
def get_path (self ) -> str :
190
- return self .path or re .sub (r"^/" , "" , self .url .path )
201
+ return getattr (self , "path" , re .sub (r"^/" , "" , self .url .path ))
202
+
203
+ def get_title (self ) -> str :
204
+ return getattr (self , "title" , "" )
191
205
192
206
def get_mimetype (self ) -> str :
193
- return self .mimetype or self .headers .get (
194
- "Content-Type" , "application/octet-stream"
207
+ return getattr (
208
+ self ,
209
+ "mimetype" ,
210
+ self .headers .get ("Content-Type" , "application/octet-stream" ),
195
211
)
196
212
197
213
def get_contentprovider (self ):
0 commit comments