Skip to content

Instantly share code, notes, and snippets.

@victorfsf
Last active January 16, 2017 11:53
Show Gist options
  • Save victorfsf/0c53f0fc271ab7231b5278cd31042323 to your computer and use it in GitHub Desktop.
Save victorfsf/0c53f0fc271ab7231b5278cd31042323 to your computer and use it in GitHub Desktop.
IPython startup scripts
"""IPython startup script to detect and inject VIRTUAL_ENV's site-packages dirs. # noqa
IPython can detect virtualenv's path and injects it's site-packages dirs into sys.path.
But it can go wrong if IPython's python version differs from VIRTUAL_ENV's.
This module fixes it looking for the actual directories. We use only old stdlib
resources so it can work with as many Python versions as possible.
References:
http://stackoverflow.com/a/30650831/443564
http://stackoverflow.com/questions/122327/how-do-i-find-the-location-of-my-python-site-packages-directory
https://github.com/ipython/ipython/blob/master/IPython/core/interactiveshell.py#L676
Author: Henrique Bastos <henrique@bastos.net>
License: BSD
"""
def load_virtualenv():
from warnings import warn
import os
import sys
virtualenv = os.environ.get('VIRTUAL_ENV')
if virtualenv:
version = os.listdir(os.path.join(virtualenv, 'lib'))[0]
site_packages = os.path.join(
virtualenv, 'lib', version, 'site-packages'
)
lib_dynload = os.path.join(virtualenv, 'lib', version, 'lib-dynload')
if not (os.path.exists(site_packages) and os.path.exists(lib_dynload)):
msg = 'Virtualenv site-packages discovery went wrong for %r' % \
repr([site_packages, lib_dynload])
warn(msg)
sys.path.insert(0, site_packages)
sys.path.insert(1, lib_dynload)
if __name__ == '__main__':
load_virtualenv()
del load_virtualenv
"""IPython startup script to load and configure the django environment settings
Author: Victor Ferraz <github.com/victorfsf>
"""
def load_django():
from warnings import warn
import os
import sys
try:
import django
module = 'DJANGO_SETTINGS_MODULE'
project_path = os.getcwd()
settings = os.getenv(
module, '%s.settings' % os.path.basename(project_path)
)
os.environ.setdefault(module, settings)
sys.path.insert(0, project_path)
try:
django.setup()
except Exception:
warn('Could not load django settings: %s' % settings)
except ImportError:
pass
if __name__ == '__main__':
load_django()
del load_django
# flake8:noqa
try:
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q, F, Count, Max, Min, Avg, Case, When
from django.apps import apps
for app, models in apps.all_models.items():
for model in models.values():
setattr(__builtin__, model.__name__, model)
except ImportError:
pass
# flake8:noqa
try:
from site_modules.modules import site
from site_modules.utils import send_to_bucket, finish_bucket
from contas.models import UserAlpha
user = UserAlpha.objects.get(username='leandro')
for module in site.modules_for_user(user):
for bucket in module.get_buckets():
setattr(module, 'bucket_%s' % bucket.name.replace('-', '_'), bucket)
setattr(__builtin__, module.name.replace('-', '_'), module)
except ImportError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment