Fabricators
Fabricators are special Services designed to retrieve information from external shell commands or internal Python functions. They can also handle continuous streams of data, such as output from a persistent shell command or a Python generator function.
Fabricators provide several key options during initialization, allowing you to:
- Set an initial value: Specify the default value when the Fabricator starts.
- Define the polling source: Choose whether to poll data from a Python function/generator or a shell command.
- Configure the polling interval: Determine how frequently the Fabricator polls the data source (in milliseconds).
- Enable streaming: Handle continuous data streams, such as those from generators or streaming shell commands.
Examples
practical examples of how to use Fabricators in your configurations:
from fabric import Application, Fabricator
# lambda symbols# f: is the fabricator itself, v: is the new valuecounter_fabricator = Fabricator( interval=50, # ms default_value=0, poll_from=lambda f: f.get_value() + 1, on_changed=lambda f, v: ( (f.stop(), print("Counter Stopped")) if v == 43 else print(f"Counter Value: {v}") ),)
# example output:# Counter Value: 1# Counter Value: 2# Counter Value: 3# ...# Counter Value: 42# Counter Stopped
weather_fabricator = Fabricator( interval=1000 * 60, # 1min poll_from="curl https://wttr.in/?format=Weather+in+%l:+%t+(Feels+Like+%f),+%C+%c", on_changed=lambda f, v: print(v.strip()),)
# example output:# Weather in Homenland:, +15°C (Feels Like +15°C), Clear ☀️# ...
date_fabricator = Fabricator( interval=500, poll_from="date", on_changed=lambda f, v: print(f"Current Date: {v.strip()}"),)
# example output:# current date and time: Fri Nov 29 03:45:32 AM EET 2024# current date and time: Fri Nov 29 03:45:32 AM EET 2024# ...
# NOTE: this is just an example, the use of the Playerctl Python module would be a better ideaplayer_fabricator = Fabricator( stream=True, poll_from="playerctl --follow metadata --format '[{{status}}] {{title}} - {{artist}}'", on_changed=lambda f, v: print(v.strip()),)
# example output:# [Playing] Something - HomenArtsHouse# [Paused] Something - HomenArtsHouse# [Playing] The Stars - HomenArtsHouse# ...
# NOTE: this is just an example, the use of something like os would be betterdocuments_fabricator = Fabricator( interval=1000, # 1 second poll_from="du -sh /home/homan/Documents/", # NOTE: edit this on_changed=lambda f, v: print(f"Size of Documents: {v.split()[0]}"),)
# example output:# Size of Documents: 1G# Size of Documents: 1.1G# ...
app = Application()app.run()