0

I want to programmatically add 'someapp' into django settings.py file, which looks like this:

before:

...
...
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
]
...

after:

...
...
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'someapp',
]
...

What comes to my mind is the following step:

  1. locate ^INSTALLED_APPS = [ string in the file
  2. locate ] string to indicate the end of string parsed.
  3. fetch all the items within [] to avoid conflict
  4. append someapp to the end of []

This looks like a simple question, but I still want to know how to do it with best practice.

Although this is about python scripting, Bash script with GNU tools, like sed,awk,grep, are also welcomed.

anonymous
  • 1,372
  • 1
  • 17
  • 22
  • well, you have some idea.. why not try it with whatever you know and ask question if you have trouble? I can give you a one-liner with sed/awk but you won't learn much from it... if you know python, then use it... for ex: using fileinput and inplace – Sundeep Apr 01 '17 at 06:27
  • and if you want to try with sed/awk, https://stackoverflow.com/questions/38972736/how-to-select-lines-between-two-patterns is a good place to start with – Sundeep Apr 01 '17 at 06:29

0 Answers0