AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1178169
Accepted
Sun Bear
Sun Bear
Asked: 2019-10-03 09:08:26 +0800 CST2019-10-03 09:08:26 +0800 CST 2019-10-03 09:08:26 +0800 CST

如何在单个命令行中为“update-alternatives --config gdm3.css”分配优先级编号?

  • 772

我可以在终端中运行这些命令:

$ sudo update-alternatives --install /usr/share/gnome-shell/theme/gdm3.css gdm3.css /usr/share/gnome-shell/theme/mytheme/mytheme.css 10
$ sudo update-alternatives --config gdm3.css
There are 2 choices for the alternative gdm3.css (providing /usr/share/gnome-shell/theme/gdm3.css).

  Selection    Path                                                    Priority   Status
------------------------------------------------------------
* 0            /usr/share/gnome-shell/theme/ubuntu.css                  10        auto mode
  1            /usr/share/gnome-shell/theme/mytheme/mytheme.css         10        manual mode
  2            /usr/share/gnome-shell/theme/ubuntu.css                  10        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/share/gnome-shell/theme/mytheme/mytheme.css to provide /usr/share/gnome-shell/theme/gdm3.css (gdm3.css) in manual mode

我将这两个cmds放在python3.6 subprocess.run()中。

我遇到的问题是第二个命令。如何在与 相同的 cmdline 中指定/usr/share/gnome-shell/theme/mytheme/mytheme.css为 的替代品?gdm3.csssudo update-alternatives --config gdm3.css

此外,此 cmd 需要输入数字才能选择mytheme.css。分配的编号取决于分配的优先级,这意味着分配的编号可以是任意的。如何克服分配号码的任意性?

gnome command-line themes gdm update-alternatives
  • 1 1 个回答
  • 1640 Views

1 个回答

  • Voted
  1. Best Answer
    Sun Bear
    2019-10-04T06:42:32+08:002019-10-04T06:42:32+08:00

    基于@PRATAP 评论,我为我的问题开发了这个python3.6 解决方案。它适用于我的 Ubuntu 18.04 系统。我希望它可以使有相同需求的其他人受益。

    #!/usr/bin/env python3.6
    # -*- coding: utf-8 -*-
    
    from subprocess import run, PIPE
    from pathlib import Path
    import mimetypes
    
    class CSSFileTypeError(Exception):
        pass
    
    class GDM3_alternatives:
        '''Class to query and configure gdm3.css.
    
        Argument:
          mytheme - path to my gnome-shell theme .css file.
    
        Attributes:
          mytheme - path to my gnome-shell theme .css file.
          query   - stdout from "update-alternatives --query gdm3.css" store in a list
          link    - gdm3.css path
          best    - gdm3.css alternative path selected by auto mode 
          value   - current gdm3.css alternative path 
          status  - whether gdm3.css is selected by manual or automatic mode 
          max     - maximum Priority value of all the installed gdm3.css alternatives
    
        Methods:
          exist()     - determines whether "mytheme" is installed as a gdm3.css alternative.
          configure() - configure "mytheme" file as a gdm3.css alternative.
        '''
        def __init__( self, mytheme=None ):       
            def _get( qvalue ):
                return [ line[ line.index('/') : ] for line in self.query if qvalue in line ][0]
            self.mytheme = mytheme
            self.query = run( [ 'update-alternatives', '--query', 'gdm3.css' ],
                              stdout=PIPE, encoding="utf-8" ).stdout.splitlines()
            self.link   = _get( 'Link:' )
            self.best   = _get( 'Best:' )
            self.value  = _get( 'Value:' )
            self.status = [ line[ line.index(':')+2 : ] for line in self.query if 'Status:' in line ][0]
            self.max = max( [ int( line[ line.index(':')+1 : ] ) for line in self.query if 'Priority:' in line ] )
            #print( f'self.query = {self.query}' )  #For debugging
            #print( f'self.link  = {self.link}' )   #For debugging
            #print( f'self.best  = {self.best}' )   #For debugging
            #print( f'self.value = {self.value}' )  #For debugging
            #print( f'self.status= {self.status}' ) #For debugging
            #print( f'self.max   = {self.max}' )    #For debugging
    
        def exist( self ):
            '''Method that determines whether "mytheme.css" is installed as a gdm3.css alternative. '''
            if self.mytheme == None:
                raise TypeError( '.css file was not defined.' ) 
            if not Path( self.mytheme ).exists():
                raise FileNotFoundError( f'{self.mytheme} does not exist.' )
            if 'css' not in mimetypes.guess_type( self.mytheme )[0] :
                raise CSSFileTypeError( f'{self.mytheme} is not a css file.' )
            return True in [ True for line in self.query if self.mytheme in line ]
    
        def configure( self ):
            '''Method to configure my theme ".css" file as a gdm3.css alternative.'''
            def _config():
                if 'auto' not in self.status:
                    run( [ 'update-alternatives', '--auto', 'gdm3.css' ] ) #Ensure auto mode is used
                run( [ 'update-alternatives', '--install', self.link, 'gdm3.css', self.mytheme, str(self.max + 1) ] )
                print( f'Configured {self.mytheme} as gdm3.css alternative.' )
    
            if not self.exist():
                _config()
            elif self.value in self.mytheme:
                print( f'{self.mytheme} is already gdm3.css alternative.' )
            else:
                run( [ 'update-alternatives', '--remove', 'gdm3.css', self.mytheme ] )
                self.__init__( self.mytheme )
                _config()
    
    if __name__ == '__main__':
        #mytheme = '/usr/share/gnome-shell/theme/mytheme/mytheme.css' 
        mytheme = '/usr/share/gnome-shell/theme/mytheme/mytheme.css' #Change this to your theme
        gdm3 = GDM3_alternatives( mytheme )
        gdm3.configure()
    
    ## This script needs to be executed by sudo. ## 
    

    如何使用这个脚本:

    1. 将其保存到具有.py扩展名的文件中,例如myscript.py.
    2. 更改行mytheme = '/usr/share/gnome-shell/theme/mytheme/mytheme.css'以显示您的主题的.css路径。
    3. 使用 sudo 权限在终端上运行脚本,即键入sudo python3.6 myscript.py。或者使用权限打开你的 python IDLEsudo并运行这个 python 脚本。

    update-alternatives我的 Ubuntu 18.04 系统上的版本是:

    $ update-alternatives --version
    Debian update-alternatives version 1.19.0.5.
    
    This is free software; see the GNU General Public License version 2 or
    later for copying conditions. There is NO warranty.
    
    • 1

相关问题

  • 是否有适用于 IMAP 邮件帐户的 Gnome 小程序?

  • 如何获取和安装更多主题、图标和指针?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

  • 如果顶部面板中缺少会话小程序,如何注销?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve