Proxy object to handle writing and reading of files to and from GridFS
| 1671 | |
| 1672 | |
| 1673 | class GridFSProxy: |
| 1674 | """Proxy object to handle writing and reading of files to and from GridFS""" |
| 1675 | |
| 1676 | _fs = None |
| 1677 | |
| 1678 | def __init__( |
| 1679 | self, |
| 1680 | grid_id=None, |
| 1681 | key=None, |
| 1682 | instance=None, |
| 1683 | db_alias=DEFAULT_CONNECTION_NAME, |
| 1684 | collection_name="fs", |
| 1685 | ): |
| 1686 | self.grid_id = grid_id # Store GridFS id for file |
| 1687 | self.key = key |
| 1688 | self.instance = instance |
| 1689 | self.db_alias = db_alias |
| 1690 | self.collection_name = collection_name |
| 1691 | self.newfile = None # Used for partial writes |
| 1692 | self.gridout = None |
| 1693 | |
| 1694 | def __getattr__(self, name): |
| 1695 | attrs = ( |
| 1696 | "_fs", |
| 1697 | "grid_id", |
| 1698 | "key", |
| 1699 | "instance", |
| 1700 | "db_alias", |
| 1701 | "collection_name", |
| 1702 | "newfile", |
| 1703 | "gridout", |
| 1704 | ) |
| 1705 | if name in attrs: |
| 1706 | return self.__getattribute__(name) |
| 1707 | obj = self.get() |
| 1708 | if hasattr(obj, name): |
| 1709 | return getattr(obj, name) |
| 1710 | raise AttributeError |
| 1711 | |
| 1712 | def __get__(self, instance, value): |
| 1713 | return self |
| 1714 | |
| 1715 | def __bool__(self): |
| 1716 | return bool(self.grid_id) |
| 1717 | |
| 1718 | def __getstate__(self): |
| 1719 | self_dict = self.__dict__ |
| 1720 | self_dict["_fs"] = None |
| 1721 | return self_dict |
| 1722 | |
| 1723 | def __copy__(self): |
| 1724 | copied = GridFSProxy() |
| 1725 | copied.__dict__.update(self.__getstate__()) |
| 1726 | return copied |
| 1727 | |
| 1728 | def __deepcopy__(self, memo): |
| 1729 | return self.__copy__() |
| 1730 |