app_list.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2011, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 #
33 # Revision $Id: topics.py 11753 2010-10-25 06:23:19Z kwc $
34 
35 # author: kwc
36 
37 """
38 Implements applist part of app_manager, which handles listing of
39 currently installed applications.
40 """
41 
42 import os
43 import rospy
44 import sys
45 import yaml
46 
47 import rospkg
48 
49 from .app import load_AppDefinition_by_name
50 from .msg import App, ClientApp, KeyValue, Icon
51 from .exceptions import AppException, InvalidAppException, NotFoundException
52 
54  """
55  Default directory where applist configuration is stored.
56  """
57  return "/etc/robot/apps"
58 
60  l = []
61  for k, v in d.items():
62  l.append(KeyValue(k, str(v)))
63  return l
64 
65 def read_Icon_file(filename):
66  icon = Icon()
67  if filename == None or filename == "":
68  return icon
69  basename, extension = os.path.splitext(filename)
70  if extension.lower() == ".jpg" or extension.lower() == ".jpeg":
71  icon.format = "jpeg"
72  elif extension.lower() == ".png":
73  icon.format = "png"
74  else:
75  icon.format = ""
76  return icon
77  icon.data = open(filename, "rb").read()
78  return icon
79 
80 def AppDefinition_to_App(app_definition):
81  a = App(name=app_definition.name, display_name=app_definition.display_name, icon=read_Icon_file(app_definition.icon))
82  a.client_apps = []
83  for c in app_definition.clients:
84  a.client_apps.append(ClientApp(c.client_type,
85  dict_to_KeyValue(c.manager_data),
86  dict_to_KeyValue(c.app_data)))
87  return a
88 
89 class InstalledFile(object):
90  """
91  Models data stored in a .installed file. These files are used to
92  track installation of apps.
93  """
94 
95  def __init__(self, filename):
96  self.filename = filename
97  # list of App
98  self.available_apps = []
99 
100  self._file_mtime = None
101  self.update()
102 
103  def _load(self):
104  available_apps = []
105  rospack = rospkg.RosPack()
106  with open(self.filename) as f:
107  installed_data = yaml.load(f)
108  for reqd in ['apps']:
109  if not reqd in installed_data:
110  raise InvalidAppException("installed file [%s] is missing required key [%s]"%(self.filename, reqd))
111  for app in installed_data['apps']:
112  for areqd in ['app']:
113  if not areqd in app:
114  raise InvalidAppException("installed file [%s] app definition is missing required key [%s]"%(self.filename, areqd))
115  try:
116  available_apps.append(
117  load_AppDefinition_by_name(app['app'], rospack=rospack))
118  except NotFoundException as e:
119  rospy.logerr(e)
120  continue
121  except Exception as e:
122  raise e
123 
124  self.available_apps = available_apps
125 
126  def update(self):
127  """
128  Update app list
129  """
130  s = os.stat(self.filename)
131  if s.st_mtime != self._file_mtime:
132  self._load()
133  self._file_mtime = s.st_mtime
134 
135  def get_available_apps(self, platform=None):
136  if platform is not None:
137  available_apps = filter(
138  lambda app: app.platform in [platform, 'all'],
139  self.available_apps)
140  return available_apps
141  else:
142  return self.available_apps
143 
144  def __eq__(self, other):
145  return self.filename == other.filename
146 
147  def __neq__(self, other):
148  return not self.__eq__(other)
149 
150 
151 class AppList(object):
152  def __init__(self, applist_directories, platform=None):
153  self.applist_directories = applist_directories
154  self.installed_files = {}
156  self.app_list = []
157  self.platform = platform
158 
160  self.need_update = True
161 
163  installed_files = []
164  for d in self.applist_directories:
165  for f in os.listdir(d):
166  if f.endswith(".installed"):
167  full_path = os.path.abspath(os.path.join(d, f))
168  installed_files.append(full_path)
169  return installed_files
170 
171  def _load(self, files):
172  if files:
173  installed_files = files
174  else:
175  installed_files = self.installed_files.keys()
176  invalid_installed_files = []
177  app_list = []
178  for f in installed_files:
179  try:
180  if f in self.installed_files:
181  # update InstalledFile object
182  installed_file = self.installed_files[f]
183  else:
184  # new installed file
185  installed_file = InstalledFile(f)
186  self.installed_files[f] = installed_file
187  installed_file.update()
188  app_list.extend(installed_file.get_available_apps(platform=self.platform))
189  rospy.loginfo("%d apps found in %s" % (len(installed_file.available_apps), installed_file.filename))
190  except AppException as e:
191  rospy.logerr("ERROR: %s" % (str(e)))
192  invalid_installed_files.append((f, e))
193  except Exception as e:
194  rospy.logerr("ERROR: %s" % (str(e)))
195  invalid_installed_files.append((f, e))
196 
197  self.app_list = app_list
198  self.invalid_installed_files = invalid_installed_files
199 
200  def get_app_list(self):
201  if not self.app_list:
202  self.update()
203  return [AppDefinition_to_App(ad) for ad in self.app_list]
204 
205  def get_app(self, name):
206  for app in self.app_list:
207  if app.name == name:
208  return app
209  return None
210 
211  def add_directory(self, directory):
212  if not os.path.exists(directory):
213  raise IOError("applist directory %s does not exist." % directory)
214  if directory in self.applist_directory:
215  raise RuntimeError("applist directory %s already exists" % directory)
216  self.applist_directories.append(directory)
217  self.need_update = True
218 
219  def remove_directory(self, directory):
220  if directory not in self.applist_directory:
221  raise RuntimeError("applist directory %s does not in list" % directory)
222  self.applist_directory.remove(directory)
223  self.need_update = True
224 
225  def update(self):
226  """
227  Update app list
228  """
229  files = None
230  if self.need_update:
231  files = self._find_installed_files()
232  self.need_update = False
233  self._load(files)
def __init__(self, applist_directories, platform=None)
Definition: app_list.py:152
def add_directory(self, directory)
Definition: app_list.py:211
def _load(self, files)
Definition: app_list.py:171
def AppDefinition_to_App(app_definition)
Definition: app_list.py:80
def load_AppDefinition_by_name(appname, rospack=None)
Definition: app.py:392
def remove_directory(self, directory)
Definition: app_list.py:219
def read_Icon_file(filename)
Definition: app_list.py:65
def get_default_applist_directory()
Definition: app_list.py:53
def get_app(self, name)
Definition: app_list.py:205
def dict_to_KeyValue(d)
Definition: app_list.py:59
def __init__(self, filename)
Definition: app_list.py:95
def get_available_apps(self, platform=None)
Definition: app_list.py:135


app_manager
Author(s): Jeremy Leibs, Ken Conley, Yuki Furuta
autogenerated on Thu Oct 13 2022 02:59:17