Python operates xml to open pycharm project

I want to use alfred to open the pycharm project. I found a python version of the workflow on the Internet, but my pycharm 2021.1 version is not easy to use, so I changed it. The project information opened by pycharm is saved in ~/Library/Application Support/JetBrains/PyCharm2021.1/options/recentProjects.xml in the following format:

 <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>

Use the python script to read the project information in the xml, and then open it with 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/en-us/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)) 

refer to:

This article is reprinted from: https://chegva.com/5267.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.