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):
				data = json.loads(open(path, "r").read())
				return MenuData.from_json_data(data, TalkingActionParser())
		# Menu file not found
		return None
Ejemplo n.º 2
0
		def export_menu(tar, filename):
			try:
				menu = MenuData.from_json_data(json.loads(open(filename, "r").read()), ActionParser())
				tar.add(filename, arcname=os.path.split(filename)[-1], recursive=False)
			except Exception, e:
				# Menu that cannot be parsed shouldn't be exported
				log.error(e)
				return False
Ejemplo n.º 3
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):
             data = json.loads(open(path, "r").read())
             return MenuData.from_json_data(data, TalkingActionParser())
     # Menu file not found
     return None
Ejemplo n.º 4
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:
            data = json.loads(open(self.args.from_file, "r").read())
            self._menuid = self.args.from_file
            self.items = MenuData.from_json_data(data)
            #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

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

        # Create buttons that are displayed on screen
        self.items = self.items.generate(self)
        for item in self.items:
            item.widget = self.generate_widget(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
Ejemplo n.º 5
0
	def load(self, filename):
		""" Loads profile from file. Returns self """
		data = json.loads(open(filename, "r").read())
		# Version
		try:
			version = int(data["version"])
		except:
			version = 0
		
		# Buttons
		self.buttons = {}
		for x in SCButtons:
			self.buttons[x] = self.parser.from_json_data(data["buttons"], x.name)
		
		# Stick & gyro
		self.stick = self.parser.from_json_data(data, "stick")
		self.gyro = self.parser.from_json_data(data, "gyro")
		
		if "triggers" in data:
			# Old format
			# Triggers
			self.triggers = ({
				x : self.parser.from_json_data(data["triggers"], x) for x in Profile.TRIGGERS
			})
			
			# Pads
			self.pads = {
				Profile.LEFT	: self.parser.from_json_data(data, "left_pad"),
				Profile.RIGHT	: self.parser.from_json_data(data, "right_pad"),
			}
		else:
			# New format
			# Triggers
			self.triggers = {
				Profile.LEFT	: self.parser.from_json_data(data, "trigger_left"),
				Profile.RIGHT	: self.parser.from_json_data(data, "trigger_right"),
			}
		
			# Pads
			self.pads = {
				Profile.LEFT	: self.parser.from_json_data(data, "pad_left"),
				Profile.RIGHT	: self.parser.from_json_data(data, "pad_right"),
			}
		
		# Menus
		self.menus = {}
		if "menus" in data:
			for id in data["menus"]:
				for invalid_char in ".:/":
					if invalid_char in id:
						raise ValueError("Invalid character '%s' in menu id '%s'" % (invalid_char, id))
				self.menus[id] = MenuData.from_json_data(data["menus"][id], self.parser)
		
		# Conversion
		if version < Profile.VERSION:
			self._convert(version)
		
		return self
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:
			data = json.loads(open(self.args.from_file, "r").read())
			self._menuid = self.args.from_file
			self.items = MenuData.from_json_data(data)
			#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
		
		print "_control_with", self._control_with
		
		if self.args.use_cursor:
			self.enable_cursor()
		
		# Create buttons that are displayed on screen
		self.items = self.items.generate(self)
		for item in self.items:
			item.widget = self.generate_widget(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
Ejemplo n.º 7
0
 def export_menu(tar, filename):
     try:
         menu = MenuData.from_json_data(
             json.loads(open(filename, "r").read()), ActionParser())
         tar.add(filename,
                 arcname=os.path.split(filename)[-1],
                 recursive=False)
     except Exception, e:
         # Menu that cannot be parsed shouldn't be exported
         log.error(e)
         return False
Ejemplo n.º 8
0
	def parse_argumets(self, argv):
		if not OSDWindow.parse_argumets(self, argv):
			return False
		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:
				data = json.loads(open(self.args.from_file, "r").read())
				self._menuid = self.args.from_file
				self.items = MenuData.from_json_data(data)
			except:
				print >>sys.stderr, '%s: error: failed to loade 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
		
		if self.args.use_cursor:
			self.f.add(self.cursor)
			self.f.show_all()
			self._use_cursor = True
		
		# Create buttons that are displayed on screen
		for item in self.items:
			item.widget = Gtk.Button.new_with_label(item.label)
			item.widget.set_name("osd-menu-item")
			item.widget.set_relief(Gtk.ReliefStyle.NONE)
		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
		return True
Ejemplo n.º 9
0
    def load_fileobj(self, fileobj):
        """
		Loads profile from file-like object.
		Filename attribute is not set, what may cause some trouble if used in GUI.
		
		Returns self.
		"""
        data = json.loads(fileobj.read())
        # Version
        try:
            version = int(data["version"])
        except:
            version = 0

        # Settings - Description
        # (stored in key "_", so it's serialized on top of JSON file)
        if "_" not in data:
            self.description = ""
        elif type(data["_"]) == list:
            self.description = "\n".join(data["_"])
        else:
            self.description = data["_"]
        # Settings - Template
        self.is_template = bool(
            data["is_template"]) if "is_template" in data else False

        # Buttons
        self.buttons = {}
        for x in SCButtons:
            self.buttons[x] = self.parser.from_json_data(
                data["buttons"], x.name)
        # Pressing stick is interpreted as STICKPRESS button,
        # formely called just STICK
        if "STICK" in data["buttons"] and "STICKPRESS" not in data["buttons"]:
            self.buttons[SCButtons.STICKPRESS] = self.parser.from_json_data(
                data["buttons"], "STICK")

        # Stick & gyro
        self.stick = self.parser.from_json_data(data, "stick")
        self.gyro = self.parser.from_json_data(data, "gyro")

        if "triggers" in data:
            # Old format
            # Triggers
            self.triggers = ({
                x: self.parser.from_json_data(data["triggers"], x)
                for x in Profile.TRIGGERS
            })

            # Pads
            self.pads = {
                Profile.LEFT: self.parser.from_json_data(data, "left_pad"),
                Profile.RIGHT: self.parser.from_json_data(data, "right_pad"),
            }
        else:
            # New format
            # Triggers
            self.triggers = {
                Profile.LEFT: self.parser.from_json_data(data, "trigger_left"),
                Profile.RIGHT:
                self.parser.from_json_data(data, "trigger_right"),
            }

            # Pads
            self.pads = {
                Profile.LEFT: self.parser.from_json_data(data, "pad_left"),
                Profile.RIGHT: self.parser.from_json_data(data, "pad_right"),
            }

        # Menus
        self.menus = {}
        if "menus" in data:
            for id in data["menus"]:
                for invalid_char in ".:/":
                    if invalid_char in id:
                        raise ValueError(
                            "Invalid character '%s' in menu id '%s'" %
                            (invalid_char, id))
                self.menus[id] = MenuData.from_json_data(
                    data["menus"][id], self.parser)

        # Conversion
        if version < Profile.VERSION:
            self._convert(version)

        return self
Ejemplo n.º 10
0
	def load_fileobj(self, fileobj):
		"""
		Loads profile from file-like object.
		Filename attribute is not set, what may cause some trouble if used in GUI.
		
		Returns self.
		"""
		data = json.loads(fileobj.read())
		# Version
		try:
			version = float(data["version"])
		except:
			version = 0
		
		# Settings - Description
		# (stored in key "_", so it's serialized on top of JSON file)
		if "_" not in data:
			self.description = ""
		elif type(data["_"]) == list:
			self.description = "\n".join(data["_"])
		else:
			self.description = data["_"]
		# Settings - Template
		self.is_template = bool(data["is_template"]) if "is_template" in data else False
		
		# Buttons
		self.buttons = {}
		for x in SCButtons:
			self.buttons[x] = self.parser.from_json_data(data["buttons"], x.name)
		# Pressing stick is interpreted as STICKPRESS button,
		# formely called just STICK
		if "STICK" in data["buttons"] and "STICKPRESS" not in data["buttons"]:
			self.buttons[SCButtons.STICKPRESS] = self.parser.from_json_data(
					data["buttons"], "STICK")
		
		# Stick & gyro
		self.stick = self.parser.from_json_data(data, "stick")
		self.gyro = self.parser.from_json_data(data, "gyro")
		
		if "triggers" in data:
			# Old format
			# Triggers
			self.triggers = ({
				x : self.parser.from_json_data(data["triggers"], x) for x in Profile.TRIGGERS
			})
			
			# Pads
			self.pads = {
				Profile.LEFT	: self.parser.from_json_data(data, "left_pad"),
				Profile.RIGHT	: self.parser.from_json_data(data, "right_pad"),
				Profile.CPAD	: NoAction()
			}
		else:
			# New format
			# Triggers
			self.triggers = {
				Profile.LEFT	: self.parser.from_json_data(data, "trigger_left"),
				Profile.RIGHT	: self.parser.from_json_data(data, "trigger_right"),
			}
			
			# Pads
			self.pads = {
				Profile.LEFT	: self.parser.from_json_data(data, "pad_left"),
				Profile.RIGHT	: self.parser.from_json_data(data, "pad_right"),
				Profile.CPAD	: self.parser.from_json_data(data, "cpad"),
			}
		
		# Menus
		self.menus = {}
		if "menus" in data:
			for id in data["menus"]:
				for invalid_char in ".:/":
					if invalid_char in id:
						raise ValueError("Invalid character '%s' in menu id '%s'" % (invalid_char, id))
				self.menus[id] = MenuData.from_json_data(data["menus"][id], self.parser)
		
		# Conversion
		self.original_version = version		# TODO: This is temporary
		if version < Profile.VERSION:
			self._convert(version)
		
		return self
Ejemplo n.º 11
0
    def load(self, filename):
        """ Loads profile from file. Returns self """
        data = json.loads(open(filename, "r").read())
        # Version
        try:
            version = int(data["version"])
        except:
            version = 0

        # Buttons
        self.buttons = {}
        for x in SCButtons:
            self.buttons[x] = self.parser.from_json_data(
                data["buttons"], x.name)

        # Stick & gyro
        self.stick = self.parser.from_json_data(data, "stick")
        self.gyro = self.parser.from_json_data(data, "gyro")

        if "triggers" in data:
            # Old format
            # Triggers
            self.triggers = ({
                x: self.parser.from_json_data(data["triggers"], x)
                for x in Profile.TRIGGERS
            })

            # Pads
            self.pads = {
                Profile.LEFT: self.parser.from_json_data(data, "left_pad"),
                Profile.RIGHT: self.parser.from_json_data(data, "right_pad"),
            }
        else:
            # New format
            # Triggers
            self.triggers = {
                Profile.LEFT: self.parser.from_json_data(data, "trigger_left"),
                Profile.RIGHT:
                self.parser.from_json_data(data, "trigger_right"),
            }

            # Pads
            self.pads = {
                Profile.LEFT: self.parser.from_json_data(data, "pad_left"),
                Profile.RIGHT: self.parser.from_json_data(data, "pad_right"),
            }

        # Menus
        self.menus = {}
        if "menus" in data:
            for id in data["menus"]:
                for invalid_char in ".:/":
                    if invalid_char in id:
                        raise ValueError(
                            "Invalid character '%s' in menu id '%s'" %
                            (invalid_char, id))
                self.menus[id] = MenuData.from_json_data(
                    data["menus"][id], self.parser)

        # Conversion
        if version < Profile.VERSION:
            self._convert(version)

        return self