Example #1
0
	def _get_dbus_player_status(pl, bus_name, player_path, iface_prop,
	                            iface_player, player_name='player'):
		bus = dbus.SessionBus()
		try:
			player = bus.get_object(bus_name, player_path)
			iface = dbus.Interface(player, iface_prop)
			info = iface.Get(iface_player, 'Metadata')
			status = iface.Get(iface_player, 'PlaybackStatus')
		except dbus.exceptions.DBusException:
			return
		if not info:
			return
		album = info.get('xesam:album')
		title = info.get('xesam:title')
		artist = info.get('xesam:artist')
		state = _convert_state(status)
		if album:
			album = out_u(album)
		if title:
			title = out_u(title)
		if artist:
			artist = out_u(artist[0])
		return {
			'state': state,
			'album': album,
			'artist': artist,
			'title': title,
			'total': _convert_seconds(info.get('mpris:length') / 1e6),
		}
Example #2
0
def get_branch_name(directory, config_file, get_func, create_watcher):
	global branch_name_cache
	with branch_lock:
		# Check if the repo directory was moved/deleted
		fw = branch_watcher(create_watcher)
		is_watched = fw.is_watching(directory)
		try:
			changed = fw(directory)
		except OSError as e:
			if getattr(e, 'errno', None) != errno.ENOENT:
				raise
			changed = True
		if changed:
			branch_name_cache.pop(config_file, None)
			# Remove the watches for this repo
			if is_watched:
				fw.unwatch(directory)
				fw.unwatch(config_file)
		else:
			# Check if the config file has changed
			try:
				changed = fw(config_file)
			except OSError as e:
				if getattr(e, 'errno', None) != errno.ENOENT:
					raise
				# Config file does not exist (happens for mercurial)
				if config_file not in branch_name_cache:
					branch_name_cache[config_file] = out_u(get_func(directory, config_file))
		if changed:
			# Config file has changed or was not tracked
			branch_name_cache[config_file] = out_u(get_func(directory, config_file))
		return branch_name_cache[config_file]
Example #3
0
 def _get_dbus_player_status(pl, bus_name, player_path, iface_prop, iface_player, player_name="player"):
     bus = dbus.SessionBus()
     try:
         player = bus.get_object(bus_name, player_path)
         iface = dbus.Interface(player, iface_prop)
         info = iface.Get(iface_player, "Metadata")
         status = iface.Get(iface_player, "PlaybackStatus")
     except dbus.exceptions.DBusException:
         return
     if not info:
         return
     album = info.get("xesam:album")
     title = info.get("xesam:title")
     artist = info.get("xesam:artist")
     state = _convert_state(status)
     if album:
         album = out_u(album)
     if title:
         title = out_u(title)
     if artist:
         artist = out_u(artist[0])
     return {
         "state": state,
         "album": album,
         "artist": artist,
         "title": title,
         "total": _convert_seconds(info.get("mpris:length") / 1e6),
     }
Example #4
0
	def get_shortened_path(self, pl, segment_info, use_shortened_path=True, **kwargs):
		if use_shortened_path:
			try:
				return out_u(segment_info['shortened_path'])
			except KeyError:
				pass
		return super(ShellCwdSegment, self).get_shortened_path(pl, segment_info, **kwargs)
Example #5
0
	def get_shortened_path(self, pl, segment_info, shorten_home=True, **kwargs):
		try:
			path = out_u(segment_info['getcwd']())
		except OSError as e:
			if e.errno == 2:
				# user most probably deleted the directory
				# this happens when removing files from Mercurial repos for example
				pl.warn('Current directory not found')
				return '[not found]'
			else:
				raise
		if shorten_home:
			home = segment_info['home']
			if home:
				home = out_u(home)
				if path.startswith(home):
					path = '~' + path[len(home):]
		return path
Example #6
0
	def test_out_u(self):
		self.assertStringsIdentical('abc', plu.out_u('abc'))
		self.assertStringsIdentical('abc', plu.out_u(b'abc'))
		self.assertRaises(TypeError, plu.out_u, None)
Example #7
0
 def test_out_u(self):
     self.assertStringsIdentical('abc', plu.out_u('abc'))
     self.assertStringsIdentical('abc', plu.out_u(b'abc'))
     self.assertRaises(TypeError, plu.out_u, None)
Example #8
0
 def get_cwd(segment_info):
     try:
         return out_u(segment_info['getcwd']())
     except OSError as e:
         return '[not found]'
Example #9
0
    def __call__(self, pl, segment_info,
                 dir_shorten_len=None,
                 dir_limit_depth=None,
                 use_path_separator=False,
                 ellipsis='...',
                 default_icon='🖿',
                 home_icon='⌂',
                 root_icon='/',
                 ** kwargs):
        def get_icon(icon_path):
            icon_file = os.path.join(icon_path, '.powerline_icon')
            if os.path.isfile(icon_file):
                try:
                    with open(icon_file, 'r') as f:
                        icon = f.readline().strip('\n')
                        if not icon:
                            icon = default_icon
                        return (icon_path, icon)
                except PermissionError:
                    return (icon_path, default_icon)
            elif icon_path == home:
                return (icon_path, home_icon)
            else:
                return (os.path.dirname(icon_path), root_icon)

        try:
            path = out_u(segment_info['getcwd']())
        except OSError as e:
            path = ""
        home = segment_info['home']
        # Find icon file
        icon_path = path
        new_icon_path, icon = get_icon(icon_path)
        while new_icon_path != icon_path:
            icon_path = new_icon_path
            new_icon_path, icon = get_icon(icon_path)
        # Update path
        if icon_path[-1] != os.sep:
            icon_path = icon_path + os.sep
        path = path[len(icon_path):]
        # Process updated path
        cwd_split = path.split(os.sep)
        cwd_split_len = len(cwd_split)
        cwd = [i[0:dir_shorten_len] if dir_shorten_len and i else i for i in cwd_split[:-1]] + [cwd_split[-1]]
        if dir_limit_depth and cwd_split_len > dir_limit_depth + 1:
            del(cwd[0:-dir_limit_depth])
            if ellipsis is not None:
                cwd.insert(0, ellipsis)
        # Add icon
        ret = [{
            'contents': icon,
            'highlight_groups': ['cwd:icon']
        }]
        # Add parts for path
        if cwd[0]:
            draw_inner_divider = not use_path_separator
            for part in cwd:
                if not part:
                    continue
                if use_path_separator:
                    part += os.sep
                ret.append({
                    'contents': part,
                    'divider_highlight_group': 'cwd:divider',
                    'draw_inner_divider': draw_inner_divider,
                })
            ret[-1]['highlight_groups'] = ['cwd:current_folder', 'cwd']
            if use_path_separator:
                ret[-1]['contents'] = ret[-1]['contents'][:-1]
                if len(ret) > 1 and ret[0]['contents'][0] == os.sep:
                    ret[0]['contents'] = ret[0]['contents'][1:]
        return ret