class CompletionTestsUnix(unittest.TestCase): def setUp(self): self._tmpd = None self._context = HotwireContext() def tearDown(self): if self._tmpd: shutil.rmtree(self._tmpd) self._context = None def _setupTree1(self): self._tmpd = tempfile.mkdtemp(prefix='hotwiretest') self._context.chdir(self._tmpd) testd = os.path.join(self._tmpd, 'testdir') os.mkdir(testd) open(os.path.join(self._tmpd, 'testf'), 'w').close() os.symlink(testd, os.path.join(self._tmpd, 'foolink')) def testCd1(self): self._setupTree1() cds = CdCompleter() results = list(cds.completions('test', self._tmpd)) self.assertEquals(len(results), 1) self.assertEquals(results[0].target.path, os.path.join(self._tmpd, 'testdir')) self.assertEquals(results[0].suffix, 'dir/') def testCd2(self): self._setupTree1() cds = CdCompleter() results = list(cds.completions('foo', self._tmpd)) self.assertEquals(len(results), 1) self.assertEquals(results[0].target.path, os.path.join(self._tmpd, 'foolink')) self.assertEquals(results[0].suffix, 'link/')
def execute(self, context, args, options=[]): regexp = args[0] if len(args) == 2: path = args[1] else: path = context.cwd regexp = args[0] comp_regexp = re.compile( regexp, (('-i' in options) and re.IGNORECASE or 0) | re.UNICODE) walk_builtin = BuiltinRegistry.getInstance()['walk'] newctx = HotwireContext(context.cwd) for fobj in walk_builtin.execute(newctx, [path]): fp = None try: fp = open_text_file(fobj.path) for i, line in enumerate(fp): match = comp_regexp.search(line) if match: yield FileStringMatch(fobj.path, line[:-1], i, match.start(), match.end()) fp.close() except OSError, e: pass except UnicodeDecodeError, e: pass
def apply(context, *args): _("""Like Unix xargs - take input and convert to arguments.""") newargs = list(args) for argument in context.input: if not isinstance(argument, str): argument = str(argument) newargs.append(argument) new_context = HotwireContext(initcwd=context.cwd) # TODO - pull in resolver from shell.py? Should this function expand # aliases? pipeline = Pipeline.create(new_context, None, *newargs) pipeline.execute_sync(assert_all_threaded=True) for result in pipeline.get_output(): yield result
def script(*args, **kwargs): from hotwire.command import Pipeline, HotwireContext if not 'context' in kwargs: kwargs['context'] = HotwireContext(initcwd=(kwargs.get('cwd', None))) return Pipeline.create(kwargs['context'], kwargs.get('resolver', None), *args)
def setUp(self): self._tmpd = None self._context = HotwireContext()