Read the AST for the given Ruby source file
177500
def read_ast(file)
      @ast_cache ||= Rufus::Lru::Hash.new(5)
      if @ast_cache.include?(file)
        @ast_cache[file]
      else
        @ast_cache[file] = uncached_read_ast(file)
      end
    end
568845
def parse_ss_def_file(self, contents):
        ''''''
        mapping = {}
        for l in [l.strip() for l in contents.split('\n') if l.strip() and not(l.strip().startswith('#'))]:
            tokens = l.split()
            if len(tokens) != 2:
                raise RosettaFileParsingException('Lines in a secondary structure definition file must have exactly two entries.')

            positions = parse_range(tokens[0])
            ss = sorted(set(tokens[1].upper()))
            for p in positions:
                if mapping.get(p) and mapping[p] != ss:
                    raise RosettaFileParsingException('There are conflicting definitions for residue %d (%s and %s).' % (p, ''.join(mapping[p]), ''.join(ss)))
                mapping[p] = ss
        self.data = mapping
1304753
def read_tree(treeish, opts = {})
      arr_opts = []
      arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
      arr_opts += [treeish]
      command('read-tree', arr_opts)
    end
940283
def _get_file_content(source):
    
    filepath = os.path.join('siglists', source + '.txt')

    lines = []
    with resource_stream(__name__, filepath) as f:
        for i, line in enumerate(f):
            line = line.decode('utf-8', 'strict').strip()
            if not line or line.startswith('#'):
                continue

            try:
                re.compile(line)
            except Exception as ex:
                raise BadRegularExpressionLineError(
                    'Regex error: {} in file {} at line {}'.format(
                        str(ex),
                        filepath,
                        i
                    )
                )

            lines.append(line)

    if source in _SPECIAL_EXTENDED_VALUES:
        lines = lines + _SPECIAL_EXTENDED_VALUES[source]

    return tuple(lines)
12638
def fetch_rails_source(file, options = {})
        # use Rails template resolution mechanism if possible (find_template)
        source_format = request_format if defined?(request_format)

        if source_format && context_scope.respond_to?(:lookup_context) # Rails 3
          lookup_proc = lambda do |partial|
            if ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR < 2
              context_scope.lookup_context.find(file, [], partial)
            else # Rails 3.2 and higher
              # pull format directly from rails unless it is html
              request_format = context_scope.request.format.to_sym
              source_format = request_format unless request_format == :html
              context_scope.lookup_context.find(file, [], partial, [], { :formats => [source_format] })
            end
          end
          template = lookup_proc.call(false) rescue nil
          template ||= lookup_proc.call(true) rescue nil
          template.identifier if template
        elsif source_format && context_scope.respond_to?(:view_paths) # Rails 2
          template = context_scope.view_paths.find_template(file, source_format, false)
          template.filename if template
        end
      end
1112375
def write_AST(wdl_file, outdir=None):
    '''
    
    '''
    if outdir is None:
        outdir = os.getcwd()
    with open(os.path.join(outdir, 'AST.out'), 'w') as f:
        with open(wdl_file, 'r') as wdl:
            wdl_string = wdl.read()
            ast = wdl_parser.parse(wdl_string).ast()
            f.write(ast.dumps(indent=2))
960613
def _read_python_source(self, filename):
        
        try:
            f = open(filename, "rb")
        except IOError as err:
            self.log_error("Can't open %s: %s", filename, err)
            return None, None
        try:
            encoding = tokenize.detect_encoding(f.readline)[0]
        finally:
            f.close()
        with _open_with_encoding(filename, "r", encoding=encoding) as f:
            return _from_system_newlines(f.read()), encoding
984561
def parse_source_file(filename):
    

    # can't use codecs.open(filename, 'r', 'utf-8') here b/c ast doesn't
    # work with unicode strings in Python2.7 "SyntaxError: encoding
    # declaration in Unicode string" In python 2.7 the string can't be
    # encoded and have information about its encoding. That is particularly
    # problematic since source files include in their header information
    # about the file encoding.
    # Minimal example to fail: ast.parse(u'# -*- coding: utf-8 -*-')

    with open(filename, 'rb') as fid:
        content = fid.read()
    # change from Windows format to UNIX for uniformity
    content = content.replace(b'\r\n', b'\n')

    try:
        node = ast.parse(content)
        return node, content.decode('utf-8')
    except SyntaxError:
        return None, content.decode('utf-8')
962202
def parse_ast(source, filename=None):
    """"""
    if isinstance(source, text_type) and sys.version_info[0] == 2:
        # ast.parse() on Python 2 doesn't like encoding declarations
        # in Unicode strings
        source = CODING_COOKIE_RE.sub(r'\1', source, 1)
    return ast.parse(source, filename or '<unknown>')
103859
def read_file(rel_path, paths=None, raw=False, as_list=False, as_iter=False,
              *args, **kwargs):
    '''
        
    '''
    if not rel_path:
        raise ValueError("rel_path can not be null!")
    paths = str2list(paths)
    # try looking the file up in a directory called static relative
    # to SRC_DIR, eg assuming metrique git repo is in ~/metrique
    # we'd look in ~/metrique/static
    paths.extend([STATIC_DIR, os.path.join(SRC_DIR, 'static')])
    paths = [os.path.expanduser(p) for p in set(paths)]
    for path in paths:
        path = os.path.join(path, rel_path)
        logger.debug("trying to read: %s " % path)
        if os.path.exists(path):
            break
    else:
        raise IOError("path %s does not exist!" % rel_path)
    args = args if args else ['rU']
    fd = open(path, *args, **kwargs)
    if raw:
        return fd

    if as_iter:
        return read_in_chunks(fd)
    else:
        fd_lines = fd.readlines()

    if as_list:
        return fd_lines
    else:
        return ''.join(fd_lines)
219669
def read(cls, source):
        
        if hasattr(source, "read"):
            return cls.fromfile(source)
        if os.path.exists(source):
            return cls.fromfile(source)
        if isinstance(source, six.string_types):
            source = source.encode("utf8")
        return cls.fromstring(source)
1298949
def ast_from_file(self, filepath, modname=None, fallback=True, source=False):
        """"""
        try:
            filepath = modutils.get_source_file(filepath, include_no_ext=True)
            source = True
        except modutils.NoSourceFile:
            pass
        if modname is None:
            try:
                modname = ".".join(modutils.modpath_from_file(filepath))
            except ImportError:
                modname = filepath
        if (
            modname in self.astroid_cache
            and self.astroid_cache[modname].file == filepath
        ):
            return self.astroid_cache[modname]
        if source:
            from astroid.builder import AstroidBuilder

            return AstroidBuilder(self).file_build(filepath, modname)
        if fallback and modname:
            return self.ast_from_module_name(modname)
        raise exceptions.AstroidBuildingError(
            "Unable to build an AST for {path}.", path=filepath
        )
1059947
def reads(s, filename=None, loader=None, implicit_tuple=True, allow_errors=False):
  """"""
  return ast.reads(s,
      filename=filename or '<input>',
      loader=loader or default_loader,
      implicit_tuple=implicit_tuple,
      allow_errors=allow_errors)
388304
def parse_file(path, target_ext=nil)
      return nil if path.nil?
      return nil if File.directory? path
      _load_from_file path, target_ext
      # if File.directory? path
      #   _load_from_directory path
      # else
      #   _load_from_file path, target_ext
      # end
    end
770762
def check(src, dst):  # type: (str,str)->None
    
    was_bad = False
    try:
        _ = ast.parse(src)
    except Exception as exc:
        was_bad = True
        major, minor = sys.version_info[:2]
        logger.warning(
            "failed to parse source file "
            "with Python {0}.{1}'s builtin AST. Switch to manual "
            "or stop using deprecated Python 2 syntax. AST error message: {2}".format(
                major, minor, exc
            )
        )

    try:
        _ = ast.parse(dst)
    except Exception as exc:
        if was_bad:
            # I didn't make it worse.
            pass
        else:
            # ok, this made the file worse. It must be jiggle_version's fault.
            print(dst)
            logger.error(dst)
            raise
1159189
def from_file(filename, encoding='utf-8')
      mode = "rb:#{encoding}"
      mode = "rb" if RUBY_VERSION < '1.9'
      input = File.open(filename, mode) {|f| f.read() }
      compile(parse(input), filename, 1)
      return self
    end
1271801
def _from_io(self, source: IO):
        
        read = source.read

        if unpack('>I', source.read(4))[0] != ClassFile.MAGIC:
            raise ValueError('invalid magic number')

        # The version is swapped on disk to (minor, major), so swap it back.
        self.version = unpack('>HH', source.read(4))[::-1]

        self._constants.unpack(source)

        # ClassFile access_flags, see section #4.1 of the JVM specs.
        self.access_flags.unpack(read(2))

        # The CONSTANT_Class indexes for "this" class and its superclass.
        # Interfaces are a simple list of CONSTANT_Class indexes.
        self._this, self._super, interfaces_count = unpack('>HHH', read(6))
        self._interfaces = unpack(
            f'>{interfaces_count}H',
            read(2 * interfaces_count)
        )

        self.fields.unpack(source)
        self.methods.unpack(source)
        self.attributes.unpack(source)