notes about auto-activating virtualenv in ipython
authordsc <david.schoonover@gmail.com>
Sat, 24 Dec 2011 22:26:37 +0000 (14:26 -0800)
committerdsc <david.schoonover@gmail.com>
Sat, 24 Dec 2011 22:26:37 +0000 (14:26 -0800)
README.md
crisishaiku/models.py
crisishaiku/static/img/fcic-cover.jpg [moved from static/img/fcic-cover.jpg with 100% similarity]
crisishaiku/views.py [new file with mode: 0644]
etc/flask.cfg

index 7fb2c46..0099bb7 100644 (file)
--- a/README.md
+++ b/README.md
@@ -65,6 +65,7 @@ Finally, let's fix the missing dict (which I dug up from somewhere).
     - Zips of the haikus, report
     - Source on GitHub
 
+
 ### Models
 
 - User
@@ -84,6 +85,54 @@ execfile('.env/bin/activate_this.py', dict(__file__='.env/bin/activate_this.py')
 
 Which resolves problems with the env's `site-packages` dir not getting picked up recursively.
 
+To do this automatically in ipython, I do a little work on startup. In my `ipython_config.py` file,
+in `c.TerminalIPythonApp.exec_lines` I have (among many other things) an import for
+a module `dsc_ipython` where I keep all my ipython specific code, including:
+
+````python
+import os, sys
+from os.path import isfile, join, relpath, abspath
+
+def activate_virtual_env(base=None, dirnames=None, override=False):
+    """ Introspects whether there's an inactive virtual environment here,
+        and if so, activates it.
+        
+        - `base` -- Path at which to search.
+            Default: `os.getcwd()`
+        - `dirnames` -- Iterable of directory names to search for env activation file.
+            Default: `('.env', '.virtualenv', 'env', 'virtualenv',)`
+        - `override` -- Whether to clobber an existing environment if detected.
+            Default: False
+    """
+    if base     is None: base     = os.getcwd()
+    if dirnames is None: dirnames = ('.', '.env', '.virtualenv', 'env', 'virtualenv',)
+    
+    # Don't auto-activate if we've already got a virtual env
+    if 'VIRTUAL_ENV' in os.environ:
+        sys.stderr.write('Existing Virtual Environment detected (%s) ... ' % os.environ['VIRTUAL_ENV'])
+        if not override:
+            sys.stderr.write('Aborting!\n')
+            return False
+        sys.stderr.write('Overriding!\n')
+    
+    for env in dirnames:
+        activate_file = abspath(join(base, env, 'bin/activate_this.py'))
+        if not isfile(activate_file):
+            continue
+        
+        sys.stderr.write('Found Virtual Environment (%s) ... ' % relpath(join(base, env)))
+        execfile(activate_file, dict(__file__=activate_file))
+        sys.stderr.write('Activated!\n')
+        return True
+    
+
+# automatically check on startup, using defaults
+activate_virtual_env()
+
+````
+
+
+
 
 ## Notes
 
index 8080b22..23bce30 100644 (file)
@@ -41,7 +41,6 @@ class TimestampMixin(object):
 # likes = db.Table('likes',
 #     Column('user_id',    Integer, ForeignKey('user.id')),
 #     Column('target_id',  Integer, ForeignKey('target.id')),
-#     Column('ctime',      DateTime, default=func.now()),
 # )
 
 # tags2haikus = db.Table('tags',
@@ -54,6 +53,7 @@ class Target(db.Model):
     "A polymorphic pointer to a Comment, Haiku, or Section."
     id        = Column(Integer, primary_key=True)
 
+
 class Section(db.Model):
     "A chunk of the Report. May represent a chapter/section node, or a paragraph of text."
     id        = Column(Integer, primary_key=True)
@@ -61,18 +61,21 @@ class Section(db.Model):
     text      = Column(Text())
     parent    = relationship('Section', backref=backref('children', lazy='dynamic'))
 
+
 class Haiku(db.Model):
     id        = Column(Integer, primary_key=True)
     text      = Column(Text())
     context   = relationship('Section', backref=backref('haikus' lazy='dynamic'))
     start     = Column(Integer) # character index into the section where this haiku begins
 
+
 class Like(db.Model):
     id        = Column(Integer, primary_key=True)
     user      = relationship('User', backref=backref('likes', lazy='dynamic'))
     target    = relationship('Target')
     ctime     = Column(DateTime, default=func.now())
 
+
 class Comment(TimestampMixin, db.Model):
     id        = Column(Integer, primary_key=True)
     target    = relationship('Target')
@@ -81,6 +84,7 @@ class Comment(TimestampMixin, db.Model):
     approved  = Column(Boolean) # spam filter approval
     deleted   = Column(Boolean)
 
+
 class Tag(db.Model):
     id        = Column(Integer, primary_key=True)
     name      = Column(String(120), unique=True)
@@ -92,6 +96,7 @@ class Tag(db.Model):
 #     Column('haiku_id',  Integer, ForeignKey('haiku.id')),
 # )
 
+
 class User(TimestampMixin, db.Model):
     id        = Column(Integer, primary_key=True)
     username  = Column(String(30), unique=True)
@@ -110,7 +115,8 @@ class User(TimestampMixin, db.Model):
     # verification_token = Column(String(60))
     
     # comments
-    # likes
+    # likes    = relationship('Target', secondary=likes, backref=backref('tags', lazy='dynamic'))
+    
     # tags = relationship('UserTag')
     
     
new file mode 100644 (file)
index 0000000000000000000000000000000000000000..b92c26a2e00adafed098b4912c777f81d1710b04
--- /dev/null
@@ -0,0 +1,6 @@
+from crisishaiku import app
+
+@app.route('/')
+def index():
+    return 'Hello World!'
+
diff --git a/crisishaiku/views.py b/crisishaiku/views.py
index 721af2cdb52b595c25a4bf5de7a173c1300552e5..2b36ffbb158cb7c727758fbf49a298f15eb9e2f1 100644 (file)
--- a/crisishaiku/views.py
@@ -1,4 +1,4 @@
-SQLALCHEMY_DATABASE_URI = 'sqlite:///var/haikus.db'
-TWITTER_CONSUMER_KEY    = 'tQww8eCQbbtF12hHPTJA',
-TWITTER_CONSUMER_SECRET = 'OE9AIRTkxWTBJU88PJjqGI4soC6k0pUOtrRur0Q8d4w',
+SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/haiku.db'
+TWITTER_CONSUMER_KEY    = 'tQww8eCQbbtF12hHPTJA'
+TWITTER_CONSUMER_SECRET = 'OE9AIRTkxWTBJU88PJjqGI4soC6k0pUOtrRur0Q8d4w'
 
diff --cc etc/flask.cfg
Simple merge