File: utils.py

package info (click to toggle)
python-odmantic 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,488 kB
  • sloc: python: 8,646; sh: 110; javascript: 45; makefile: 34; xml: 13
file content (34 lines) | stat: -rw-r--r-- 1,193 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import re


def is_dunder(name: str) -> bool:
    return name.startswith("__") and name.endswith("__")


def raise_on_invalid_key_name(name: str) -> None:
    # https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names
    if name.startswith("$"):
        raise TypeError("key_name cannot start with the dollar sign ($) character")
    if "." in name:
        raise TypeError("key_name cannot contain the dot (.) character")


def raise_on_invalid_collection_name(collection_name: str, cls_name: str) -> None:
    # https://docs.mongodb.com/manual/reference/limits/#Restriction-on-Collection-Names
    if "$" in collection_name:
        raise TypeError(f"Invalid collection name for {cls_name}: cannot contain '$'")
    if collection_name == "":
        raise TypeError(f"Invalid collection name for {cls_name}: cannot be empty")
    if collection_name.startswith("system."):
        raise TypeError(
            f"Invalid collection name for {cls_name}:" " cannot start with 'system.'"
        )


def to_snake_case(s: str) -> str:
    tmp = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", s)
    return re.sub("([a-z0-9])([A-Z])", r"\1_\2", tmp).lower()


class Undefined:
    pass