MCPcopy
hub / github.com/pallets/click / convert

Method convert

src/click/types.py:926–969  ·  view source on GitHub ↗
(
        self,
        value: str | os.PathLike[str] | t.IO[t.Any],
        param: Parameter | None,
        ctx: Context | None,
    )

Source from the content-addressed store, hash-verified

924 return False
925
926 def convert(
927 self,
928 value: str | os.PathLike[str] | t.IO[t.Any],
929 param: Parameter | None,
930 ctx: Context | None,
931 ) -> t.IO[t.Any]:
932 if _is_file_like(value):
933 return value
934
935 try:
936 lazy = self.resolve_lazy_flag(value)
937
938 if lazy:
939 lf = LazyFile(
940 value, self.mode, self.encoding, self.errors, atomic=self.atomic
941 )
942
943 if ctx is not None:
944 ctx.call_on_close(lf.close_intelligently)
945
946 return t.cast("t.IO[t.Any]", lf)
947
948 f, should_close = open_stream(
949 value, self.mode, self.encoding, self.errors, atomic=self.atomic
950 )
951
952 # If a context is provided, we automatically close the file
953 # at the end of the context execution (or flush out). If a
954 # context does not exist, it's the caller's responsibility to
955 # properly close the file. This for instance happens when the
956 # type is used with prompts.
957 if ctx is not None:
958 if should_close:
959 ctx.call_on_close(safecall(f.close))
960 else:
961 ctx.call_on_close(safecall(f.flush))
962
963 return f
964 except OSError as e:
965 self.fail(
966 f"'{format_filename(value)}': {e.strerror}",
967 param,
968 ctx,
969 )
970
971 def shell_complete(
972 self, ctx: Context, param: Parameter, incomplete: str

Callers 1

convertMethod · 0.45

Calls 8

resolve_lazy_flagMethod · 0.95
_is_file_likeFunction · 0.85
LazyFileClass · 0.85
open_streamFunction · 0.85
safecallFunction · 0.85
format_filenameFunction · 0.85
call_on_closeMethod · 0.80
failMethod · 0.45

Tested by

no test coverage detected