Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Fails if models file has local imports #1

Open
Nixellion opened this issue Dec 17, 2020 · 3 comments
Open

Fails if models file has local imports #1

Nixellion opened this issue Dec 17, 2020 · 3 comments

Comments

@Nixellion
Copy link

Nixellion commented Dec 17, 2020

Hi! Trying to use this and can't figure out if there's a way to use it on a project with local imports in models file?

I have a project like:

ProjectDir
---app.py
---models.py
---configuration.py

and and import configuration.py in models.py, and when trying to use peewee-erd on such file it can't import configuration.py and fails

@rominf
Copy link
Owner

rominf commented Dec 18, 2020

Hi @Nixellion.
Thanks for reaching out. Unfortunately, I have the possibility to work on a project to fix it myself. PRs are welcome, however.

@p6l-richard
Copy link

p6l-richard commented Mar 3, 2021

Hi @Nixellion,

I had the same issue as you did. The quick and dirty workaround for me was to uncomment the imports that make the run fail and afterwards recommenting.
Fails:

# models.py
import peewee
import datetime
from database import database

# Base to inherit from


class BaseModel(peewee.Model):
    class Meta:
        database = database.db

# DB Models


class User(BaseModel):
    email = peewee.CharField()
    created_on = peewee.DateTimeField(default=datetime.datetime.now)
    is_authorized = peewee.BooleanField(default=False)
    client = peewee.CharField()
    uuid = peewee.UUIDField()
# ...

Works:

# models.py
import peewee
import datetime
# from database import database

# Base to inherit from


class BaseModel(peewee.Model):
    pass
    # class Meta:
    #    database = database.db

# DB Models


class User(BaseModel):
    email = peewee.CharField()
    created_on = peewee.DateTimeField(default=datetime.datetime.now)
    is_authorized = peewee.BooleanField(default=False)
    client = peewee.CharField()
    uuid = peewee.UUIDField()
# ...

@Nixellion
Copy link
Author

Nixellion commented Mar 3, 2021

I ended up writing a simple parser into Markdown mermaid syntax instead. Here's the code, I'm not sure if it's a working one though, might need some tweaking:

import os
import re
from paths import APP_DIR

file_to_read = "dbo.py"
class_re = r'class (?P<classname>.*)\(.*'
field_re = r'(.*)ForeignKeyField\((?P<classname>.*),(.*)'
var_re = r'(?P<variable>.*) = (.*)Field(.*)'

filepath = os.path.join(APP_DIR, file_to_read)

with open(filepath, "r") as f:
    lines = f.readlines()

current_class = None
output = "```mermaid\ngraph TD\n"

for line in lines:
    current_target = None
    var_name = None
    match = re.match(class_re, line)
    if match:
        current_class = match.groupdict()['classname']
    else:
        match = re.match(field_re, line)
        if match:
            current_target = match.groupdict()['classname']
        else:
            match = re.match(var_re, line)
            if match:
                var_name = match.groupdict()['variable']

    if current_class is not None and current_target is not None:
        output += f"    {current_class} --> {current_target}\n"
    # elif current_class is not None and var_name is not None:
    #     output += f"    {current_class} : {var_name}\n"

output += "```"

print(output)

Output text then works in many Markdown enabled apps that include mermaid, like Gitea (which was my use case), Joplin, etc.

It does not support attributes yet, but mermaid syntax does support all that, so it can be added in the same way.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants