Pycharm + Black 自动保存格式化运行脚本

huangapple go评论145阅读模式
英文:

Pycharm + Black with Formatting on Auto-save by Running a Script

问题

我正在尝试编写一个Python脚本,该脚本将安装Black(代码格式化工具),并在Pycharm设置中进行更改,以在自动保存时运行Black并将其添加到外部工具(我正在使用Linux)。我不知道如何开始,以及要编写什么样的代码。

对于我的本地机器,我手动操作,但需要不手动操作并运行脚本,让它执行所有所需步骤。由于参与项目的不仅仅是我一个人,我们所有人都需要具有相同的格式配置,因此需要编写一个Python脚本来实现。

英文:

I am trying to write a Python script that will install Black(code formatter), and do changes in Pycharm settings to run Black on Autosave and add it to external tools(I am using Linux). I didn't know how to start, and what kind of code to write.

For my local machine, I do it manually, but needed to not do it manually and run the script and it does all the needed steps. As people working on the project not only me, we all need to have the same formatting configurations, so need to have a python script for that.

答案1

得分: 1

你可以通过更新PyCharm中工具的XML文件并添加所需的工具来实现。

以下是Python中的函数:

  1. import xml.etree.ElementTree as ET
  2. def add_external_tool(tool_name, command):
  3. your_version = "2023.1.2" # 请将您的PyCharm版本放在这里
  4. # 工具的tools.xml文件路径
  5. tools_file = fr"~/.PyCharm{your_version}/config/tools/tools.xml"
  6. # 加载XML文件
  7. tree = ET.parse(tools_file)
  8. root = tree.getroot()
  9. # 创建一个新的 <tool> 元素
  10. tool = ET.SubElement(root, 'tool')
  11. tool.set('name', tool_name)
  12. tool.set('description', 'My custom tool') # 工具的描述
  13. # 创建带有命令的 <exec> 元素
  14. exec_element = ET.SubElement(tool, 'exec') # 根据您的需求更改
  15. exec_element.text = command
  16. # 将修改后的XML保存回文件
  17. tree.write(tools_file)
  18. # 使用示例
  19. add_external_tool('black', 'which black')

您可以根据需要修改代码以获取有关如何设置 black 格式化程序 的更多信息。

英文:

You can do it with update xml file of tools in pycharm, and add tool you want.

> Here is function in python

  1. import xml.etree.ElementTree as ET
  2. def add_external_tool(tool_name, command):
  3. your_version = &quot;2023.1.2&quot; # Put your pycharm version here
  4. # Path to the tools.xml file
  5. tools_file = fr&quot;~/.PyCharm{your_version}/config/tools/tools.xml&quot;
  6. # Load the XML file
  7. tree = ET.parse(tools_file)
  8. root = tree.getroot()
  9. # Create a new &lt;tool&gt; element
  10. tool = ET.SubElement(root, &#39;tool&#39;)
  11. tool.set(&#39;name&#39;, tool_name)
  12. tool.set(&#39;description&#39;, &#39;My custom tool&#39;) # Your description for tool
  13. # Create the &lt;exec&gt; element with the command
  14. exec_element = ET.SubElement(tool, &#39;exec&#39;) # Change it as you want
  15. exec_element.text = command
  16. # Save the modified XML back to the file
  17. tree.write(tools_file)
  18. # Usage
  19. add_external_tool(&#39;black&#39;, &#39;which black&#39;)

you can modify code as you want was reading to get more information on how to setup black formatter

huangapple
  • 本文由 发表于 2023年6月15日 02:30:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定