Create a GridIn property.
(
field_name: str,
docstring: str,
read_only: Optional[bool] = False,
closed_only: Optional[bool] = False,
)
| 96 | |
| 97 | |
| 98 | def _grid_in_property( |
| 99 | field_name: str, |
| 100 | docstring: str, |
| 101 | read_only: Optional[bool] = False, |
| 102 | closed_only: Optional[bool] = False, |
| 103 | ) -> Any: |
| 104 | """Create a GridIn property.""" |
| 105 | warn_str = "" |
| 106 | if docstring.startswith("DEPRECATED,"): |
| 107 | warn_str = ( |
| 108 | f"GridIn property '{field_name}' is deprecated and will be removed in PyMongo 5.0" |
| 109 | ) |
| 110 | |
| 111 | def getter(self: Any) -> Any: |
| 112 | if warn_str: |
| 113 | warnings.warn(warn_str, stacklevel=2, category=DeprecationWarning) |
| 114 | if closed_only and not self._closed: |
| 115 | raise AttributeError("can only get %r on a closed file" % field_name) |
| 116 | # Protect against PHP-237 |
| 117 | if field_name == "length": |
| 118 | return self._file.get(field_name, 0) |
| 119 | return self._file.get(field_name, None) |
| 120 | |
| 121 | def setter(self: Any, value: Any) -> Any: |
| 122 | if warn_str: |
| 123 | warnings.warn(warn_str, stacklevel=2, category=DeprecationWarning) |
| 124 | if self._closed: |
| 125 | self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {field_name: value}}) |
| 126 | self._file[field_name] = value |
| 127 | |
| 128 | if read_only: |
| 129 | docstring += "\n\nThis attribute is read-only." |
| 130 | elif closed_only: |
| 131 | docstring = "{}\n\n{}".format( |
| 132 | docstring, |
| 133 | "This attribute is read-only and " |
| 134 | "can only be read after :meth:`close` " |
| 135 | "has been called.", |
| 136 | ) |
| 137 | |
| 138 | if not read_only and not closed_only: |
| 139 | return property(getter, setter, doc=docstring) |
| 140 | return property(getter, doc=docstring) |
| 141 | |
| 142 | |
| 143 | def _grid_out_property(field_name: str, docstring: str) -> Any: |