This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Fails if models file has local imports #1
Comments
Hi @Nixellion. |
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. # 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()
# ... |
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.
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
The text was updated successfully, but these errors were encountered: