def _activate(self, path): absfpath = abspath(expanduser(path)) self.publish(C_PRE_ACTIVATE, name=absfpath) if True: vbin = to_vbin(absfpath) vlib = to_vlib(absfpath) # compute e.g. <venv>/lib/python2.6. # we call bullshit if they have a more than one dir; # it might be a chroot but i dont think it's a venv python_dir = glob.glob(opj(vlib, 'python*/')) if len(python_dir) == 0: raise RuntimeError('no python dir in {0}'.format(vlib)) if len(python_dir) > 1: err = "multiple python dirs matching in {0}".format(vlib) raise RuntimeError(err) python_dir = python_dir[0] # this bit might enable switching between two venv's # that are be "no-global-site" vs "use-global-site" # .. tabling it for now # site_file = opj(python_dir, 'site.py') # assert ope(site_file) # tmp = dict(__file__=site_file) # execfile(site_file, tmp) # tmp['main']() # some environment variable manipulation that would # normally be done by 'source bin/activate', but is # not handled by activate_this.py #path = get_path().split(':') os.environ['VIRTUAL_ENV'] = absfpath sandbox = dict(__file__=opj(vbin, 'activate_this.py')) execfile(opj(vbin, 'activate_this.py'), sandbox) self.reset_path = sandbox['prev_sys_path'] # libraries like 'datetime' can very occasionally fail on import # if this isnt done, and i'm not sure why activate_this.py doesnt # accomplish it. it might have something to do with venv's using # mixed pythons (different versions) or mixed --no-site-packages # tabling it for now # dynload = opj(python_dir, 'lib-dynload') # sys.path.append(dynload) # NB: this rehash will update bins but iirc kills aliases! msg = '$PATH was adjusted to {0}'.format(os.environ['PATH']) smash_log.debug(msg) self.report('Adjusting $PATH') msg = 'rehashing aliases' smash_log.info(msg) self.shell.magic('rehashx') self.publish(C_POST_ACTIVATE, absfpath)
def on_file_input(self, fpath): def doit(_fpath, _suffix, _opener, _rest): if ope(_fpath) and not isdir(_fpath): if _opener is not None: self.report( 'Using _opener "{0}" for "{1}"'.format(_opener, _suffix)) return '{0} {1}'.format(_opener, _fpath + _rest) else: msg = "Legit file input, but no _suffix alias could be found for " + \ _suffix self.report(msg) if is_editable(_fpath): self.report( "File looks like ASCII text, assuming I should edit it") return doit(_fpath, _suffix, 'ed', _rest) else: msg = 'Attempted file input, but path "{0}" does not exist' msg = msg.format(fpath) smash_log.info(msg) fpath = abspath(expanduser(fpath)) if isdir(fpath) and self.automatic_cd: self.report('cd ' + fpath) self.smash.shell.magic('pushd ' + fpath) return True # handle input like .foo/bin/python, # this shouldn't really even get here but # ipython throws a syntax error if os.access(fpath, os.X_OK): return self.smash.shell.system(fpath) # isolate file:col:row syntax if not ope(fpath) and ':' in fpath: tmp = fpath fpath = tmp[:tmp.find(':')] rest = tmp[tmp.find(':'):] else: rest = '' # isolate file suffix, guess an opener suffix = splitext(fpath)[-1][1:].lower() opener = self.suffix_aliases.get(suffix, None) cmd = doit(fpath, suffix, opener, rest) if cmd: self.smash.shell.run_cell(cmd) return True
def _event_set_project_map(self, key, val): """ final word in cleaning/verifying/binding input that goes to project_map. project_map should have only pristine data. Therefore DO NOT abstract the helper method "_bind_project". """ def _bind_project(name, path): """ NOTE: be aware this is also used for re-binding """ if not ope(path): self.report("bound project {0} to nonexistent {1}".format( name, path)) self.update_interface() name = key clean_name = clean_project_name(name) clean_path = abspath(expanduser(val)) dict.__setitem__( self.project_map, clean_name, clean_path) _bind_project(clean_name, clean_path)
fname_user_config = 'config.py' fname_editor_config = 'editor.json' LOG_FORMAT_DEFAULT = ('[%(name)s:%(levelname)s:%(process)d] ' '%(pathname)s:%(lineno)-4d' ' - %(funcName)s:\n %(message)s') LOG_HANDLER_DEFAULTS = { 'class': 'logging.handlers.RotatingFileHandler', 'level': 'INFO', 'formatter': 'detailed', 'mode': 'a', 'maxBytes': 10485760, 'backupCount': 3, } SMASH_DIR = expanduser('~/.smash') SMASH_BIN = os.path.join(SMASH_DIR, 'bin') D_SMASH_LOGS = os.path.join(SMASH_DIR, 'logs') DIR_SMASH_ETC = os.path.join(SMASH_DIR, 'etc') SMASHLIB_DIR = dirname(__file__) USER_CONFIG_PATH = os.path.join(SMASH_DIR, fname_user_config) ALIAS_CONFIG_PATH = os.path.join(DIR_SMASH_ETC, 'aliases.json') MACRO_CONFIG_PATH = os.path.join(DIR_SMASH_ETC, 'macros.json') EDITOR_CONFIG_PATH = os.path.join(DIR_SMASH_ETC, fname_editor_config) ENV_CONFIG_PATH = os.path.join(DIR_SMASH_ETC, 'env.json') PROMPT_CONFIG_PATH = os.path.join(DIR_SMASH_ETC, 'prompt.json') P_CODE_FILE = { '.py': 'python', '.pp': 'puppet', '.md': 'docs',