Python操作xml打开pycharm项目

想使用alfred打开pycharm项目,网上找了下有个python版本的workflow但是我的是 pycharm 2021.1 版本不好使,于是改了一下。pycharm打开的项目信息保存在 ~/Library/Application Support/JetBrains/PyCharm2021.1/options/recentProjects.xml 中,格式如下:

<application>
  <component name="RecentProjectsManager">
    <option name="additionalInfo">
      <map>
        <entry key="$USER_HOME$/PycharmProjects">
          <value>
            <RecentProjectMetaInfo frameTitle="PycharmProjects – mro.py" opened="true" projectWorkspaceId="1zUzHVRnd0K7VJhEi2HWWteP84G">
              <option name="binFolder" value="$APPLICATION_HOME_DIR$/bin" />
              <option name="build" value="PY-211.6693.115" />
              <option name="buildTimestamp" value="1617710427956" />
              <frame x="4" y="23" width="1436" height="877" />
              <option name="productionCode" value="PY" />
              <option name="projectOpenTimestamp" value="1651048636586" />
            </RecentProjectMetaInfo>
          </value>
        </entry>
        <entry key="$USER_HOME$/anzhihe/python/script">
          <value>
            <RecentProjectMetaInfo frameTitle="script – srebot.py" opened="true" projectWorkspaceId="28NERWHPDRW2stelFxQuz409UgB">
              <option name="binFolder" value="$APPLICATION_HOME_DIR$/bin" />
              <option name="build" value="PY-211.6693.115" />
              <option name="buildTimestamp" value="1617710427956" />
              <frame x="4" y="23" width="1436" height="877" />
              <option name="productionCode" value="PY" />
              <option name="projectOpenTimestamp" value="1651048636586" />
            </RecentProjectMetaInfo>
          </value>
        </entry>
      </map>
    </option>
    <option name="lastProjectLocation" value="$USER_HOME$/PycharmProjects" />
  </component>
</application>

用python脚本把xml中的项目信息读取出来,再用alfred-workflow打开就好了。

#!/usr/bin/python
# encoding: utf-8

import sys

# Workflow3 supports Alfred 3's new features. The `Workflow` class
# is also compatible with Alfred 2.
# xml.etree.ElementTree --- ElementTree XML API
# https://docs.python.org/zh-cn/3/library/xml.etree.elementtree.html
from workflow import Workflow3
import xml.etree.ElementTree as ET
import os

LAUNCHER_DIR = '/usr/local/bin/charm'
RECENT_XPATH = ".//component[@name='RecentProjectsManager']/option[@name='additionalInfo']/map/"


def parse_start_script(path=LAUNCHER_DIR):
    run_path, config_path = None, None
    for line in open(path, 'r'):
        line = line.strip()
        if line.startswith('CONFIG_PATH = '):
            config_path = line.split('=')[1].strip().replace("u'", '').rstrip("'")
        elif line.startswith('RUN_PATH = '):
            run_path = line.split('=')[1].strip().replace("u'", '').rstrip("'")
    return run_path, config_path


def main(wf):
    pycharm_path, config_path = parse_start_script()
    home_dir = os.path.expanduser('~')

    root = ET.parse(config_path + '/options/recentProjects.xml')

    project_paths = (
        el.attrib['key'].replace('$USER_HOME$', home_dir)
        for el in root.findall(RECENT_XPATH)
    )

    paths_with_names = (
        (path, os.path.basename(path))
        for path in project_paths
    )

    query = None
    if wf.args:
        query = wf.args[0]

    results = wf.filter(query, paths_with_names, lambda r: r[1], fold_diacritics=False)

    for path, name in results:
        wf.add_item(
            title=name,
            subtitle=path,
            uid=path,
            arg=path,
            valid=True,
            icontype='fileicon',
            icon=pycharm_path
        )

    wf.send_feedback()


if __name__ == '__main__':
    # Create a global `Workflow3` object
    wf = Workflow3()
    # Call your entry function via `Workflow3.run()` to enable its
    # helper functions, like exception catching, ARGV normalization,
    # magic arguments etc.
    sys.exit(wf.run(main))

参考:

anzhihe 安志合个人博客,版权所有 丨 如未注明,均为原创 丨 转载请注明转自:https://chegva.com/5267.html | ☆★★每天进步一点点,加油!★★☆ | 

您可能还感兴趣的文章!

发表评论

电子邮件地址不会被公开。 必填项已用*标注