Last active
February 8, 2019 06:29
-
-
Save avalanchy/cdcda3d4f8c474f6b0fa83f791a72e8f to your computer and use it in GitHub Desktop.
List all factory boy factories in a way expected by shell_plus command from django-extensions. Python 3.7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
import re | |
def factories_lookup(base_dir): | |
"""Yields all model factories in a way expected by shell_plus command. | |
Returned generator is evaluated only when running shell_plus. Evaluation | |
itself on my machine takes ± 3.63 ms. | |
Usage: | |
SHELL_PLUS_POST_IMPORTS = factories_lookup(BASE_DIR) | |
""" | |
modules = pathlib.Path(base_dir).glob('**/factories.py') | |
for module in modules: | |
m = str(module) | |
p = m.replace(base_dir, '') | |
p = p[1:] | |
p = p.replace('/', '.') | |
p = p.replace('.py', '') | |
with open(m, 'r') as f: | |
c = re.findall('class ([A-Za-z]+Factory)', f.read()) | |
if c: | |
yield p, c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment