will never be translated
the path to addons is in sys.path so we can import things from org_fedora_hello_world
export only the spoke, no helper functions, classes or constants
# will never be translated
_ = lambda x: x
N_ = lambda x: x
# the path to addons is in sys.path so we can import things from org_fedora_hello_world
from org_fedora_hello_world.gui.categories.hello_world import HelloWorldCategory
from pyanaconda.ui.gui.spokes import NormalSpoke
# export only the spoke, no helper functions, classes or constants
all = ["HelloWorldSpoke"]
class HelloWorldSpoke(FirstbootSpokeMixIn, NormalSpoke):
"""
Class for the Hello world spoke. This spoke will be in the Hello world
category and thus on the Summary hub. It is a very simple example of a unit
for the Anaconda's graphical user interface. Since it is also inherited form
the FirstbootSpokeMixIn, it will also appear in the Initial Setup (successor
of the Firstboot tool).
:see: pyanaconda.ui.common.UIObject
:see: pyanaconda.ui.common.Spoke
:see: pyanaconda.ui.gui.GUIObject
:see: pyanaconda.ui.common.FirstbootSpokeMixIn
:see: pyanaconda.ui.gui.spokes.NormalSpoke
"""
# class attributes defined by API #
# list all top-level objects from the .glade file that should be exposed
# to the spoke or leave empty to extract everything
builderObjects = ["helloWorldSpokeWindow", "buttonImage"]
# the name of the main window widget
mainWidgetName = "helloWorldSpokeWindow"
# name of the .glade file in the same directory as this source
uiFile = "hello_world.glade"
# category this spoke belongs to
category = HelloWorldCategory
# spoke icon (will be displayed on the hub)
# preferred are the -symbolic icons as these are used in Anaconda's spokes
icon = "face-cool-symbolic"
# title of the spoke (will be displayed on the hub)
title = N_("_HELLO WORLD")
Copy to ClipboardCopied!Toggle word wrapToggle overflow
在 Hello World add-on 示例中,定义了如下两种方法:注意传给 __init__ 方法的编号和描述参数。
例 5.5. 定义 __init__ 和初始化方法:
def __init__(self, data, storage, payload):
"""
:see: pyanaconda.ui.common.Spoke.init
:param data: data object passed to every spoke to load/store data
from/to it
:type data: pykickstart.base.BaseHandler
:param storage: object storing storage-related information
(disks, partitioning, boot loader, etc.)
:type storage: blivet.Blivet
:param payload: object storing packaging-related information
:type payload: pyanaconda.packaging.Payload
"""
NormalSpoke.init(self, data, storage, payload)
self._hello_world_module = HELLO_WORLD.get_proxy()
def initialize(self):
"""
The initialize method that is called after the instance is created.
The difference between init and this method is that this may take
a long time and thus could be called in a separate thread.
:see: pyanaconda.ui.common.UIObject.initialize
"""
NormalSpoke.initialize(self)
self._entry = self.builder.get_object("textLines")
self._reverse = self.builder.get_object("reverseCheckButton")
def __init__(self, data, storage, payload):
"""
:see: pyanaconda.ui.common.Spoke.init
:param data: data object passed to every spoke to load/store data
from/to it
:type data: pykickstart.base.BaseHandler
:param storage: object storing storage-related information
(disks, partitioning, boot loader, etc.)
:type storage: blivet.Blivet
:param payload: object storing packaging-related information
:type payload: pyanaconda.packaging.Payload
"""
NormalSpoke.init(self, data, storage, payload)
self._hello_world_module = HELLO_WORLD.get_proxy()
def initialize(self):
"""
The initialize method that is called after the instance is created.
The difference between init and this method is that this may take
a long time and thus could be called in a separate thread.
:see: pyanaconda.ui.common.UIObject.initialize
"""
NormalSpoke.initialize(self)
self._entry = self.builder.get_object("textLines")
self._reverse = self.builder.get_object("reverseCheckButton")
Copy to ClipboardCopied!Toggle word wrapToggle overflow
def refresh(self):
"""
The refresh method that is called every time the spoke is displayed.
It should update the UI elements according to the contents of
internal data structures.
:see: pyanaconda.ui.common.UIObject.refresh
"""
lines = self._hello_world_module.Lines
self._entry.get_buffer().set_text("".join(lines))
reverse = self._hello_world_module.Reverse
self._reverse.set_active(reverse)
def apply(self):
"""
The apply method that is called when user leaves the spoke. It should
update the D-Bus service with values set in the GUI elements.
"""
buf = self._entry.get_buffer()
text = buf.get_text(buf.get_start_iter(),
buf.get_end_iter(),
True)
lines = text.splitlines(True)
self._hello_world_module.SetLines(lines)
self._hello_world_module.SetReverse(self._reverse.get_active())
def execute(self):
"""
The execute method that is called when the spoke is exited. It is
supposed to do all changes to the runtime environment according to
the values set in the GUI elements.
"""
# nothing to do here
pass
def refresh(self):
"""
The refresh method that is called every time the spoke is displayed.
It should update the UI elements according to the contents of
internal data structures.
:see: pyanaconda.ui.common.UIObject.refresh
"""
lines = self._hello_world_module.Lines
self._entry.get_buffer().set_text("".join(lines))
reverse = self._hello_world_module.Reverse
self._reverse.set_active(reverse)
def apply(self):
"""
The apply method that is called when user leaves the spoke. It should
update the D-Bus service with values set in the GUI elements.
"""
buf = self._entry.get_buffer()
text = buf.get_text(buf.get_start_iter(),
buf.get_end_iter(),
True)
lines = text.splitlines(True)
self._hello_world_module.SetLines(lines)
self._hello_world_module.SetReverse(self._reverse.get_active())
def execute(self):
"""
The execute method that is called when the spoke is exited. It is
supposed to do all changes to the runtime environment according to
the values set in the GUI elements.
"""
# nothing to do here
pass
Copy to ClipboardCopied!Toggle word wrapToggle overflow
以下是在 Hello World 附加组件中实现这些方法的示例,这需要在 HelloWorldData 类的文本属性中设置一个特定的值:
例 5.7. 定义 ready 、completed 和 mandatory 方法
@property
def ready(self):
"""
The ready property reports whether the spoke is ready, that is, can be visited
or not. The spoke is made (in)sensitive based on the returned value of the ready
property.
:rtype: bool
"""
# this spoke is always ready
return True
@property
def mandatory(self):
"""
The mandatory property that tells whether the spoke is mandatory to be
completed to continue in the installation process.
:rtype: bool
"""
# this is an optional spoke that is not mandatory to be completed
return False
@property
def ready(self):
"""
The ready property reports whether the spoke is ready, that is, can be visited
or not. The spoke is made (in)sensitive based on the returned value of the ready
property.
:rtype: bool
"""
# this spoke is always ready
return True
@property
def mandatory(self):
"""
The mandatory property that tells whether the spoke is mandatory to be
completed to continue in the installation process.
:rtype: bool
"""
# this is an optional spoke that is not mandatory to be completed
return False
Copy to ClipboardCopied!Toggle word wrapToggle overflow
@property
def status(self):
"""
The status property that is a brief string describing the state of the
spoke. It should describe whether all values are set and if possible
also the values themselves. The returned value will appear on the hub
below the spoke's title.
:rtype: str
"""
lines = self._hello_world_module.Lines
if not lines:
return _("No text added")
elif self._hello_world_module.Reverse:
return _("Text set with {} lines to reverse").format(len(lines))
else:
return _("Text set with {} lines").format(len(lines))
@property
def status(self):
"""
The status property that is a brief string describing the state of the
spoke. It should describe whether all values are set and if possible
also the values themselves. The returned value will appear on the hub
below the spoke's title.
:rtype: str
"""
lines = self._hello_world_module.Lines
if not lines:
return _("No text added")
elif self._hello_world_module.Reverse:
return _("Text set with {} lines to reverse").format(len(lines))
else:
return _("Text set with {} lines").format(len(lines))
Copy to ClipboardCopied!Toggle word wrapToggle overflow