Re: Proposed design for importlib.resources()

Donald Stufft <[email protected]> Tue, 24 Nov 2015 15:47:10 -0500
Newsgroups gmane.comp.python.import
Message-ID <[email protected]>
--===============8042544708986755489==
Content-Type: multipart/signed; boundary="Apple-Mail=_9CAFDF0C-EF7B-4473-B379-555E7E630ADD"; protocol="application/pgp-signature"; micalg=pgp-sha512


--Apple-Mail=_9CAFDF0C-EF7B-4473-B379-555E7E630ADD
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_C228868D-2C65-42B1-962D-A60AB8E2AA58"


--Apple-Mail=_C228868D-2C65-42B1-962D-A60AB8E2AA58
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8


> On Nov 24, 2015, at 3:21 PM, Brett Cannon <[email protected]> wrote:
>=20
>=20
> resource_filename().  Doesn't pkg_resources already have a strategy =
for the
> temporary file that sometimes has to be created?
>=20
> Yes and I don't like it. :) Basically you either create an instance or =
implicitly use a global instance of a class that stores the references =
and registers with atexit a cleanup function to be executed.


In pkg_resources they aren=E2=80=99t actually temporary files but are =
instead cached files which are stored in a non temporary location per =
user and will be recreated if need be. It would probably be sane to make =
this context manager like the ones in tempfile where the creation =
happens in __init__ and the deletion happens in __exit__ (with also a =
.delete() function) so people can choose how to use it. If you wanted =
something more like what pkg_resources does now, you could do:

filename =3D importlib.resources.get_filename(=E2=80=9Cmypackage=E2=80=9D,=
 =E2=80=9Csome/path.txt=E2=80=9D, dir=3D=E2=80=9C~/.cache/mypackage=E2=80=9D=
)

-----------------
Donald Stufft
PGP: 0x6E3CBCE93372DCFA // 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 =
DCFA


--Apple-Mail=_C228868D-2C65-42B1-962D-A60AB8E2AA58
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html =
charset=3Dutf-8"></head><body style=3D"word-wrap: break-word; =
-webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" =
class=3D""><br class=3D""><div><blockquote type=3D"cite" class=3D""><div =
class=3D"">On Nov 24, 2015, at 3:21 PM, Brett Cannon &lt;<a =
href=3D"mailto:[email protected]" class=3D"">[email protected]</a>&gt; =
wrote:</div><br class=3D"Apple-interchange-newline"><div =
class=3D""><blockquote class=3D"gmail_quote" style=3D"font-family: =
Helvetica; font-size: 12px; font-style: normal; font-variant: normal; =
font-weight: normal; letter-spacing: normal; orphans: auto; text-align: =
start; text-indent: 0px; text-transform: none; white-space: normal; =
widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: =
0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, =
204, 204); border-left-style: solid; padding-left: 1ex;"><br =
class=3D"">resource_filename().&nbsp; Doesn't pkg_resources already have =
a strategy for the<br class=3D"">temporary file that sometimes has to be =
created?</blockquote><div style=3D"font-family: Helvetica; font-size: =
12px; font-style: normal; font-variant: normal; font-weight: normal; =
letter-spacing: normal; orphans: auto; text-align: start; text-indent: =
0px; text-transform: none; white-space: normal; widows: auto; =
word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=3D""><br =
class=3D""></div><div style=3D"font-family: Helvetica; font-size: 12px; =
font-style: normal; font-variant: normal; font-weight: normal; =
letter-spacing: normal; orphans: auto; text-align: start; text-indent: =
0px; text-transform: none; white-space: normal; widows: auto; =
word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=3D"">Yes and I =
don't like it. :) Basically you either create an instance or implicitly =
use a global instance of a class that stores the references and =
registers with atexit a cleanup function to be =
executed.</div></div></blockquote></div><div class=3D""><br =
class=3D""></div>In pkg_resources they aren=E2=80=99t actually temporary =
files but are instead cached files which are stored in a non temporary =
location per user and will be recreated if need be. It would probably be =
sane to make this context manager like the ones in tempfile where the =
creation happens in __init__ and the deletion happens in __exit__ (with =
also a .delete() function) so people can choose how to use it. If you =
wanted something more like what pkg_resources does now, you could =
do:<div class=3D""><br class=3D""></div><div class=3D"">filename =3D =
importlib.resources.get_filename(=E2=80=9Cmypackage=E2=80=9D, =
=E2=80=9Csome/path.txt=E2=80=9D, dir=3D=E2=80=9C~/.cache/mypackage=E2=80=9D=
)<br class=3D""><div class=3D"">
<br class=3D"">-----------------<br class=3D"">Donald Stufft<br =
class=3D"">PGP: 0x6E3CBCE93372DCFA // 7C6B&nbsp;7C5D 5E2B 6356 A926 F04F =
6E3C&nbsp;BCE9 3372 DCFA

</div>
<br class=3D""></div></body></html>=

--Apple-Mail=_C228868D-2C65-42B1-962D-A60AB8E2AA58--

--Apple-Mail=_9CAFDF0C-EF7B-4473-B379-555E7E630ADD
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename=signature.asc
Content-Type: application/pgp-signature;
	name=signature.asc
Content-Description: Message signed with OpenPGP using GPGMail

-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org

iQIcBAEBCgAGBQJWVMzVAAoJEG48vOkzctz6B4AP/RsJ753sC/5tAX8UpVN2d1jz
YrhCiigeVa/dq8YjlyLjUzV5St2dds9M8g+FMgggsFAmBgwvCsSsKWWih+vt56U1
FM6+sFHEtWczBL9PRDPBJKdpo/4o42apImwamu7ttmg1rcKp6QIHIk/wmX4UPNEq
6zD7HdthD/GX86tVFRGtJQ2MaG5W2ZguVu7md7yqrJeyCkAzMU8z81dtmxVgyhAx
7tq0YxNpni9khnzaAtBCmEiEfq6w/Ohqg8wX/LHahewg63iEzheiF6bnvQtGdQ/X
LvODEjuj8ctq1R+eGE+TVJazfagtLHMns78ZzlVh8Nc68RFyiSPeJMU0IzNvX6+Z
ABxlsRGJjVniiiPhbf/6YUa8N9xCM0gRuoTjppTo8o+J4UBpldAM2iggfTCUB++E
90WcUotEMnbRIp1AiZnDdiGklZH3IWrlLSNwatGbKvAiKIBLD31EbK9o+dfUj0LV
WcK62efFBDjdN7lPhPe822SsuYdDarEHKedcmFsS/DmCN4L4yKbIEPwGzn5f6Oyq
Z6qG9W8Kgl/hvREZlEdKvTJUPTKUfkht+uaD9TfeXnlYYRExH/weH9GnRGOYcbxR
Cx+ZfRqor1PVKePc2DBIZ1U0MlR1xv5Hxx7sb0/2W+SNdnRM2gP5BFlT4aDkYy1m
L5WwMlEWMhUxiqmMNwSK
=1GxL
-----END PGP SIGNATURE-----

--Apple-Mail=_9CAFDF0C-EF7B-4473-B379-555E7E630ADD--

--===============8042544708986755489==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Import-SIG mailing list
[email protected]
https://mail.python.org/mailman/listinfo/import-sig

--===============8042544708986755489==--