def wiki_root_url(self): url_base = self.setting('url-base') if url_base is None: raise UserFeedback("The url-base setting must be provided.") if not url_base.startswith("http"): raise UserFeedback("The url-base setting should start with https.") return "%s%s" % (url_base, self.setting('wiki-path'))
def read_data(self, this=True): with open(self.data_file(this), "rb") as f: data = json.load(f) if hasattr(data, 'keys'): msg = "Data storage format has changed. Please clear your dexy cache by running dexy with '-r' option." raise UserFeedback(msg) return data
def map_files(self): """ Generates a map of files present in the project directory. """ exclude = self.exclude_dirs() filemap = {} for dirpath, dirnames, filenames in os.walk('.', followlinks=True): for x in exclude: if x in dirnames and not x in self.include: dirnames.remove(x) if '.nodexy' in filenames: dirnames[:] = [] elif 'pip-delete-this-directory.txt' in filenames: msg = s("""pip left an old build/ file lying around, please remove this before running dexy""") raise UserFeedback(msg) else: for filename in filenames: filepath = posixpath.normpath( posixpath.join(dirpath, filename)) filemap[filepath] = {} filemap[filepath]['stat'] = os.stat( os.path.join(dirpath, filename)) filemap[filepath]['ospath'] = os.path.normpath( os.path.join(dirpath, filename)) filemap[filepath]['dir'] = os.path.normpath(dirpath) return filemap
def handle_fail(self, name, e): msg = self.import_err_msg % (name, e) if self.setting('error-on-import-fail'): msg += "\nYou can set error-on-import-fail to False in pydoc args to log this instead." raise UserFeedback(msg) else: self.log_warn(e)
def repo_from_path(path): """ Initializes a pygit Repository instance from a local repo at 'path' """ repo = pygit2.init_repository(path, False) if repo.is_empty: raise UserFeedback("no git repository was found at '%s'" % path) return repo, None
def find_page_info_by_title(self, page_title=None): space_key = self.setting('space-key') if page_title is None: page_title = self.setting('page-title') if space_key is None: raise UserFeedback("A space-key must be provided.") if page_title is None: raise UserFeedback("A page-title must be provided") params = {"spaceKey": space_key, "title": page_title} matching_pages = self.get_path("content", params) if matching_pages['size'] == 1: return matching_pages['results'][0] elif matching_pages['size'] == 0: return None else: print(matching_pages) raise Exception("Should only be 0 or 1 matching pages.")
def handle_response_code(self, response): if response.status_code < 400: pass elif response.status_code in range(400, 500): raise UserFeedback(response.json()['message']) elif response.status_code in (500, ): raise Exception("\nServer error %s:\n%s" % (response.status_code, response.json()['message'])) else: print(response.text) raise Exception("Not set up to handle status code %s" % response.status_code)
def assert_dexy_dirs_exist(self): """ Raise a UserFeedback error if user has tried to run dexy without setting up necessary directories first. """ self.detect_dot_dexy_files() self.update_cache_directory() if not self.dexy_dirs_exist(): msg = "You need to run 'dexy setup' in this directory first." raise UserFeedback(msg) self.deprecate_logs_directory()
def parse_additional_ipython_args(self): raw_ipython_args = self.setting('ipython-args') if raw_ipython_args: if isinstance(raw_ipython_args, basestring): user_ipython_args = raw_ipython_args.split() elif isinstance(raw_ipython_args, list): assert isinstance(raw_ipython_args[0], basestring) user_ipython_args = raw_ipython_args else: raise UserFeedback("ipython-args must be a string or list of strings") return user_ipython_args else: return []
def configure_casper_script(self, wd, port, cellmetas): scriptfile = os.path.join(wd, self.setting('script')) cellmetafile = os.path.join( wd, "%s-cellmetas.js" % self.input_data.baserootname()) default_scripts_dir = os.path.join(os.path.dirname(__file__), "ipynbcasper") if not os.path.exists(scriptfile): # look for a matching default script script_setting = self.setting('script') filepath = os.path.join(default_scripts_dir, script_setting) if os.path.exists(filepath): with open(filepath, "r") as f: js = f.read() else: default_scripts = os.listdir(default_scripts_dir) args = ( self.setting('script'), ", ".join(default_scripts), ) raise UserFeedback( "No script file named '%s' found.\nAvailable built-in scripts: %s" % args) else: with open(scriptfile, "r") as f: js = f.read() args = { 'width': self.setting('width'), 'height': self.setting('height'), 'port': port, 'name': self.input_data.baserootname(), 'ext': self.setting('image-ext'), 'cell_timeout': self.setting('cell-timeout') } with open(scriptfile, "w") as f: f.write(js % args) with open(cellmetafile, "w") as f: json.dump(cellmetas, f)
def credentials(self): try: authstring = self.setting('authstring') except UserFeedback: authstring = None try: username = self.setting('username') password = self.setting('password') except UserFeedback: username = None password = None if authstring is not None: return {"headers": {"Authorization": "Basic %s" % authstring}} elif username is not None and password is not None: return {"auth": (username, password)} else: raise UserFeedback( "Must provide an authstring or both username and password.")
def iter_dexy_dirs(self): """ Iterate over the required dirs (e.g. artifacts, logs) """ for d in self.__class__._required_dirs: dirpath = self.__dict__[d] safety_filepath = os.path.join(dirpath, self.safety_filename) try: stat = os.stat(dirpath) except OSError: stat = None if stat: if not file_exists(safety_filepath): msg = s("""You need to manually delete the '%s' directory and then run 'dexy setup' to create new directories. This should just be a once-off issue due to a change in dexy to prevent accidentally deleting directories which dexy does not create. """) % dirpath raise UserFeedback(msg) yield (dirpath, safety_filepath, stat)
def command_string_args(self): args = super(Asciidoctor, self).command_string_args() stylesheet = self.setting('stylesheet') if stylesheet: stylesdir = os.path.abspath( os.path.join(os.path.dirname(__file__), 'asciidoctor')) if not os.path.exists(stylesdir): msg = "Asciidoctor stylesheet directory not found at '%s'" raise InternalDexyProblem(msg % stylesdir) args['ss'] = "-a stylesheet=%s -a stylesdir=%s" % (stylesheet, stylesdir) if not os.path.exists(os.path.join(stylesdir, stylesheet)): msg = "No stylesheet file named '%s' was found in directory '%s'. Files found: %s" stylesheets = os.listdir(stylesdir) raise UserFeedback( msg % (stylesheet, stylesdir, ", ".join(stylesheets))) else: args['ss'] = '' return args
def handle_fail(self, name, e): msg = self.import_err_msg % (name, e) if self.setting('error-on-import-fail'): raise UserFeedback(msg) else: self.log_debug(e)
def throw(self, err_message): raise UserFeedback("template throw from '%s': %s" % (self.filter_instance.key, err_message))
def t_idio_IDIOCLOSE(t): r'(-->)|(\*/)' if not t.lexer.idio_expect_closing_block: raise UserFeedback("Unexpected code %s in an idio block" % t.value) return t
def section_output(self): """ Runs the code in sections and returns an iterator so we can do custom stuff. """ input_sections = self.input_data.items() # If we want to automatically record values of local variables in the # script we are running, we add a section at the end of script do_record_vars = self.setting('record-vars') if do_record_vars: if not self.setting('save-vars-to-json-cmd'): raise UserFeedback( "You specified record-vars but this option isn't available since SAVE_VARS_TO_JSON_CMD is not set for this filter." ) section_text = self.setting( 'save-vars-to-json-cmd') % self.input_data.basename() self.log_debug("Adding save-vars-to-json-cmd code:\n%s" % section_text) input_sections.append(('dexy--save-vars', section_text)) if not self.setting('add-new-files'): docstr = self._instance_settings['add-new-files'][0] self._instance_settings['add-new-files'] = (docstr, ".json") search_terms = self.prompt_search_terms() env = self.setup_env() if self.setting('ps1'): ps1 = self.setting('ps1') self.log_debug("Setting PS1 to %s" % ps1) env['PS1'] = ps1 if self.setting('ps2'): ps2 = self.setting('PS2') self.log_debug("Setting PS2 to %s" % ps2) env['PS2'] = ps2 if self.setting('ps3'): ps3 = self.arg_value('PS3') self.log_debug("Setting PS3 to %s" % ps3) env['PS3'] = ps3 if self.setting('ps4'): ps4 = self.arg_value('PS4') self.log_debug("Setting PS4 to %s" % ps4) env['PS4'] = ps4 env['TERM'] = self.setting('term') timeout = self.setup_timeout() initial_timeout = self.setup_initial_timeout() self.log_debug("timeout set to '%s'" % timeout) if self.setting('use-wd'): wd = self.parent_work_dir() else: wd = os.getcwd() executable = self.setting('executable') self.log_debug("about to spawn new process '%s' in '%s'" % (executable, wd)) # Spawn the process try: proc = pexpect.spawn(executable, cwd=wd, env=env) except pexpect.ExceptionPexpect as e: if "The command was not found" in unicode(e): raise InactivePlugin(self) else: raise self.log_debug("Capturing initial prompt...") initial_prompt = self.setting('initial-prompt') try: if initial_prompt: proc.expect(initial_prompt, timeout=initial_timeout) elif self.setting('prompt-regex'): proc.expect(search_terms, timeout=initial_timeout) else: proc.expect_exact(search_terms, timeout=initial_timeout) except pexpect.TIMEOUT: if self.setting('initial-prompt'): match = self.setting('initial-prompt') else: match = search_terms msg = "%s failed at matching initial prompt within %s seconds. " % ( self.__class__.__name__, initial_timeout) msg += "Received '%s', tried to match with '%s'" % (proc.before, match) msg += "\nExact characters received:\n" for i, c in enumerate(proc.before): msg += "chr %02d: %s\n" % (i, ord(c)) msg += "The developer might need to set a longer initial prompt timeout or the regexp may be wrong." raise InternalDexyProblem(msg) start = proc.before + proc.after self.log_debug(u"Initial prompt captured!") self.log_debug(unicode(start)) for section_key, section_text in input_sections: section_transcript = start start = "" lines = self.lines_for_section(section_text) for l in lines: self.log_debug(u"Sending '%s'" % l) section_transcript += start proc.send(l.rstrip() + self.setting('send-line-ending')) try: if self.setting('prompt-regex'): proc.expect(search_terms, timeout=timeout) else: proc.expect_exact(search_terms, timeout=timeout) self.log_debug(u"Received '%s'" % unicode(proc.before, errors='replace')) section_transcript += self.strip_newlines(proc.before) start = proc.after except pexpect.EOF: self.log_debug("EOF occurred!") raise DexyEOFException() except pexpect.TIMEOUT as e: for c in proc.before: print ord(c), ":", c msg = "pexpect timeout error. failed at matching prompt within %s seconds. " % timeout msg += "received '%s', tried to match with '%s'" % ( proc.before, search_terms) msg += "something may have gone wrong, or you may need to set a longer timeout" self.log_warn(msg) raise UserFeedback(msg) except pexpect.ExceptionPexpect as e: raise UserFeedback(unicode(e)) except pexpect.EOF as e: raise UserFeedback(unicode(e)) if self.setting('strip-regex'): section_transcript = re.sub(self.setting('strip-regex'), "", section_transcript) yield section_key, section_transcript if self.setting('add-new-files'): self.add_new_files() try: proc.close() except pexpect.ExceptionPexpect: msg = "process %s may not have closed for %s" msgargs = (proc.pid, self.key) raise UserFeedback(msg % msgargs) if proc.exitstatus and self.setting('check-return-code'): self.handle_subprocess_proc_return(self.setting('executable'), proc.exitstatus, section_transcript)
def throw(self, err_message): if hasattr(self, 'filter_instance'): raise UserFeedback("template throw from '%s': %s" % (self.filter_instance.key, err_message)) else: raise UserFeedback("template throw: %s" % (err_message))