Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FromImportNode modifyed in memory but not saved to the file #221

Open
rayantlbn opened this issue Dec 10, 2023 · 0 comments
Open

FromImportNode modifyed in memory but not saved to the file #221

rayantlbn opened this issue Dec 10, 2023 · 0 comments

Comments

@rayantlbn
Copy link

Dear RedBaron Support,

I'm currently working on modifying an url.py file in django project using RedBaron. This file already contains two FromImportNode nodes. I'm attempting to add a new import statement (from django.conf.urls.static import static) to these existing nodes.

Additionally, I need to insert this new import before the urlpatterns = [...] statement within the file.

However, despite successfully adding the new import to the FromImportNode nodes, I'm facing challenges when trying to save these changes using file.write. Strangely, other modifications, such as inserting Count_urls, are being saved correctly.

like :
Count_urls = red.find("assignment", target=lambda x: x.dumps() == "urlpatterns")
Count_urls.value.append(f'path("accounts/", include("django.contrib.auth.urls")),')

red.append('\n')
with open(url_path, 'w') as file:
    file.write(red.dumps())  

Could you kindly assist me in understanding how to properly add the new import to the existing FromImportNode, insert it before urlpatterns, and ensure that these modifications are successfully saved using RedBaron's functionality?

Thank you very much for your help.

my code :

def modify_urls_App(url_path):

with open(url_path, 'r') as file:
    code = file.read()
red = RedBaron(code)

#import to add
new_import = RedBaron("from django.conf.urls.static import static")

# FromImportNode nodes
from_import_nodes = red.find_all('FromImportNode')

# adding new_import not saved to red
if from_import_nodes:
    from_import_nodes.append(new_import[0])
    print(from_import_nodes)

this code is workins

Count_urls = red.find("assignment", target=lambda x: x.dumps() == "urlpatterns")
Count_urls.value.append(f'path("accounts/", include("django.contrib.auth.urls")),')

with open(url_path, 'w') as file:
    file.write(red.dumps())    
@rayantlbn rayantlbn changed the title FromImportNode modifyed not enrgistred FromImportNode modifyed in memory but not saved to the file Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant