def process(self): if self.prev_ext == '.txt': self.process_packages() elif self.prev_ext == '.py': self.process_file() else: raise InternalDexyProblem("Should not have ext %s" % self.prev_ext) self.output_data.save()
def run(self): methods = {} for name in dir(self): if name.startswith("do_"): method = getattr(self, name) docs = inspect.getdoc(method).splitlines()[0].strip() if not docs: raise InternalDexyProblem("You must define docstring for %s" % name) methods[name.replace("do_", "")] = (docs, method) return methods
def process_tree(tree, add_to_dir): for entry in tree: obj = repo[entry.oid] if obj.__class__.__name__ == 'Blob': doc_key = os.path.join(add_to_dir, entry.name) self.add_doc(doc_key, obj.data) elif obj.__class__.__name__ == 'Tree': process_tree(obj, os.path.join(parent_dir, entry.name)) else: raise InternalDexyProblem(obj.__class__.__name__)
def update_attributes_from_kwargs(self, kwargs): """ Updates instance values from a dictionary of kwargs, checking that the attribute names are also present in defaults dict. """ for key, value in kwargs.items(): if not key in dexy.utils.defaults: msg = "invalid kwarg '%s' being passed to wrapper, not defined in defaults dict" raise InternalDexyProblem(msg % key) setattr(self, key, value)
def persist(self): if self.connected_to == 'existing': assert os.path.exists(self.data_file(read=False)) elif self.connected_to == 'working': self.assert_location_is_in_project_dir(self.data_file(read=False)) self._storage.commit() shutil.copyfile(self.working_file(), self.data_file(read=False)) else: msg = "Unexpected 'connected_to' value %s" msgargs = self.connected_to raise InternalDexyProblem(msg % msgargs)
def __init__(self, key, ext, storage_key, settings, wrapper): self.key = key self.ext = ext self.storage_key = storage_key self.wrapper = wrapper self.initialize_settings(**settings) self._data = None self.state = None self.name = self.setting('canonical-name') if not self.name: msg = "Document must provide canonical-name setting to data." raise InternalDexyProblem(msg) elif self.name.startswith("("): raise Exception() self.transition('new')
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 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)