Coverage for api/database.py: 0%

# Coverage for api/database.py : 0%

## 34 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import importlib

2 import os

3

4 import click

5

6 from shopyo.api.constants import SEP_CHAR

7 from shopyo.api.constants import SEP_NUM

8

9

10 def autoload_models(verbose=False):

11 """

12 Auto imports models from modules/ in desired file. Used so that

13 flask_migrate does not miss models when migrating

14

15 Returns

16 -------

17 None

18 """

19 click.echo("Auto importing models...")

20 click.echo(SEP_CHAR * SEP_NUM)

21

22 for folder in os.listdir("modules"):

23 if folder.startswith("__"):

24 continue

25 elif folder.startswith("box__"):

26 for sub_folder in os.listdir(os.path.join("modules", folder)):

27 if sub_folder.startswith("__"): # ignore __pycache__

28 continue

29 elif sub_folder.endswith(".json"): # box_info.json

30 continue

31 try:

32 to_load_submodel = f"modules.{folder}.{sub_folder}.models"

33 importlib.import_module(to_load_submodel)

34 if verbose:

35 click.echo(f"[x] imported {to_load_submodel}")

36 except Exception as e:

37 if verbose:

38 click.echo(f"[ ] {e}")

39 else:

40 try:

41 to_load = f"modules.{folder}.models"

42 importlib.import_module(to_load)

43 if verbose:

44 click.echo(f"[x] imported {to_load}")

45 except Exception as e:

46 if verbose:

47 click.echo(f"[ ] {e}")

48

49 click.echo("")