upsies.jobs.dialog
Get information from the user
Classes
- class upsies.jobs.dialog.ChoiceJob(*, home_directory=None, cache_directory=None, cache_id='', ignore_cache=False, no_output_is_ok=False, hidden=False, autostart=True, guaranteed=False, precondition=None, prejobs=(), callbacks={}, **kwargs)[source]
Bases:
JobBaseAsk the user to choose from a set of values
This job adds the following signals to
signal:dialog_updatedEmitted when the
options,focusedorautodetectedproperties are set or whenoptionsis modified. Registered callbacks get no arguments.autodetectingEmitted when
autodetectis called. Registered callbacks get the job instance as a positional argument.autodetectedEmitted when
autodetectreturns. Registered callbacks get the job instance as a positional argument.
- property name
Internal name (e.g. for the cache file name)
- property label
User-facing name
- property options
Sequence of options the user can make
An option is a
tuplewith 2 items. The first item is a human-readablestrand the second item is any object that is available aschoicewhen the job is finished.Options may also be passed as a flat iterable of
str, in which case both items in the tuple are identical.When setting this property, focus is preserved if the value of the focused option exists in the new options. Otherwise, the first option is focused.
- property multichoice
Whether multiple options can be selected
- get_index(thing)[source]
Return index in
options- Parameters:
thing –
Identifier of a option in
optionsThis can be:
- None
Return None
- an index (
int) Return thing, but limited to the minimum/maximum valid index.
- one of the 2-tuples in
options Return the index of thing in
options.- an item of one of the 2-tuples in
options Return the index of the first 2-tuple in
optionsthat contains thing.- a
regular expression Return the index of the first 2-tuple in
optionsthat contains something that matches thing. Non-string values are converted tostrfor matching against the regular expression.
- Raises:
ValueError – if thing is not found in
options
- get_option(thing)[source]
Return item in
options- Parameters:
thing – See
get_index()
If thing is None, return the currently focused option, which is indicated by
focused_index.
- property focused
Currently focused option (2-tuple)
This property can be set to anything that is a valid value for
get_index().
- property autodetected
Autodetected option (2-tuple) or sequence of autodetected options (if multichoice) or None
This property can be set to anything that is a valid value for
get_index(). If multichoice was True, it must be set to a sequence of validget_index()arguments.If this property is set to None, all options are marked as “not autodetected”.
- property choice
User-chosen value if job is finished, None otherwise
If multichoice was True, this is a sequence of chosen values.
While
outputcontains the user-readable string (first item of the chosen 2-tuple inoptions), this is the object attached to it (second item).
- make_choice(thing)[source]
Make a choice and
finalize()this job- Parameters:
thing – See
get_option()
After this method is called, this job
is_finished,outputcontains the human-readable label of the choice andchoiceis the machine-readable value of the choice.
- set_label(identifier, new_label)[source]
Assign new label to option
- Parameters:
identifier – Option (2-tuple of (<current label>, <value>)) or the current label or value of an option
new_label – New label for the option defined by identifier
Do nothing if identifier doesn’t match any option.
- initialize(*, name, label, options, question=None, focused=None, autodetected=None, autodetect=None, autofinish=False, multichoice=None, validate=None)[source]
Set internal state
- Parameters:
name – Name for internal use
label – Name for user-facing use
options – Iterable of options the user can pick from
question – Any text that is displayed alongside the options
focused – See
focusedautodetected – See
autodetectedautodetect –
Callable that sets
autodetectedwhen job is startedautodetect gets the job instance (
self) as a positional argument.autofinish – Whether to call
make_choice()if autodetect returns anything that is not Nonemultichoice – Whether multiple or no items from options can be chosen
validate –
callablethat gets the sequence of chosen options and raisesValueErrorto inform the user about their mistake
- Raises:
ValueError – if options is shorter than 2 or focused is invalid
- async run()[source]
Do the work
This method is called by
start(). Its coroutine is passed toadd_task().Any keyword arguments passed to
initialize()are available viakwargs.The job
is_finishedwhen alladded tasksare done or cancelled. (See alsofinalize()andfinalization().)This method may call
add_task()if more tasks are required.
- class upsies.jobs.dialog.TextFieldJob(*, home_directory=None, cache_directory=None, cache_id='', ignore_cache=False, no_output_is_ok=False, hidden=False, autostart=True, guaranteed=False, precondition=None, prejobs=(), callbacks={}, **kwargs)[source]
Bases:
JobBaseAsk the user for text input
This job adds the following signals to
signal:textEmitted when
textwas changed without user input. Registered callbacks get the new text as a positional argument.is_loadingEmitted when
is_loadingwas changed. Registered callbacks get the newis_loadingvalue as a positional argument.read_onlyEmitted when
read_onlywas changed. Registered callbacks get the newread_onlyvalue as a positional argument.obscuredEmitted when
obscuredwas changed. Registered callbacks get the newobscuredvalue as a positional argument.
- property name
Internal name (e.g. for the cache file name)
- property label
User-facing name
- property text
Current text
- property obscured
Whether the text is unreadable, e.g. when entering passwords
This is currently not fully implemented.
- property read_only
Whether the user should be able change the text
This is just a boolean flag that is not enforced by the job so that autodetection will still work. The user interface must track its status via the
read_onlysignal and enforce it.
- initialize(*, name, label, text='', default=None, finish_on_success=False, warn_exceptions=(), error_exceptions=(), validator=None, normalizer=None, obscured=False, read_only=False)[source]
Set internal state
- Parameters:
name – Name for internal use
label – Name for user-facing use
text – Initial text or callable (synchronous or asynchronous) which will be called when the job is
started. The return value is used as the initial text.default – Text to use if text is None, returns None or raises warn_exceptions
finish_on_success –
Whether to call
finalize()after settingtextto textNote
finalize()is not called iftextis set to default.warn_exceptions – Sequence of exception classes that are caught if raised by text and passed on to
warn()error_exceptions – Sequence of exception classes that are caught if raised by text and passed on to
error()validator (callable or None) – Callable that gets text before job is finished. If ValueError is raised, it is displayed as a warning instead of finishing the job.
normalizer (callable or None) – Callable that gets text and returns the new text. It is called before validator. It should not raise any exceptions.
obscured (bool) – Whether
obscuredis set to True initially (currently fully implemented)read_only (bool) – Whether
read_onlyis set to True initially
- async run()[source]
Do the work
This method is called by
start(). Its coroutine is passed toadd_task().Any keyword arguments passed to
initialize()are available viakwargs.The job
is_finishedwhen alladded tasksare done or cancelled. (See alsofinalize()andfinalization().)This method may call
add_task()if more tasks are required.
- set_text(text, *, default=None, finish_on_success=False, warn_exceptions=(), error_exceptions=())[source]
Change
textvalue- Parameters:
text –
New text value
If this is callable, it must return the new
textor None to use defaultdefault – Text to use if text is None, returns None or raises warn_exceptions
finish_on_success –
Whether to call
finish()after settingtextto textNote
finish()is not called whentextis set to default.warn_exceptions – Sequence of exception classes that may be raised by coro and are passed on to :~.JobBase.warn
error_exceptions – Sequence of exception classes that may be raised by coro and are passed on to :~.JobBase.error
- async fetch_text(coro, *, default=None, finish_on_success=False, warn_exceptions=(), error_exceptions=())[source]
Get
textfrom coroutine- Parameters:
coro – Coroutine that returns the new
textor None to use defaultdefault – Text to use if text is None, returns None or raises warn_exceptions
finish_on_success –
Whether to call
finish()after settingtextto coro return valueNote
finish()is not called whentextis set to default.warn_exceptions – Sequence of exception classes that may be raised by coro and are passed on to :~.JobBase.warn
error_exceptions – Sequence of exception classes that may be raised by coro and are passed on to :~.JobBase.error
- add_output(output)[source]
Validate output before actually adding it
Pass output to validator (see
initialize()). IfValueErroris raised, pass it towarn()and do not finalize. Otherwise, pass output toadd_output()andfinalize()this job.