forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
130 lines (103 loc) · 4.08 KB
/
Copy path__init__.py
File metadata and controls
130 lines (103 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
"""List and open JetBrains IDE projects.
Synopsis: <trigger> <filter>"""
import os
from shutil import which
from xml.etree import ElementTree
from albertv0 import *
__iid__ = "PythonInterface/v0.3"
__prettyname__ = "Jetbrains IDE Projects"
__version__ = "1.2"
__trigger__ = "jb "
__author__ = "Markus Richter, Thomas Queste"
__dependencies__ = []
default_icon = os.path.dirname(__file__) + "/jetbrains.svg"
HOME_DIR = os.environ["HOME"]
paths = [ # <Name for config directory>, <possible names for the binary/icon>
["CLion", "clion"],
["DataGrip", "datagrip"],
["GoLand", "goland"],
["IntelliJIdea",
"intellij-idea-ue-bundled-jre intellij-idea-ultimate-edition idea-ce-eap idea-ue-eap idea idea-ultimate"],
["PhpStorm", "phpstorm"],
["PyCharm", "pycharm pycharm-eap charm"],
["RubyMine", "rubymine"],
["WebStorm", "webstorm"],
]
# find the executable path and icon of a program described by space-separated lists of possible binary-names
def find_exec(namestr: str):
for name in namestr.split(" "):
executable = which(name)
if executable:
icon = iconLookup(name) or default_icon
return executable, icon
return None
# parse the xml at path, return all recent project paths and the time they were last open
def get_proj(path):
r = ElementTree.parse(path).getroot() # type:ElementTree.Element
add_info = None
items = dict()
for o in r[0]: # type:ElementTree.Element
if o.attrib["name"] == 'recentPaths':
for i in o[0]:
items[i.attrib["value"]] = 0
else:
if o.attrib["name"] == 'additionalInfo':
add_info = o[0]
if len(items) == 0:
return []
if add_info is not None:
for i in add_info:
for o in i[0][0]:
if o.tag == 'option' and 'name' in o.attrib and o.attrib["name"] == 'projectOpenTimestamp':
items[i.attrib["key"]] = int(o.attrib["value"])
return [(items[e], e.replace("$USER_HOME$", HOME_DIR)) for e in items]
def handleQuery(query):
if query.isTriggered:
query.disableSort()
binaries = {}
projects = []
for app in paths:
config_path = "config/options/recentProjectDirectories.xml"
if app[0] == "IntelliJIdea":
config_path = "config/options/recentProjects.xml"
# dirs contains possibly multiple directories for a program (eg. .GoLand2018.1 and .GoLand2017.3)
dirs = [f for f in os.listdir(HOME_DIR) if
os.path.isdir(os.path.join(HOME_DIR, f)) and f.startswith("." + app[0])]
# take the newest
dirs.sort(reverse=True)
if len(dirs) == 0:
continue
config_path = os.path.join(HOME_DIR, dirs[0], config_path)
if not os.path.exists(config_path):
continue
# extract the binary name and icon
binaries[app[0]] = find_exec(app[1])
# add all recently opened projects
projects.extend([[e[0], e[1], app[0]] for e in get_proj(config_path)])
projects.sort(key=lambda s: s[0], reverse=True)
# List all projects or the one corresponding to the query
if query.string:
projects = [p for p in projects if p[1].lower().find(query.string.lower()) != -1]
items = []
for p in projects:
last_update = p[0]
project_path = p[1]
project_dir = project_path.split("/")[-1]
product_name = p[2]
binary = binaries[product_name]
if not binary:
continue
executable = binary[0]
icon = binary[1]
items.append(Item(
id="-" + str(last_update),
icon=icon,
text=project_dir,
subtext=project_path,
completion=__trigger__ + project_dir,
actions=[
ProcAction("Open in %s" % product_name, [executable, project_path])
]
))
return items