|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import contextlib |
3 | 4 | import functools |
4 | 5 | import inspect |
5 | 6 | import logging |
@@ -156,9 +157,10 @@ def pin_meta(self, name, version: str = None) -> Meta: |
156 | 157 | meta_name = self.meta_factory.get_meta_name(*components) |
157 | 158 |
|
158 | 159 | path_meta = self.construct_path([*components, meta_name]) |
159 | | - f, local = self._open_pin_meta(path_meta) |
160 | | - |
161 | | - meta = self.meta_factory.read_pin_yaml(f, pin_name, selected_version, local=local) |
| 160 | + with self._open_pin_meta(path_meta) as (f, local): |
| 161 | + meta = self.meta_factory.read_pin_yaml( |
| 162 | + f, pin_name, selected_version, local=local |
| 163 | + ) |
162 | 164 |
|
163 | 165 | return meta |
164 | 166 |
|
@@ -788,14 +790,15 @@ def _load_data(self, meta, pin_version_path): |
788 | 790 |
|
789 | 791 | # filesystem and cache methods -------------------------------------------- |
790 | 792 |
|
| 793 | + @contextlib.contextmanager |
791 | 794 | def _open_pin_meta(self, path): |
792 | | - f = self.fs.open(path) |
793 | | - self._touch_cache(path) |
| 795 | + with self.fs.open(path) as f: |
| 796 | + self._touch_cache(path) |
794 | 797 |
|
795 | | - # optional additional data to put in Meta.local |
796 | | - local = {} |
| 798 | + # optional additional data to put in Meta.local |
| 799 | + local = {} |
797 | 800 |
|
798 | | - return f, local |
| 801 | + yield f, local |
799 | 802 |
|
800 | 803 | def _get_cache_path(self, pin_name, version=None, fname=None): |
801 | 804 | version_part = [version] if version is not None else [] |
@@ -934,8 +937,10 @@ def pin_meta(self, name, version=None): |
934 | 937 | # note that pins on this board should point to versions, so we use an |
935 | 938 | # empty string to mark version (it ultimately is ignored) |
936 | 939 | path_meta = self.construct_path([pin_name, "", meta_name]) |
937 | | - f, local = self._open_pin_meta(path_meta) |
938 | | - meta = self.meta_factory.read_pin_yaml(f, pin_name, VersionRaw(""), local=local) |
| 940 | + with self._open_pin_meta(path_meta) as (f, local): |
| 941 | + meta = self.meta_factory.read_pin_yaml( |
| 942 | + f, pin_name, VersionRaw(""), local=local |
| 943 | + ) |
939 | 944 |
|
940 | 945 | # TODO(#59,#83): handle caching, and then re-enable pin_read. |
941 | 946 | # self._touch_cache(path_meta) |
@@ -1143,22 +1148,23 @@ def pin_versions_prune(self, *args, **kwargs): |
1143 | 1148 | ) |
1144 | 1149 | super().pin_versions_prune(*args, **kwargs) |
1145 | 1150 |
|
| 1151 | + @contextlib.contextmanager |
1146 | 1152 | def _open_pin_meta(self, path): |
1147 | | - f = self.fs.open(path) |
1148 | | - self._touch_cache(path) |
1149 | | - |
1150 | | - # optional additional data to put in Meta.local |
1151 | | - user_name, content_name, bundle_id = str(path).split("/")[:3] |
1152 | | - user_guid = self.fs._user_name_cache[user_name] |
1153 | | - content_guid = self.fs._content_name_cache[(user_guid, content_name)] |
1154 | | - |
1155 | | - local = { |
1156 | | - "content_id": content_guid, |
1157 | | - "version": bundle_id, |
1158 | | - "url": f"{self.fs.api.server_url}/content/{content_guid}/", |
1159 | | - } |
| 1153 | + with self.fs.open(path) as f: |
| 1154 | + self._touch_cache(path) |
| 1155 | + |
| 1156 | + # optional additional data to put in Meta.local |
| 1157 | + user_name, content_name, bundle_id = str(path).split("/")[:3] |
| 1158 | + user_guid = self.fs._user_name_cache[user_name] |
| 1159 | + content_guid = self.fs._content_name_cache[(user_guid, content_name)] |
| 1160 | + |
| 1161 | + local = { |
| 1162 | + "content_id": content_guid, |
| 1163 | + "version": bundle_id, |
| 1164 | + "url": f"{self.fs.api.server_url}/content/{content_guid}/", |
| 1165 | + } |
1160 | 1166 |
|
1161 | | - return f, local |
| 1167 | + yield f, local |
1162 | 1168 |
|
1163 | 1169 | def validate_pin_name(self, name) -> None: |
1164 | 1170 | # this should be the default behavior, expecting a full pin name. |
|
0 commit comments