def set(self, *args): """ set a value in kinbaku's persistent configuration """ if not args: return if isinstance(args[0], dict): self.write(json.dumps(args[0])) return try: obj = eval(args[0]) except SyntaxError: # ie one=two,three=four if len(args)==1: args = dict([ arg.split('=') for arg in args[0].split(',') ]) #ie one=two three=four else: args = dict([ arg.split('=') for arg in args ]) report("Updating config with: {data}",data=args) else: if isinstance(obj,dict): args=obj else: raise Exception,obj current = self.dct or {} current.update(args) fhandle = self.open() fhandle.write(json.dumps(current)) fhandle.close()
def init_pyscope(self, fpath): """ initialize pythoscope with codebase ( will be <codebase-shadow>/.pythonscope ) """ from pythoscope import init_project self.workspace = (fpath + path('/.pythoscope')) if self.workspace.exists(): report("Workspace exists.. wiping it") from kinbaku.util import remove_recursively remove_recursively(self.workspace) init_project(fpath)
def display_results(result): """ console.draw_line(msg="inside context") report("codebase", codebase) #report(" test_files: "); report(*[fname for fname in codebase]) test_search = codebase.search("zam"); #report(" test_search: {results}",results=str(test_search)) import IPython;IPython.Shell.IPShellEmbed(argv=[])() """ if isinstance(result,list): #report(*result) for x in result: print ' ',x elif isinstance(result,dict): report(**result) else: pass
def open(self, mode='w'): """ """ assert self.fpath if not os.path.exists(self.fpath): report("Writing empty config file to {fp}",fp=self.fpath) try: fhandle = open(self.fpath, mode) except IOError: # file doesnt exist? fhandle = open(self.fpath,'w') fhandle.close() return self.open(mode) fhandle.write(json.dumps({})) fhandle.close() return self.open() return open(self.fpath, mode)
def __rshift__(self,fpath): """ mirrors a fpath into sandbox: if this is called multiple times, it will get a fresh copy of the originating file each time.. """ if self.debug: report('mirroring "{fpath}" in sandbox', fpath=fpath.name) namebase = fpath.namebase try: mod = generate.create_module(self.project, namebase) except RopeError,e: if "already exists" in str(e): ## Should not get here because we're wiping existing projects, right? name_would_be = os.path.join(self.pth_shadow, fpath.name) if os.path.exists(name_would_be): remove_recursively(name_would_be) return self>>fpath else: raise Exception,['wait, what?', str(e), name_would_be] else: raise e
def __exit__(self, type, value, tb): console.draw_line(msg=" __exit__ ") if not any([type, value, tb]): report(" closing project:", self.project.close()) report(" removing shadow", remove_recursively(self.pth_shadow)) else: report("exit with error", type, value, tb)
def generate(self, input_file_or_dir, originals=True, imports=True): """ Generates empty unittests from project at @input_file_or_dir. If input is a single file, the result will be sent to stdout. """ from kinbaku.codebase import plugin as CodeBase postprocessors = [] if originals: postprocessors.append(self.originals) if imports: postprocessors.append(self.imports) with CodeBase(input_file_or_dir, gloves_off=True, workspace=None) as codebase: if not codebase.python_files: report('No files') sys.exit(1) self.codebase = codebase results = self._generate(input_file_or_dir, imports=imports, codebase=codebase) for pp in postprocessors: results=pp([x for x in results]) for fname,tname,generated_test in results: print generated_test
def pythoscope_vox(*args, **kargs): report(' ',*args, **kargs)
def __enter__(self): """ """ console.draw_line(msg="__enter__") report(" running _open") return self
def wipe(self): """ destroys all of the persistent settings """ report("Wiping kinbaku configuration in {fp}",fp=self.fpath) self.set({})