1010# #
1111# ############################################################################ #
1212
13- import os
14- import sys
15- import inspect
1613import subprocess
14+ import sys
1715from contextlib import contextmanager
16+ from pathlib import Path
1817
1918import c_formatter_42 .data
2019
21- CONFIG_FILENAME = ".clang-format"
20+ CONFIG_FILENAME = Path ( ".clang-format" )
2221
23- DATA_DIR = os . path . dirname ( inspect . getfile ( c_formatter_42 .data ))
22+ DATA_DIR = Path ( c_formatter_42 .data . __file__ ). parent
2423
2524
2625@contextmanager
@@ -29,31 +28,29 @@ def _config_context():
2928 If there already is a config in the current directory, it's backed up
3029 then put back in place after clang-format is done running
3130 """
32- config_path = os . path . join ( DATA_DIR , CONFIG_FILENAME )
31+ config_path = DATA_DIR / CONFIG_FILENAME
3332 previous_config = None
3433 try :
35- os . symlink (config_path , CONFIG_FILENAME )
34+ CONFIG_FILENAME . symlink_to (config_path )
3635 except FileExistsError :
37- if not os .path .islink (CONFIG_FILENAME ):
38- with open (CONFIG_FILENAME ) as f :
39- previous_config = f .read ()
40- os .unlink (CONFIG_FILENAME )
41- os .symlink (config_path , CONFIG_FILENAME )
36+ if not CONFIG_FILENAME .is_symlink ():
37+ previous_config = CONFIG_FILENAME .read_text ()
38+ CONFIG_FILENAME .unlink ()
39+ CONFIG_FILENAME .symlink_to (config_path )
4240 try :
4341 yield
4442 finally :
45- os .unlink (CONFIG_FILENAME )
43+ CONFIG_FILENAME .unlink ()
4644 if previous_config is not None :
47- with open (CONFIG_FILENAME , "w" ) as f :
48- f .write (previous_config )
45+ CONFIG_FILENAME .write_text (previous_config )
4946
5047
5148if sys .platform == "linux" :
52- CLANG_FORMAT_EXEC = os . path . join ( DATA_DIR , "clang-format-linux" )
49+ CLANG_FORMAT_EXEC = DATA_DIR / "clang-format-linux"
5350elif sys .platform == "darwin" :
54- CLANG_FORMAT_EXEC = os . path . join ( DATA_DIR , "clang-format-darwin" )
51+ CLANG_FORMAT_EXEC = DATA_DIR / "clang-format-darwin"
5552elif sys .platform == "win32" :
56- CLANG_FORMAT_EXEC = os . path . join ( DATA_DIR , "clang-format-win32.exe" )
53+ CLANG_FORMAT_EXEC = DATA_DIR / "clang-format-win32.exe"
5754else :
5855 raise NotImplementedError ("Your platform is not supported" )
5956
@@ -73,6 +70,6 @@ def clang_format(content: str) -> str:
7370 stderr = subprocess .PIPE ,
7471 )
7572 out , err = process .communicate (input = content .encode ())
76- if process .returncode != 0 :
73+ if process .returncode != 0 : # pragma: no cover
7774 raise RuntimeError (f"clang-format error: { err .decode ()} " )
7875 return out .decode ()
0 commit comments