From: dsc Date: Sat, 24 Dec 2011 22:26:37 +0000 (-0800) Subject: notes about auto-activating virtualenv in ipython X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=3539fec792c669374a6e646f69102f4bf8f7fd00;p=crisishaiku.git notes about auto-activating virtualenv in ipython --- diff --git a/README.md b/README.md index 7fb2c46..0099bb7 100644 --- 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 diff --git a/crisishaiku/models.py b/crisishaiku/models.py index 8080b22..23bce30 100644 --- a/crisishaiku/models.py +++ b/crisishaiku/models.py @@ -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') diff --git a/crisishaiku/views.py b/crisishaiku/views.py new file mode 100644 index 0000000..b92c26a --- /dev/null +++ b/crisishaiku/views.py @@ -0,0 +1,6 @@ +from crisishaiku import app + +@app.route('/') +def index(): + return 'Hello World!' + diff --git a/etc/flask.cfg b/etc/flask.cfg index 721af2c..2b36ffb 100644 --- a/etc/flask.cfg +++ b/etc/flask.cfg @@ -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'