-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectables.py
180 lines (142 loc) · 4.72 KB
/
connectables.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# -*- coding: UTF-8 -*-
"""
Copyright 2010 Artem <spiritedflow@gmail.com>
This file is a part of the program kupfer, which is
released under GNU General Public License v3 (or any later version),
see the main program file, and COPYING for details.
"""
import os
import stat
import glob
import yaml
from kupfer.objects import Leaf, Action, Source
from kupfer.obj.helplib import PicklingHelperMixin, FilesystemWatchMixin
from kupfer import utils
__kupfer_name__ = _("Connectables")
__kupfer_sources__ = ("ConnectableSource", )
__description__ = _("Access to servers, routers, switches and other connectable devices. You need to configure this plugin manually. See README in example-config directory")
__version__ = "0.1"
__author__ = "Artem <spiritedflow@gmail.com>"
# ---------------------------------------------
# Paths
# ---------------------------------------------
config_home = os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
base_path = os.path.join(config_home, "kupfer", "connectables")
leafs_path = os.path.join(base_path, "leafs")
actions_path = os.path.join(base_path, "actions")
# ---------------------------------------------
# Action Cache
# ---------------------------------------------
class ActionDataCache:
"""
Cache for actions data
"""
def __init__(self, directory):
self.directory = directory
self.cache = {}
def get_data(self, name):
"""
Returns data for actions with name
At first it checks cache, then if there is no entry in the cache, it loads data from the file
"""
file = os.path.join (self.directory, name + '.yaml')
mtime = os.stat(file)[stat.ST_MTIME]
# check for cache entry,
# if it's ok return it
if name in self.cache and self.cache['name']['_mtime_'] == mtime:
return self.cache['name']['data']
# if there no cache entry or it is old,
# make new one
data = yaml.load(open(file))
self.cache['name'] = {
'data' : data,
'_mtime_' : mtime
}
# and return new data
return data
actions_cache = ActionDataCache(actions_path)
# ----------------------------------------
# Kupfer objects
# ----------------------------------------
class ConnectableSource (Source, PicklingHelperMixin, FilesystemWatchMixin):
"""
Source for manually configured connectables objects
The class contains all objects in your ~/.config/kupfer/connectables/leafs directory
"""
def __init__(self, name=_("Connectable Devices List")):
self.directory = leafs_path
Source.__init__(self, name)
self.unpickle_finish()
def unpickle_finish(self):
self.monitor_token = self.monitor_directories(self.directory)
def get_items(self):
files = glob.glob(os.path.join(self.directory, '*.yaml'))
for fullname in files:
(filepath, filename) = os.path.split(fullname)
(name, ext) = os.path.splitext(filename)
data = yaml.load (open(fullname))
yield ConnectableLeaf(data, name)
def get_description(self):
return _("All connectable objects")
def get_icon_name(self):
return "applications-internet"
def provides(self):
yield ConnectableLeaf
class ConnectableLeaf (Leaf):
"""
Connectable object
Reads and holds all actions you can do with this device
"""
def __init__ (self, data, name):
self.name = name
obj = {
'name' : name,
'descr' : _("Connectable object %(name)s"),
'icon_name': 'system'
}
obj.update(data)
Leaf.__init__(self, obj, name)
def get_actions(self):
for act_name in self.object['actions']:
yield ConnectableAction(act_name, self.object)
def get_description(self):
return substitute(self.object['descr'], self.object)
def get_icon_name(self):
return self.object['icon_name']
class ConnectableAction (Action):
"""
An action for connectable object
Do what described in ~/.config/kupfer/connectables/actions/<action>.yaml
"""
def __init__(self, name, object):
self.name = name
self.object = object
self.data = {
'type': 'unkwnon',
'name': 'Unknown',
'descr': 'Seems an action file ' + name + '.yaml is missed',
'icon_name': 'system-run',
'env': {},
'rank': 0,
}
self.data.update(actions_cache.get_data (name))
self.rank_adjust = self.data['rank']
Action.__init__(self, self.data['name'])
def activate (self, leaf):
run_command (self.data['type'], self.data, self.object)
def get_description(self):
return substitute(self.data['descr'], self.object)
def get_icon_name(self):
return self.data['icon_name']
def substitute(s, data):
"""Substitute all %(var)s with elements in data"""
return s % data
def run_command(typ, action_data, leaf_data):
"""Run command. Called from actions"""
if typ == 'command':
cmd = substitute(action_data['cmd'], leaf_data)
env = action_data['env']
utils.launch_commandline(cmd)
elif typ == 'uri':
uri = substitute(action_data['uri'], leaf_data)
utils.show_url(uri)