@@ -73,6 +73,7 @@ def open( # noqa, pylint: disable=redefined-builtin
73
73
remote : Optional [str ] = None ,
74
74
mode : str = "r" ,
75
75
encoding : Optional [str ] = None ,
76
+ config : Optional [Dict [str , Any ]] = None ,
76
77
):
77
78
"""
78
79
Opens a file tracked in a DVC project.
@@ -114,6 +115,8 @@ def open( # noqa, pylint: disable=redefined-builtin
114
115
Defaults to None.
115
116
This should only be used in text mode.
116
117
Mirrors the namesake parameter in builtin `open()`_.
118
+ config(dict, optional): config to be passed to the DVC repository.
119
+ Defaults to None.
117
120
118
121
Returns:
119
122
_OpenContextManager: A context manager that generatse a corresponding
@@ -209,14 +212,24 @@ def open( # noqa, pylint: disable=redefined-builtin
209
212
"rev" : rev ,
210
213
"mode" : mode ,
211
214
"encoding" : encoding ,
215
+ "config" : config ,
212
216
}
213
217
return _OpenContextManager (_open , args , kwargs )
214
218
215
219
216
- def _open (path , repo = None , rev = None , remote = None , mode = "r" , encoding = None ):
217
- repo_kwargs : Dict [str , Any ] = {"subrepos" : True , "uninitialized" : True }
220
+ def _open (path , repo = None , rev = None , remote = None , mode = "r" , encoding = None , config = None ):
218
221
if remote :
219
- repo_kwargs ["config" ] = {"core" : {"remote" : remote }}
222
+ if config is not None :
223
+ raise ValueError (
224
+ "can't specify both `remote` and `config` at the same time"
225
+ )
226
+ config = {"core" : {"remote" : remote }}
227
+
228
+ repo_kwargs : Dict [str , Any ] = {
229
+ "subrepos" : True ,
230
+ "uninitialized" : True ,
231
+ "config" : config ,
232
+ }
220
233
221
234
with Repo .open (repo , rev = rev , ** repo_kwargs ) as _repo :
222
235
with _wrap_exceptions (_repo , path ):
@@ -251,13 +264,19 @@ def _open(path, repo=None, rev=None, remote=None, mode="r", encoding=None):
251
264
raise DvcIsADirectoryError (f"'{ path } ' is a directory" ) from exc
252
265
253
266
254
- def read (path , repo = None , rev = None , remote = None , mode = "r" , encoding = None ):
267
+ def read (path , repo = None , rev = None , remote = None , mode = "r" , encoding = None , config = None ):
255
268
"""
256
269
Returns the contents of a tracked file (by DVC or Git). For Git repos, HEAD
257
270
is used unless a rev argument is supplied. The default remote is tried
258
271
unless a remote argument is supplied.
259
272
"""
260
273
with open (
261
- path , repo = repo , rev = rev , remote = remote , mode = mode , encoding = encoding
274
+ path ,
275
+ repo = repo ,
276
+ rev = rev ,
277
+ remote = remote ,
278
+ mode = mode ,
279
+ encoding = encoding ,
280
+ config = config ,
262
281
) as fd :
263
282
return fd .read ()
0 commit comments