def collections_namedtuple(obj, arguments, callback): """ Implementation of the namedtuple function. This has to be done by processing the namedtuple class template and evaluating the result. """ evaluator = obj.evaluator # Process arguments name = u'jedi_unknown_namedtuple' for c in _follow_param(evaluator, arguments, 0): x = get_str_or_none(c) if x is not None: name = force_unicode(x) break # TODO here we only use one of the types, we should use all. param_contexts = _follow_param(evaluator, arguments, 1) if not param_contexts: return NO_CONTEXTS _fields = list(param_contexts)[0] string = get_str_or_none(_fields) if string is not None: fields = force_unicode(string).replace(',', ' ').split() elif isinstance(_fields, iterable.Sequence): fields = [ force_unicode(get_str_or_none(v)) for lazy_context in _fields.py__iter__() for v in lazy_context.infer() ] fields = [f for f in fields if f is not None] else: return NO_CONTEXTS # Build source code code = _NAMEDTUPLE_CLASS_TEMPLATE.format( typename=name, field_names=tuple(fields), num_fields=len(fields), arg_list=repr(tuple(fields)).replace("u'", "").replace("'", "")[1:-1], repr_fmt='', field_defs='\n'.join( _NAMEDTUPLE_FIELD_TEMPLATE.format(index=index, name=name) for index, name in enumerate(fields))) # Parse source code module = evaluator.grammar.parse(code) generated_class = next(module.iter_classdefs()) parent_context = ModuleContext( evaluator, module, file_io=None, string_names=None, code_lines=parso.split_lines(code, keepends=True), ) return ContextSet( [ClassContext(evaluator, parent_context, generated_class)])
def builtins_getattr(objects, names, defaults=None): # follow the first param for obj in objects: for name in names: string = get_str_or_none(name) if string is None: debug.warning('getattr called without str') continue else: return obj.py__getattribute__(force_unicode(string)) return NO_CONTEXTS
def _add_strings(context, nodes, add_slash=False): string = '' first = True for child_node in nodes: contexts = context.eval_node(child_node) if len(contexts) != 1: return None c, = contexts s = get_str_or_none(c) if s is None: return None if not first and add_slash: string += os.path.sep string += force_unicode(s) first = False return string
def _os_path_join(args_set, callback): if len(args_set) == 1: string = u'' sequence, = args_set is_first = True for lazy_context in sequence.py__iter__(): string_contexts = lazy_context.infer() if len(string_contexts) != 1: break s = get_str_or_none(next(iter(string_contexts))) if s is None: break if not is_first: string += os.path.sep string += force_unicode(s) is_first = False else: return ContextSet( [compiled.create_simple_object(sequence.evaluator, string)]) return callback()
def iterate(): for context in strings: s = get_str_or_none(context) if s is not None: s = func(s) yield compiled.create_simple_object(context.evaluator, s)