Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pymongo<4.9 pin, upgrade pymongo, and fixed errors and warnings #69

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions ming/datastore.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from __future__ import annotations

import time
from contextlib import closing
import logging
from threading import Lock
from typing import Union, TYPE_CHECKING
import urllib
import warnings
import weakref

from pymongo import MongoClient
from pymongo.database import Database
from pymongo.encryption import ClientEncryption, Algorithm
Expand Down Expand Up @@ -96,8 +100,8 @@ def create_datastore(uri, **kwargs) -> DataStore:
# Create engine without connection.
bind = create_engine(**kwargs)

return DataStore(bind, database, encryption_config)

return DataStore(bind, database, encryption_config)

class Engine:
"""Engine represents the connection to a MongoDB (or in-memory database).
Expand All @@ -106,6 +110,11 @@ class Engine:
accessed.
"""

@staticmethod
def _cleanup_conn(client, *args, **kwargs):
if getattr(client, 'close', None) is not None:
client.close()

def __init__(self, Connection,
conn_args, conn_kwargs, connect_retry, auto_ensure_indexes, _sleep=time.sleep):
self._Connection = Connection
Expand Down Expand Up @@ -147,8 +156,10 @@ def connect(self):
with self._lock:
if self._conn is None:
# NOTE: Runs MongoClient/EncryptionClient
self._conn = self._Connection(
conn = self._Connection(
*self._conn_args, **self._conn_kwargs)
weakref.finalize(self, Engine._cleanup_conn, conn)
self._conn = conn
else:
return self._conn
except ConnectionFailure:
Expand Down
14 changes: 11 additions & 3 deletions ming/mim.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from bson.binary import UuidRepresentation, Binary
from bson.codec_options import CodecOptions
from bson.raw_bson import RawBSONDocument
from pymongo import database, collection, ASCENDING, MongoClient, UpdateOne
from pymongo import database, collection, ASCENDING, MongoClient as RealMongoClient, UpdateOne
from pymongo.cursor import Cursor as PymongoCursor
from pymongo.errors import InvalidOperation, OperationFailure, DuplicateKeyError
from pymongo.results import DeleteResult, UpdateResult, InsertManyResult, InsertOneResult, BulkWriteResult
Expand All @@ -51,7 +51,11 @@ def __del__(self):
pass


class Connection:
class MongoClient:
pass


class Connection(MongoClient):
_singleton = None

@classmethod
Expand All @@ -64,12 +68,13 @@ def __init__(self):
self._databases = {}

# Clone defaults from a MongoClient instance.
mongoclient = MongoClient(uuidRepresentation=UUID_REPRESENTATION_STR)
mongoclient = RealMongoClient(uuidRepresentation=UUID_REPRESENTATION_STR)
self.options = mongoclient.options
self.read_preference = mongoclient.read_preference
self.write_concern = mongoclient.write_concern
self.codec_options = mongoclient.codec_options
self.read_concern = getattr(mongoclient, 'read_concern', None)
mongoclient.close()

def drop_all(self):
self._databases = {}
Expand Down Expand Up @@ -98,6 +103,9 @@ def _get(self, name):
def list_database_names(self):
return self._databases.keys()

def close(self):
pass

def drop_database(self, name):
try:
del self._databases[name]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
include_package_data=True,
zip_safe=True,
install_requires=[
"pymongo[encryption]<4.9",
"pymongo[encryption]",
"pytz",
],
tests_require=[
Expand Down