cervmongo package¶
package contents¶
-
cervmongo.
quick_load_client
(host=None, port=None, database='test_db', collection='test_col', replica_set=None, async_=False)[source]¶ Returns CollectionClient instance
- Parameters
host (
Optional
[str
]) –port (
Optional
[int
]) –database (
str
) –collection (
str
) –replica_set (
Optional
[str
]) –async_ (
bool
) –
cervmongo.config submodule¶
-
class
cervmongo.config.
Config
[source]¶ Bases:
object
MongoDB and cervmongo settings, loaded initially by environmental variables
-
MONGO_DB
: Optional[str] = None¶ The MongoDB database to use
-
MONGO_HOST
: str = '127.0.0.1'¶ The host server for MongoDB
-
MONGO_PORT
: int = 27017¶ The port for connection to host server
-
MONGO_REPLICA_SET
: Optional[str] = None¶ The name of the replica set, if any
-
MONGO_MAX_POOL_SIZE
: int = 20¶
-
MONGO_MIN_POOL_SIZE
: int = 10¶
-
MONGO_USER
: Optional[str] = None¶ The username for MongoDB connection
-
MONGO_PASSWORD
: Optional[str] = None¶ The password for MongoDB connection
-
MONGO_URI
: Optional[str] = 'mongodb://127.0.0.1:27017/None'¶ The MongoDB URI that will be used when accessing clients
-
DEBUG_LEVEL
: int = 30¶ The level at which to display information, defaults to logging.warning
-
JSON_SAMPLE_PATH
: str = './'¶ For use with JSON sample records to simplify schema process of new documents
-
JSON_SCHEMA_PATH
: str = './'¶ For use with JSON schema documents for validating new documents on creation
-
LIST_MODE
: bool = False¶ Only implemented in SyncIOClient. Instructs results to convert any Cursor to list instantly, ensuring no partially-filled cursors remain open
-
classmethod
reset
()[source]¶ resets config values to the first values assigned when cervmongo was imported
- Return type
None
-
classmethod
set_debug_level
(debug_level)[source]¶ sets debug level of application, default is logging.warning
- Parameters
debug_level (
int
) –- Return type
-
classmethod
enable_list_mode
()[source]¶ sets cls.LIST_MODE to True; use corresponding function if SyncIOClient instantiated.
- Return type
-
classmethod
disable_list_mode
()[source]¶ sets cls.LIST_MODE to False; use corresponding function if SyncIOClient instantiated.
- Return type
-
classmethod
set_mongo_db
(database_name)[source]¶ assigns the MONGO_DB var in config and regenerates mongodb uri
returns Class
- Parameters
cls (
Type
[~Config]) –database_name (
str
) –
- Return type
-
classmethod
set_mongo_host
(host)[source]¶ assigns the MONGO_HOST var in config and regenerates mongodb uri
returns Class
- Parameters
cls (
Type
[~Config]) –host (
str
) –
- Return type
-
classmethod
set_mongo_port
(port)[source]¶ assigns the MONGO_PORT var in config and regenerates mongodb uri
returns Class
- Parameters
cls (
Type
[~Config]) –port (
int
) –
- Return type
-
classmethod
set_mongo_replica_set
(replica_set=None)[source]¶ assigns the MONGO_REPLICA_SET var in config and regenerates mongodb uri
returns Class
- Parameters
cls (
Type
[~Config]) –- Return type
-
classmethod
generate_mongo_uri
()[source]¶ uses config class values to generate mongodb uri and returns connection string
- Return type
NewType()
(MongoDB URI
,str
)
-
classmethod
reload
()[source]¶ Reloads config class values from environment, falls back to current values if none found
- Return type
None
-
cervmongo.main submodule¶
-
cervmongo.main.
SUPPORT_GRIDFS
= True¶ True if gridfs functionality is available else False
-
class
cervmongo.main.
SyncIOClient
(mongo_uri=None, default_collection=None, **kwargs)[source]¶ Bases:
pymongo.mongo_client.MongoClient
High-level MongoClient subclass with additional methods added for ease-of-use, having some automated conveniences and defaults.
- Parameters
mongo_uri (
Optional
[str
]) –default_collection (
Optional
[str
]) –
-
set_database
(database)[source]¶ is used to change or set database of client instance, also changes db in Config class
- Parameters
database (
str
) –- Return type
None
-
COLLECTION
(collection)[source]¶ returns unique CollectionClient class instance
Be aware, CollectionClient is NOT a valid MongoClient. To access the original SyncIOClient instance, use method get_client of CollectionClient instance.
- Parameters
collection (
str
) –
-
PAGINATED_QUERY
(collection, limit=20, sort=<Pagination Sort Fields._id: '_id'>, after=None, before=None, page=None, endpoint='/', ordering=-1, query={}, **kwargs)[source]¶ Returns paginated results of cursor from the collection query.
- Available pagination methods:
- Cursor-based (default)
after
before
limit (results per page, default 20)
- Time-based (a datetime field must be selected)
sort (set to datetime field)
after (records after this time)
before (records before this time)
limit (results per page, default 20)
- Offset-based (not recommended)
limit (results per page, default 20)
page
- Parameters
collection (
Optional
[str
]) –limit (
int
) –sort (
Pagination Sort Fields
) –after (
Optional
[str
]) –before (
Optional
[str
]) –page (
Optional
[int
]) –endpoint (
str
) –ordering (
int
) –query (
dict
) –
-
GENERATE_ID
(_id=None)[source]¶ returns a unique ObjectID, simply for convenience
- Parameters
_id (
Optional
[str
]) –- Return type
NewType()
(Document ID
,ObjectId
)
-
DELETE
(collection, record_or_records, soft=False, one=False)[source]¶ deletes the requested document(s)
returns MongoDB response document
If soft=true, creates collection (‘deleted.{collection}’) and inserts the deleted document there. field ‘oid’ is guaranteed to equal the original document’s “_id”.
- Parameters
collection (
Optional
[str
]) –soft (
bool
) –one (
bool
) –
- Return type
Union
[MongoDictResponse
,MongoListResponse
]
-
INDEX
(collection, key='_id', sort=1, unique=False, reindex=False)[source]¶ creates an index, however most useful in constraining certain fields as unique
- Parameters
key (
Union
[List
,str
]) –sort (
Union
[List
,int
]) –unique (
bool
) –reindex (
bool
) –
- Return type
None
-
ADD_FIELD
(collection, field, value=None, data=False, query={})[source]¶ adds field with value provided to all records in collection that match query
kwarg ‘data’, if provided, is an existing field in the record that can be used as the default value. useful for name changes of fields in schema.
- Parameters
field (
str
) –value (
Union
[Dict
,List
,str
,int
,float
,None
]) –query (
dict
) –
- Return type
None
-
REMOVE_FIELD
(collection, field, query={})[source]¶ removes field of all records in collection that match query
useful for complete field removal, once field is no longer needed.
- Parameters
field (
str
) –query (
dict
) –
- Return type
None
-
UPLOAD
(fileobj, filename=None, content_type=None, extension=None, **kwargs)[source]¶ returns GridFS response document after successful upload
fileobj can be either valid filepath or file/file-like object, all other kwargs are stored as meta data
- Parameters
fileobj (
Union
[IO
,str
]) –filename (
Optional
[str
]) –content_type (
Optional
[str
]) –extension (
Optional
[str
]) –
-
ERASE
(filename_or_id, revision=- 1)[source]¶ deletes the GridFS file. if multiple revisions, deletes most recent by default.
filename_or_id can be the _id field value or the filename value.
- Parameters
filename_or_id (
Union
[str
,NewType()
(Document ID
,ObjectId
)]) –revision (
int
) –
- Return type
None
-
DOWNLOAD
(filename_or_id=None, revision=- 1, skip=None, limit=None, sort=- 1, **query)[source]¶ returns download stream of file if filename_or_id is provided, else returns a cursor of files matching query
- Parameters
filename_or_id (
Union
[str
,NewType()
(Document ID
,ObjectId
),None
]) –revision (
int
) –skip (
Optional
[int
]) –limit (
Optional
[int
]) –sort (
int
) –
-
GET
(collection, id_or_query={}, sort=1, key='_id', count=None, search=None, fields=None, page=None, perpage=False, limit=None, after=None, before=None, empty=None, distinct=None, one=False, **kwargs)[source]¶ record can be either _id (accepts unicode form of ObjectId, as well as extended JSON bson format) or query
Unless certain kwargs are used (count, distinct, one), behaviour is as follows:
if _id is recognized, one is set to true and will return the exact matching document
if query is recognized, will return MongoList response of cursor
if count is provided and _id is not recognized, returns number of documents in cursor
if distinct is provided, returns a unique list of the field values (accepts dot notation)
if one is provided, returns the first matching document of cursor
kwargs count, distinct, one cannot be used together, priority is as follows if all are provided:
count
distinct
one
- Parameters
id_or_query (
Union
[NewType()
(Document ID
,ObjectId
),Dict
,str
]) –sort (
int
) –key (
str
) –count (
Optional
[bool
]) –search (
Optional
[str
]) –fields (
Union
[List
,dict
,None
]) –page (
Optional
[int
]) –perpage (
int
) –limit (
Optional
[int
]) –after (
Optional
[str
]) –before (
Optional
[str
]) –distinct (
Optional
[str
]) –one (
bool
) –
-
SEARCH
(collection, search, **kwargs)[source]¶ returns the results of querying textIndex of collection
- Parameters
search (
str
) –
-
POST
(collection, record_or_records)[source]¶ creates new record(s) and returns MongoDB response document
- Parameters
record_or_records (
Union
[List
,Dict
]) –
-
class
cervmongo.main.
SyncIODoc
(_id=None, doc_type=None, doc_sample=None, doc_schema=None, doc_id=None, mongo_uri=None, **kwargs)[source]¶ Bases:
cervmongo.main.SyncIOClient
Custom MongoClient subclass with customizations for creating standardized documents and adding json schema validation.
- Parameters
doc_type (
Optional
[str
]) –doc_sample (
Union
[Dict
,str
,None
]) –doc_schema (
Union
[Dict
,str
,None
]) –doc_id (
Optional
[str
]) –mongo_uri (
Optional
[str
]) –
-
create
(save=False, trigger=None, template='{total}', query={}, **kwargs)[source]¶ - Parameters
save (
bool
) –template (
str
) –query (
dict
) –
-
close
()[source]¶ Cleanup client resources and disconnect from MongoDB.
On MongoDB >= 3.6, end all server sessions created by this client by sending one or more endSessions commands.
Close all sockets in the connection pools and stop the monitor threads. If this instance is used again it will be automatically re-opened and the threads restarted unless auto encryption is enabled. A client enabled with auto encryption cannot be used again after being closed; any attempt will raise
InvalidOperation
.Changed in version 3.6: End all server sessions created by this client.
-
class
cervmongo.main.SyncIOClient.COLLECTION.
CollectionClient
¶ Convenience class that is instantiated and supplied by the COLLECTION method. It auto-supplies collection to all upper-cased SyncIOClient methods, where required [source]
-
get_client
()¶
-
GENERATE_ID
(_id=None)¶
-
COLLECTION
(collection)¶
-
UPLOAD
(fileobj, filename=None, content_type=None, extension=None, **kwargs)¶
-
DOWNLOAD
(filename_or_id=None, revision=- 1, skip=None, limit=None, sort=- 1, **query)¶
-
ERASE
(filename_or_id, revision=- 1)¶
-
INDEX
(key='_id', sort=1, unique=False, reindex=False)¶
-
ADD_FIELD
(field, value='', data=False, query={})¶
-
REMOVE_FIELD
(field, query={})¶
-
DELETE
(record_or_records, soft=False)¶
-
GET
(id_or_query={}, sort=1, key='_id', count=None, search=None, fields=None, page=None, perpage=False, limit=None, after=None, before=None, empty=None, distinct=None, one=False, **kwargs)¶
-
POST
(record_or_records)¶
-
PUT
(record_or_records)¶
-
PATCH
(original, updates, w=1, upsert=False, multi=False, log=None)¶
-
REPLACE
(original, replacement, upsert=False)¶
-
SEARCH
(search, **kwargs)¶
-
PAGINATED_QUERY
(limit=20, sort=<Pagination Sort Fields._id: "_id">, after=None, before=None, page=None, endpoint='/', ordering=-1, query={}, **kwargs)¶
-
cervmongo.aio submodule¶
-
cervmongo.aio.
SUPPORT_ASYNCIO_CLIENT
= True¶ True if motor package is installed else False
-
cervmongo.aio.
SUPPORT_ASYNCIO_BUCKET
= True¶ True if motor package is installed else False
-
class
cervmongo.aio.
AsyncIOClient
(mongo_uri=None, default_collection=None, **kwargs)[source]¶ Bases:
motor.motor_asyncio.AsyncIOMotorClient
High-level AsyncIOMotorClient subclass with additional methods added for ease-of-use, having some automated conveniences and defaults.
-
async
PAGINATED_QUERY
(collection, limit=20, sort=<Pagination Sort Fields._id: '_id'>, after=None, before=None, page=None, endpoint='/', ordering=-1, query={}, **kwargs)[source]¶ Returns paginated results of collection w/ query.
- Available pagination methods:
- Cursor-based (default)
after
before
limit (results per page, default 20)
- Time-based (a datetime field must be selected)
sort (set to datetime field)
after (records after this time)
before (records before this time)
limit (results per page, default 20)
- Offset-based (not recommended)
limit (results per page, default 20)
page
- Parameters
limit (
int
) –sort (
Pagination Sort Fields
) –after (
Optional
[str
]) –before (
Optional
[str
]) –page (
Optional
[int
]) –endpoint (
str
) –ordering (
int
) –query (
dict
) –
-
async
UPLOAD
(fileobj, filename=None, content_type=None, extension=None, **kwargs)[source]¶ - Parameters
filename (
Optional
[str
]) –content_type (
Optional
[str
]) –extension (
Optional
[str
]) –
-
async
DOWNLOAD
(filename_or_id=None, revision=- 1, skip=None, limit=None, sort=- 1, **query)[source]¶ - Parameters
revision (
int
) –skip (
Optional
[int
]) –limit (
Optional
[int
]) –sort (
int
) –
-
async
DELETE
(collection, record, soft=False, one=False)[source]¶ - Parameters
soft (
bool
) –one (
bool
) –
-
INDEX
(collection, key='_id', sort=1, unique=False, reindex=False)[source]¶ - Parameters
key (
str
) –sort (
int
) –unique (
bool
) –reindex (
bool
) –
-
async
ADD_FIELD
(collection, field, value='', data=False, query={})[source]¶ - Parameters
field (
str
) –value (
Union
[Dict
,List
,str
,int
,float
]) –query (
dict
) –
-
async
REMOVE_FIELD
(collection, field, query={})[source]¶ - Parameters
field (
str
) –query (
dict
) –
- Return type
None
-
async
GET
(collection, id_or_query={}, sort=1, key='_id', count=None, search=None, fields=None, page=None, perpage=False, limit=None, after=None, before=None, empty=None, distinct=None, one=False, **kwargs)[source]¶ - Parameters
id_or_query (
Union
[NewType()
(Document ID
,ObjectId
),str
,Dict
]) –sort (
int
) –key (
str
) –count (
Optional
[bool
]) –search (
Optional
[str
]) –fields (
Optional
[dict
]) –page (
Optional
[int
]) –perpage (
int
) –limit (
Optional
[int
]) –after (
Optional
[str
]) –before (
Optional
[str
]) –distinct (
Optional
[str
]) –one (
bool
) –
-
async
POST
(collection, record_or_records)[source]¶ - Parameters
record_or_records (
Union
[List
,Dict
]) –
-
async
PUT
(collection, record_or_records)[source]¶ creates or replaces record(s) with exact _id provided, _id is required with record object(s)
returns original document, if replaced
- Parameters
record_or_records (
Union
[List
,Dict
]) –
-
async
-
class
cervmongo.aio.
AsyncIODoc
(_id=None, doc_type=None, doc_sample=None, doc_schema=None, doc_id=None, mongo_uri=None, **kwargs)[source]¶ Bases:
cervmongo.aio.AsyncIOClient
Custom MongoClient subclass with customizations for creating standardized documents and adding json schema validation.
- Parameters
doc_type (
Optional
[str
]) –doc_sample (
Union
[Dict
,str
,None
]) –doc_schema (
Union
[Dict
,str
,None
]) –doc_id (
Optional
[str
]) –mongo_uri (
Optional
[str
]) –
-
async
create
(save=False, trigger=None, template='{total}', query={}, **kwargs)[source]¶ - Parameters
save (
bool
) –template (
str
) –query (
dict
) –
-
async
close
()[source]¶ Cleanup client resources and disconnect from MongoDB.
On MongoDB >= 3.6, end all server sessions created by this client by sending one or more endSessions commands.
Close all sockets in the connection pools and stop the monitor threads. If this instance is used again it will be automatically re-opened and the threads restarted unless auto encryption is enabled. A client enabled with auto encryption cannot be used again after being closed; any attempt will raise
InvalidOperation
.Changed in version 3.6: End all server sessions created by this client.
-
class
cervmongo.aio.AsyncIOClient.COLLECTION.
CollectionClient
¶ Convenience class that is instantiated and supplied by the COLLECTION method. It auto-supplies collection to all upper-cased AsyncIOClient methods, where required [source]
-
get_client
()¶
-
GENERATE_ID
(_id=None)¶
-
COLLECTION
(collection)¶
-
async
UPLOAD
(fileobj, filename=None, content_type=None, extension=None, **kwargs)¶
-
async
DOWNLOAD
(filename_or_id=None, revision=- 1, skip=None, limit=None, sort=- 1, **query)¶
-
async
ERASE
(filename_or_id, revision=- 1)¶
-
INDEX
(key='_id', sort=1, unique=False, reindex=False)¶
-
async
ADD_FIELD
(field, value='', data=False, query={})¶
-
async
REMOVE_FIELD
(field, query={})¶
-
async
DELETE
(record_or_records, soft=False)¶
-
async
GET
(id_or_query={}, sort=1, key='_id', count=None, search=None, fields=None, page=None, perpage=False, limit=None, after=None, before=None, empty=None, distinct=None, one=False, **kwargs)¶
-
async
POST
(record_or_records)¶
-
async
PUT
(record_or_records)¶
-
async
PATCH
(original, updates, w=1, upsert=False, multi=False, log=None)¶
-
async
REPLACE
(original, replacement, upsert=False)¶
-
async
SEARCH
(search, **kwargs)¶
-
async
PAGINATED_QUERY
(limit=20, sort=<Pagination Sort Fields._id: "_id">, after=None, before=None, page=None, endpoint='/', ordering=-1, query={}, **kwargs)¶
-
cervmongo.models submodule¶
-
class
cervmongo.models.
DefaultModel
[source]¶ Bases:
object
Intended for use as a base class for externally-facing models. Any models that inherit from this class will: * accept fields using snake_case or camelCase keys * use camelCase keys in the generated OpenAPI spec (when supported) * create an orm_mode Config attrib and have it on by default
from fastapi-utils
-
class
Config
[source]¶ Bases:
object
-
orm_mode
= True¶
-
allow_population_by_field_name
= True¶
-
alias_generator
(*, start_lower: bool = True) → str¶ Converts a snake_case string to camelCase. The start_lower argument determines whether the first letter in the generated camelcase should be lowercase (if start_lower is True), or capitalized (if start_lower is False).
-
-
class
cervmongo.models.
GenericResponse
(*args, **kwargs)[source]¶ Bases:
cervmongo.models._GenericResponse
a premade web API friendly response object
-
class
cervmongo.models.
StandardResponse
(*args, **kwargs)[source]¶ Bases:
cervmongo.models._StandardResponse
a premade web API friendly response object
-
data
: Union[Dict, List, str, int, float]¶
-
-
class
cervmongo.models.
YAMLStandardResponse
(*args, **kwargs)[source]¶ Bases:
cervmongo.models._StandardResponse
a premade web API friendly response object
-
data
: Union[Dict, List, str, int, float]¶
-
-
class
cervmongo.models.
MongoDictResponse
[source]¶ Bases:
dict
the normal response for a single document when using cervmongo.main.SyncIOClient or cervmongo.aio.AsyncIOClient
-
class
cervmongo.models.
MongoListResponse
(cursor=[], _original=None)[source]¶ Bases:
list
the normal response for multiple documents when using cervmongo.main.SyncIOClient or cervmongo.aio.AsyncIOClient
-
flatten
()[source]¶ returns a MongoListResponse containing an array of flattened records, saving record of original
- Return type
List
[Dict
]
-
distinct
(field='_id')[source]¶ returns list of distinct values based on field, defaults to “_id”. supports dot notation for nested values.
- Parameters
field (
str
) –- Return type
List
[Any
]
-
count
()[source]¶ returns the count of the number of records in cursor or list results
- Return type
int
-
cervmongo.utils submodule¶
-
cervmongo.utils.
get_file_meta_information
(fileobj, filename=None, content_type=None, extension=None, **kwargs)[source]¶ - Parameters
filename (
Optional
[str
]) –content_type (
Optional
[str
]) –extension (
Optional
[str
]) –
- Return type
dict
-
cervmongo.utils.
current_datetime
(alt=False)[source]¶ Returns current datetime object by default. Accepts alternate format for string format result.
- Parameters
alt (
str
) –- Return type
datetime
-
cervmongo.utils.
current_date
(alt=False)[source]¶ Returns current date object by default. Accepts alternate format for string format result.
- Parameters
alt (
str
) –- Return type
date
-
cervmongo.utils.
clean_kwargs
(*, ONLY=[], kwargs={})[source]¶ Allows for sanitization of keyword args before passing to another function
- Parameters
ONLY (
list
) –kwargs (
dict
) –
- Return type
dict
-
cervmongo.utils.
parse_string_header
(string)[source]¶ For use when parsing nested data from tabular data formats, such as spreadsheets
- Parameters
string (
str
) –- Return type
str
-
cervmongo.utils.
format_string_for_id
(string)[source]¶ Cleans string to allow for functional, readable, and permissible MongoDB ID
- Parameters
string (
str
) –- Return type
str
-
cervmongo.utils.
return_value_from_dict
(dictionary, key, if_not=' ')[source]¶ - Parameters
dictionary (
dict
) –key (
str
) –if_not (
str
) –
-
cervmongo.utils.
snake2camel
(snake, start_lower=False)[source]¶ Converts a snake_case string to camelCase. The start_lower argument determines whether the first letter in the generated camelcase should be lowercase (if start_lower is True), or capitalized (if start_lower is False).
- Parameters
snake (
str
) –start_lower (
bool
) –
- Return type
str
-
cervmongo.utils.
camel2snake
(camel)[source]¶ Converts a camelCase string to snake_case.
- Parameters
camel (
str
) –- Return type
str
-
cervmongo.utils.
ensure_enums_to_strs
(items)[source]¶ - Parameters
items (
Union
[Sequence
[Union
[Enum
,str
]],Type
[Enum
]]) –
-
cervmongo.utils.
json_dump
(data, pretty=False)[source]¶ - Parameters
data (
dict
) –pretty (
bool
) –
- Return type
str
-
cervmongo.utils.
silent_drop_kwarg
(kwargs, key, reason='')[source]¶ - Parameters
kwargs (
dict
) –key (
str
) –reason (
str
) –