Ejemplo n.º 1
0
 def _load_items_from_file(self, id):
     for p in (get_menus_path(), get_default_menus_path()):
         path = os.path.join(p, "%s.menu" % (id, ))
         if os.path.exists(path):
             return MenuData.from_file(path, TalkingActionParser())
     # Menu file not found
     return None
Ejemplo n.º 2
0
 def parse_menu(self):
     if self.args.from_profile:
         try:
             self._menuid = self.args.items[0]
             self.items = MenuData.from_profile(self.args.from_profile,
                                                self._menuid)
         except IOError:
             print >> sys.stderr, '%s: error: profile file not found' % (
                 sys.argv[0])
             return False
         except ValueError:
             print >> sys.stderr, '%s: error: menu not found' % (
                 sys.argv[0])
             return False
     elif self.args.from_file:
         try:
             self._menuid = self.args.from_file
             self.items = MenuData.from_file(self.args.from_file)
         except:
             print >> sys.stderr, '%s: error: failed to load menu file' % (
                 sys.argv[0])
             return False
     else:
         try:
             self.items = MenuData.from_args(self.args.items)
             self._menuid = None
         except ValueError:
             print >> sys.stderr, '%s: error: invalid number of arguments' % (
                 sys.argv[0])
             return False
     return True
Ejemplo n.º 3
0
	def parse_menu(self):
		if self.args.from_profile:
			try:
				self._menuid = self.args.items[0]
				self.items = MenuData.from_profile(self.args.from_profile, self._menuid)
			except IOError:
				print >>sys.stderr, '%s: error: profile file not found' % (sys.argv[0])
				return False
			except ValueError:
				print >>sys.stderr, '%s: error: menu not found' % (sys.argv[0])
				return False
		elif self.args.from_file:
			try:
				self._menuid = self.args.from_file
				self.items = MenuData.from_file(self.args.from_file)
			except:
				print >>sys.stderr, '%s: error: failed to load menu file' % (sys.argv[0])
				return False
		else:
			try:
				self.items = MenuData.from_args(self.args.items)
				self._menuid = None
			except ValueError:
				print >>sys.stderr, '%s: error: invalid number of arguments' % (sys.argv[0])
				return False
		return True
Ejemplo n.º 4
0
	def _add_refereced_menu(self, model, menu_id, used):
		"""
		As _add_refereced_profile, but reads and parses menu file.
		"""
		if "." in menu_id and menu_id not in used:
			# Dot in id means filename
			used.add(menu_id)
			filename = find_menu(menu_id)
			name = ".".join(menu_id.split(".")[0:-1])
			if name.startswith(".") and menu_is_default(menu_id):
				# Default and hidden, don't bother user with it
				return
			if filename:
				model.append((not menu_is_default(menu_id), _("Menu"), name,
						filename, True, self.TP_MENU))
				try:
					menu = MenuData.from_file(filename, ActionParser())
				except Exception, e:
					# Menu that cannot be parsed shouldn't be exported
					log.error(e)
					return
				for item in menu:
					if isinstance(item, Submenu):
						self._add_refereced_menu(model, os.path.split(item.filename)[-1], used)
					if hasattr(item, "action"):
						self._parse_action(model, item.action, used)
			else:
				model.append((False, _("Menu"), _("%s (not found)") % (name,),
						"", False, self.TP_MENU))
Ejemplo n.º 5
0
    def _add_refereced_menu(self, model, menu_id, used):
        """
		As _add_refereced_profile, but reads and parses menu file.
		"""
        if "." in menu_id and menu_id not in used:
            # Dot in id means filename
            used.add(menu_id)
            filename = find_menu(menu_id)
            name = ".".join(menu_id.split(".")[0:-1])
            if name.startswith(".") and menu_is_default(menu_id):
                # Default and hidden, don't bother user with it
                return
            if filename:
                model.append((not menu_is_default(menu_id), _("Menu"), name,
                              filename, True, self.TP_MENU))
                try:
                    menu = MenuData.from_file(filename, ActionParser())
                except Exception, e:
                    # Menu that cannot be parsed shouldn't be exported
                    log.error(e)
                    return
                for item in menu:
                    if isinstance(item, Submenu):
                        self._add_refereced_menu(
                            model,
                            os.path.split(item.filename)[-1], used)
                    if hasattr(item, "action"):
                        self._parse_action(model, item.action, used)
            else:
                model.append((False, _("Menu"), _("%s (not found)") % (name, ),
                              "", False, self.TP_MENU))
Ejemplo n.º 6
0
    def parse_argumets(self, argv):
        if not OSDWindow.parse_argumets(self, argv):
            return False
        if not self.config:
            self.config = Config()
        if self.args.from_profile:
            try:
                self._menuid = self.args.items[0]
                self.items = MenuData.from_profile(self.args.from_profile,
                                                   self._menuid)
            except IOError:
                print >> sys.stderr, '%s: error: profile file not found' % (
                    sys.argv[0])
                return False
            except ValueError:
                print >> sys.stderr, '%s: error: menu not found' % (
                    sys.argv[0])
                return False
        elif self.args.from_file:
            #try:
            self._menuid = self.args.from_file
            self.items = MenuData.from_file(self.args.from_file)
            #except:
            #	print >>sys.stderr, '%s: error: failed to load menu file' % (sys.argv[0])
            #	return False
        else:
            try:
                self.items = MenuData.from_args(self.args.items)
                self._menuid = None
            except ValueError:
                print >> sys.stderr, '%s: error: invalid number of arguments' % (
                    sys.argv[0])
                return False

        # Parse simpler arguments
        self._control_with = self.args.control_with
        self._confirm_with = self.args.confirm_with
        self._cancel_with = self.args.cancel_with
        self._max_size = self.args.max_size

        if self.args.use_cursor:
            self.enable_cursor()

        # Create buttons that are displayed on screen
        items = self.items.generate(self)
        self.items = []
        for item in items:
            item.widget = self.generate_widget(item)
            if item.widget:
                self.items.append(item)
        self.pack_items(self.parent, self.items)
        if len(self.items) == 0:
            print >> sys.stderr, '%s: error: no items in menu' % (sys.argv[0])
            return False

        if self.args.print_items:
            max_id_len = max(*[len(x.id) for x in self.items])
            row_format = "{:>%s}:\t{}" % (max_id_len, )
            for item in self.items:
                print row_format.format(item.id, item.label)
        return True