@@ -127,6 +127,15 @@ def build_argument_parser():
127
127
help = "path to config file (default: ~/.paper2remarkable.yml)" ,
128
128
default = None ,
129
129
)
130
+ parser .add_argument (
131
+ "--source" ,
132
+ choices = ["url" , "file" ],
133
+ help = (
134
+ "Force the source type (url or file) in case of detection failure."
135
+ " This is useful when the input is ambiguous, but be aware that "
136
+ "this does not guarantee successful processing."
137
+ ),
138
+ )
130
139
parser .add_argument (
131
140
"input" ,
132
141
help = "One or more URLs to a paper or paths to local PDF files" ,
@@ -153,18 +162,28 @@ def exception(msg):
153
162
raise SystemExit (1 )
154
163
155
164
156
- def choose_provider (cli_input ):
165
+ def choose_provider (cli_input , source_type = None ):
157
166
"""Choose the provider to use for the given source
158
167
159
- This function first tries to check if the input is a local file, by
160
- checking if the path exists. Next, it checks if the input is a "valid" url
161
- using the validators library. If it is, the registered provider classes are
162
- checked to see which provider can handle this url.
168
+ This function determines the appropriate provider based on the input and the
169
+ optional source_type parameter. If source_type is specified, it overrides
170
+ the automatic detection. Otherwise, it first tries to check if the input is
171
+ a local file by checking if the path exists. Next, it checks if the input is
172
+ a "valid" url using a regex test. If it is, the registered provider classes
173
+ are checked to see which provider can handle this url.
174
+
175
+ Parameters
176
+ ----------
177
+ cli_input : str
178
+ The input provided by the user, either a file path or a URL.
179
+ source_type : str, optional
180
+ The type of the source, either "file" or "url". If provided, it overrides
181
+ the automatic detection.
163
182
164
183
Returns
165
184
-------
166
185
provider : class
167
- The class of the provider than can handle the source. A subclass of the
186
+ The class of the provider that can handle the source. A subclass of the
168
187
Provider abc.
169
188
170
189
new_input : str
@@ -178,19 +197,22 @@ def choose_provider(cli_input):
178
197
Raises
179
198
------
180
199
UnidentifiedSourceError
181
- Raised when the input is neither an existing local file nor a valid url
200
+ Raised when the input is neither an existing local file nor a valid url,
201
+ and no valid source_type is provided.
182
202
183
203
InvalidURLError
184
- Raised when the input *is* a valid url, but no provider can handle it.
185
-
204
+ Raised when the input *is* a valid url (or source_type is "url"), but no
205
+ provider can handle it.
186
206
"""
187
207
provider = cookiejar = None
188
- if LocalFile .validate (cli_input ):
189
- # input is a local file
208
+ if source_type == "file" or (
209
+ source_type is None and LocalFile .validate (cli_input )
210
+ ):
211
+ # input is a local file or user specified source type is file
190
212
new_input = cli_input
191
213
provider = LocalFile
192
- elif is_url (cli_input ):
193
- # input is a url
214
+ elif source_type == "url" or ( source_type is None and is_url (cli_input ) ):
215
+ # input is a url or user specified source type is url
194
216
new_input , cookiejar = follow_redirects (cli_input )
195
217
provider = next ((p for p in providers if p .validate (new_input )), None )
196
218
else :
@@ -292,7 +314,9 @@ def runner(inputs, filenames, options, debug=False):
292
314
if not len (inputs ) == len (filenames ):
293
315
raise ValueError ("Number of inputs and filenames must be the same" )
294
316
for cli_input , filename in zip (inputs , filenames ):
295
- provider , new_input , cookiejar = choose_provider (cli_input )
317
+ provider , new_input , cookiejar = choose_provider (
318
+ cli_input , options ["core" ].get ("source" )
319
+ )
296
320
prov = provider (
297
321
verbose = options ["core" ]["verbose" ],
298
322
upload = options ["core" ]["upload" ],
0 commit comments