Parse and return text content of infiles.

    Keyword arguments:
    args -- program arguments (dict)
    infilenames -- name of user-inputted and/or downloaded file (str)

    Return a list of strings of text.
779824
def get_parsed_text(args, infilename):
    
    parsed_text = []
    if infilename.endswith('.html'):
        # Convert HTML to lxml object for content parsing
        html = lh.fromstring(read_files(infilename))
        text = None
    else:
        html = None
        text = read_files(infilename)

    if html is not None:
        parsed_text = parse_text(html, args['xpath'], args['filter'],
                                 args['attributes'])
    elif text is not None:
        parsed_text = parse_text(text, args['xpath'], args['filter'])
    else:
        if not args['quiet']:
            sys.stderr.write('Failed to parse text from {0}.\n'
                             .format(infilename))
    return parsed_text
1063686
def _get_inputs(self):
        
        # Convert file inputs to a dict, to make it easier to override
        # them with commandline inputs
        file_inputs = self._get_file_inputs()
        try:
            jsonschema.validate(file_inputs, file_input_schema)
        except jsonschema.ValidationError:
            raise SystemExit("ERROR! Input file was invalid")
        input_dict = {}
        for (channel, input_id) in file_inputs.iteritems():
            input_dict[channel] = input_id

        if self.args.inputs:
            for kv_pair in self.args.inputs:
                (channel, input_id) = kv_pair.split('=')
                input_dict[channel] = self._parse_string_to_nested_lists(
                    input_id)

        inputs = []
        for (channel, contents) in input_dict.iteritems():
            inputs.append({
                'channel': channel,
                'data': {
                    'contents': contents
                }
            })
        return inputs
1192051
def _get_args_contents(self):
        
        return ' '.join(
            '%s=%s' % (key, shlex_quote(str(self.args[key])))
            for key in self.args
        ) + ' '
581738
def commandline_text(bytestring):
    ''
    if six.PY3:
        return bytestring
    unicode_string = bytestring.decode(sys.getfilesystemencoding())
    return unicode_string
604751
def data(self):
        
        def read(path):
            with open(path, 'r', encoding='UTF-8') as f:
                return f.read()

        return [
            read(f) for f in self.files
        ]
1145906
def parse_help_text(self, file_path):
        '''  '''
        with open(file_path) as f:
            lines = f.readlines()

        # Parse commands and descriptions, which are separated by a multi-space
        # (any sequence of two or more space characters in a row.
        cmds = []
        descs = []
        for line in lines:
            line = line.strip()
            if len(line) == 0:
                cmds.append('')
                descs.append('')
            else:
                tokens = line.split('  ')
                cmds.append(tokens[0])
                descs.append(''.join(tokens[1:]).strip())

        max_len = len(max(cmds, key=len))

        # Convert commands and descriptions into help text.
        text = ''
        for cmd, desc in zip(cmds, descs):
            if len(cmd) == 0:
                text += '\n'
            else:
                text += self.style.help(cmd.ljust(max_len + 2), desc + '\n')

        return cmds, text
728712
def get_files_from_textfile(textfile_handler):
    """"""
    for line in textfile_handler:
        line = line.rstrip()
        try:
            (image_name, width) = line.rsplit(',', 1)
            width = int(width)
        except ValueError:
            image_name = line
            width = None
        yield (image_name, width)
389124
def _parse(contents=None, file_name=None):
    '''
    
    '''
    if contents:
        pass
    elif file_name and os.path.exists(file_name):
        with salt.utils.files.fopen(file_name, 'r') as ifile:
            contents = salt.utils.stringutils.to_unicode(ifile.read())
    else:
        log.error('File "%s" does not exist', file_name)
        return False

    rules = []
    for line in contents.splitlines():
        if not line:
            continue
        if line.startswith('#'):
            continue
        control_flag = ''
        module = ''
        arguments = []
        comps = line.split()
        interface = comps[0]
        position = 1
        if comps[1].startswith('['):
            control_flag = comps[1].replace('[', '')
            for part in comps[2:]:
                position += 1
                if part.endswith(']'):
                    control_flag += ' {0}'.format(part.replace(']', ''))
                    position += 1
                    break
                else:
                    control_flag += ' {0}'.format(part)
        else:
            control_flag = comps[1]
            position += 1
        module = comps[position]
        if len(comps) > position:
            position += 1
            arguments = comps[position:]
        rules.append({'interface': interface,
                      'control_flag': control_flag,
                      'module': module,
                      'arguments': arguments})
    return rules
1270583
def read_input(self, input_cls, filename, **kwargs):
        
        input_inst = input_cls()
        input_inst.read_input(filename)
        return input_inst.get_data()
504403
def parse(self, fail_callback=None):
        
        # get text fields
        for field in self.field_arguments:
            self.values[field['name']] = self.__get_value(field['name'])
            if self.values[field['name']] is None and field['required']:
                if fail_callback is not None:
                    fail_callback()
                self.__invalid_request(field['error'])
        # get file fields
        for file in self.file_arguments:
            self.files[file['name']] = self.__get_file(file)
            if self.files[file['name']] is None and file['required']:
                if fail_callback is not None:
                    fail_callback()
                self.__invalid_request(file['error'])
187560
def load_data(file_path: str) -> Tuple[List[str], List[str]]:
    
    data = []

    with open(file_path) as f:
        for line in f:
            pairs = line.strip().split()
            sentence, tags = zip(*(pair.split("###") for pair in pairs))
            data.append((sentence, tags))

    return data
1040547
def _parse_from_file(self, filepath, fname,
                         dependencies, recursive, greedy):
        """"""
        #Now that we have the file contents, we can parse them using the parsers
        string = self.tramp.read(filepath)
        pmodules = self.modulep.parse(string, self, filepath=filepath)
        file_mtime = self._get_mod_mtime(filepath)

        for module in pmodules:
            module.change_time = file_mtime
            self.modules[module.name.lower()] = module
            self._modulefiles[fname].append(module.name.lower())

        pprograms = self.modulep.parse(string, self, False)
        for program in pprograms:
            program.change_time = file_mtime
            self.programs[program.name.lower()] = program
            self._programfiles[fname].append(program.name.lower())

        #There may be xml files for the docstrings that also need to be parsed.
        self._parse_docstrings(filepath)
        
        return (pmodules, pprograms)
244557
def _read_file(path):
    '''
    
    '''
    try:
        with salt.utils.files.flopen(path, 'rb') as contents:
            return [salt.utils.stringutils.to_str(line) for line in contents.readlines()]
    except (OSError, IOError):
        return ''
466461
def paras(self, fileids=None) -> Generator[str, str, None]:
        

        def _recurse_to_strings(my_dict: Dict[str, Any]) -> List[str]:
            """Internal accumulator method."""
            vals = []  # type: List[str]
            m_keys = sorted(list(my_dict.keys()))
            for mkey in m_keys:
                if isinstance(my_dict[mkey], dict):
                    vals += _recurse_to_strings(my_dict[mkey])
                else:
                    vals += [my_dict[mkey]]
            return vals

        text_sections = []  # type: List[str]
        for doc in self.docs(fileids):
            text_data = _recurse_to_strings(doc['text'])
            for text_part in text_data:
                skip = False
                if self.skip_keywords:
                    for keyword in self.skip_keywords:
                        if keyword in text_part:
                            skip = True
                if not skip:
                    text_sections.append(text_part)
            paras = (''.join(text_sections)).split(self.paragraph_separator)
            for para in paras:
                yield para
854005
def parse(self, filenames):
        
        filenames = list_strings(filenames)

        read_ok = []
        for fname in filenames:
            try:
                fh = open(fname)
            except IOError:
                logger.warning("Cannot open file %s" % fname)
                continue

            try:
                self._read(fh, fname)
                read_ok.append(fname)

            except self.Error as e:
                logger.warning("exception while parsing file %s:\n%s" % (fname, str(e)))
                continue

            finally:
                fh.close()

        # Add read_ok to the list of files that have been parsed.
        self._filenames.extend(read_ok)
        return read_ok
263486
def get_file_contents(path, binary=False):
    
    # if this isn't a text file, we should raise an IOError
    f = open(path, 'r')
    file_contents = f.read()
    f.close()
    if not binary and file_contents.find('\000') >= 0:
        raise IOError('Expected text file, got binary file')
    return file_contents
871938
def parseFile( self, file_or_filename, parseAll=False ):
        
        try:
            file_contents = file_or_filename.read()
        except AttributeError:
            with open(file_or_filename, "r") as f:
                file_contents = f.read()
        try:
            return self.parseString(file_contents, parseAll)
        except ParseBaseException as exc:
            if ParserElement.verbose_stacktrace:
                raise
            else:
                # catch and re-raise exception from here, clears out pyparsing internal stack trace
                raise exc