def IsPythonFile(filename): """Return True if filename is a Python file.""" if os.path.splitext(filename)[1] == '.py': return True try: with open(filename, 'rb') as fd: encoding = tokenize.detect_encoding(fd.readline)[0] # Check for correctness of encoding. with py3compat.open_with_encoding(filename, encoding=encoding) as fd: fd.read() except UnicodeDecodeError: encoding = 'latin-1' except (IOError, SyntaxError): # If we fail to detect encoding (or the encoding cookie is incorrect - which # will make detect_encoding raise SyntaxError), assume it's not a Python # file. return False try: with py3compat.open_with_encoding(filename, mode='r', encoding=encoding) as fd: first_line = fd.readlines()[0] except (IOError, IndexError): return False return re.match(r'^#!.*\bpython[23]?\b', first_line)
def ReadFile(filename, logger=None): """Read the contents of the file. An optional logger can be specified to emit messages to your favorite logging stream. If specified, then no exception is raised. This is external so that it can be used by third-party applications. Arguments: filename: (unicode) The name of the file. logger: (function) A function or lambda that takes a string and emits it. Returns: The contents of filename. Raises: IOError: raised if there was an error reading the file. """ try: with open(filename, 'rb') as fd: encoding = tokenize.detect_encoding(fd.readline)[0] except IOError as err: if logger: logger(err) raise try: with py3compat.open_with_encoding(filename, mode='r', encoding=encoding) as fd: source = fd.read() return source, encoding except IOError as err: if logger: logger(err) raise
def ReadFile(filename, logger=None): """Read the contents of the file. An optional logger can be specified to emit messages to your favorite logging stream. If specified, then no exception is raised. This is external so that it can be used by third-party applications. Arguments: filename: (unicode) The name of the file. logger: (function) A function or lambda that takes a string and emits it. Returns: The contents of filename. Raises: IOError: raised if there was an error reading the file. """ try: encoding = file_resources.FileEncoding(filename) # Preserves line endings. with py3compat.open_with_encoding( filename, mode='r', encoding=encoding, newline='') as fd: lines = fd.readlines() line_ending = file_resources.LineEnding(lines) source = '\n'.join(line.rstrip('\r\n') for line in lines) + '\n' return source, line_ending, encoding except IOError as err: # pragma: no cover if logger: logger(err) raise
def testInPlaceReformattingEmpty(self): unformatted_code = u'' expected_formatted_code = u'' with tempfile.NamedTemporaryFile( suffix='.py', dir=self.test_tmpdir) as testfile: testfile.write(unformatted_code.encode('UTF-8')) testfile.seek(0) p = subprocess.Popen(YAPF_BINARY + ['--in-place', testfile.name]) p.wait() with py3compat.open_with_encoding( testfile.name, mode='r', encoding='utf-8') as fd: reformatted_code = fd.read() self.assertEqual(reformatted_code, expected_formatted_code)
def FormatFile(filename, style_config=None, lines=None, print_diff=False, verify=True, in_place=False, logger=None): """Format a single Python file and return the formatted code. Arguments: filename: (unicode) The file to reformat. in_place: (bool) If True, write the reformatted code back to the file. logger: (io streamer) A stream to output logging. remaining arguments: see comment at the top of this module. Returns: Tuple of (reformatted_code, encoding, changed). reformatted_code is None if the file is sucessfully written to (having used in_place). reformatted_code is a diff if print_diff is True. Raises: IOError: raised if there was an error reading the file. ValueError: raised if in_place and print_diff are both specified. """ _CheckPythonVersion() if in_place and print_diff: raise ValueError('Cannot pass both in_place and print_diff.') original_source, encoding = ReadFile(filename, logger) reformatted_source, changed = FormatCode(original_source, style_config=style_config, filename=filename, lines=lines, print_diff=print_diff, verify=verify) if in_place: with py3compat.open_with_encoding(filename, mode='w', encoding=encoding) as fd: fd.write(reformatted_source) return None, encoding, changed return reformatted_source, encoding, changed
def WriteReformattedCode(filename, reformatted_code, in_place, encoding): """Emit the reformatted code. Write the reformatted code into the file, if in_place is True. Otherwise, write to stdout. Arguments: filename: (unicode) The name of the unformatted file. reformatted_code: (unicode) The reformatted code. in_place: (bool) If True, then write the reformatted code to the file. encoding: (unicode) The encoding of the file. """ if in_place: with py3compat.open_with_encoding(filename, mode='w', encoding=encoding) as fd: fd.write(reformatted_code) else: py3compat.EncodeAndWriteToStdout(reformatted_code, encoding)
def FormatFile(filename, style_config=None, lines=None, print_diff=False, verify=True, in_place=False): """Format a single Python file and return the formatted code. Arguments: filename: (unicode) The file to reformat. remaining arguments: see comment at the top of this module. in_place: (bool) If True, write the reformatted code back to the file. Returns: Pair of (reformatted_code, encoding). reformatted_code is None if the file is sucessfully written to (having used in_place). reformatted_code is a diff if print_diff is True. Raises: IOError: raised if there was an error reading the file. ValueError: raised if in_place and print_diff are both specified. """ _CheckPythonVersion() if in_place and print_diff: raise ValueError('Cannot pass both in_place and print_diff.') original_source, encoding = ReadFile(filename, logging.warning) reformatted_source = FormatCode(original_source, style_config=style_config, filename=filename, lines=lines, print_diff=print_diff, verify=verify) if in_place: with py3compat.open_with_encoding(filename, mode='w', encoding=encoding) as fd: fd.write(reformatted_source) return None, encoding return reformatted_source, encoding
def ReadFile(filename, logger=None): """Read the contents of the file. An optional logger can be specified to emit messages to your favorite logging stream. If specified, then no exception is raised. This is external so that it can be used by third-party applications. Arguments: filename: (unicode) The name of the file. logger: (function) A function or lambda that takes a string and emits it. Returns: The contents of filename. Raises: IOError: raised if there was an error reading the file. """ try: encoding = file_resources.FileEncoding(filename) # Preserves line endings. with py3compat.open_with_encoding(filename, mode='r', encoding=encoding, newline='') as fd: lines = fd.readlines() line_ending = file_resources.LineEnding(lines) source = '\n'.join(line.rstrip('\r\n') for line in lines) + '\n' return source, line_ending, encoding except IOError as err: # pragma: no cover if logger: logger(err) raise except UnicodeDecodeError as err: # pragma: no cover if logger: logger( 'Could not parse %s! Consider excluding this file with --exclude.', filename) logger(err) raise
def ReadFile(filename, logger=None): """Read the contents of the file. An optional logger can be specified to emit messages to your favorite logging stream. If specified, then no exception is raised. This is external so that it can be used by third-party applications. Arguments: filename: (unicode) The name of the file. logger: (function) A function or lambda that takes a string and emits it. Returns: The contents of filename. Raises: IOError: raised during an error if a logger is not specified. """ try: with open(filename, 'rb') as fd: encoding = tokenize.detect_encoding(fd.readline)[0] except IOError as err: if logger: logger(err) else: raise try: with py3compat.open_with_encoding(filename, mode='r', encoding=encoding) as fd: source = fd.read() return source, encoding except IOError as err: if logger: logger(err) else: raise
def test_with_latin_encoding(self): file1 = os.path.join(self.test_tmpdir, 'testfile1') with py3compat.open_with_encoding(file1, mode='w', encoding='latin-1') as f: f.write(u'#! /bin/python2\n') self.assertTrue(file_resources.IsPythonFile(file1))
def _MakeTempFileWithContents(self, filename, contents): path = os.path.join(self.test_tmpdir, filename) with py3compat.open_with_encoding(path, mode='w', encoding='utf-8') as f: f.write(py3compat.unicode(contents)) return path