zaz/doc/build/_sources/main/tools.rst.txt

512 lines
8.7 KiB
ReStructuredText

Tools
-----
Remember, always import library.
.. code-block:: python
import easymacro as app
Info from PC
^^^^^^^^^^^^
* Operate system
.. code-block:: python
app.msgbox(app.OS)
* Current user
.. code-block:: python
app.msgbox(app.USER)
* Name PC
.. code-block:: python
app.msgbox(app.PC)
* Name desktop, only GNU/Linux
.. code-block:: python
app.msgbox(app.DESKTOP)
* Language
.. code-block:: python
app.msgbox(app.LANG)
* Language with variant
.. code-block:: python
app.msgbox(app.LANGUAGE)
* Application name
.. code-block:: python
app.msgbox(app.NAME)
* Application version
.. code-block:: python
app.msgbox(app.VERSION)
* In Windows
.. code-block:: python
app.msgbox(app.IS_WIN)
* In Mac
.. code-block:: python
app.msgbox(app.IS_MAC)
Message Box
^^^^^^^^^^^
.. code-block:: python
app.msgbox(app.IS_WIN, 'My Macro')
Show warning
^^^^^^^^^^^^
.. code-block:: python
message = 'Caution, this action is dangerous'
title = 'My App'
app.warning(message, title)
Show error box
^^^^^^^^^^^^^^
.. code-block:: python
message = 'ERROR: Contact technical support'
title = 'My App'
app.errorbox(message, title)
Make question
^^^^^^^^^^^^^
.. code-block:: python
message = 'Is easy Python?'
title = 'My App'
result = app.question(message, title)
app.msgbox(result)
InputBox
^^^^^^^^
* Normal data
.. code-block:: python
message = 'Type your name'
default = ''
title = 'My App'
result = app.inputbox(message, default, title)
app.msgbox(result)
* Private data
.. code-block:: python
message = 'Type your password'
default = ''
title = 'My App'
echochar = "*"
result = app.inputbox(message, default, title, echochar)
app.msgbox(result)
Date and times
^^^^^^^^^^^^^^
* Get today
.. code-block:: python
app.msgbox(app.today())
* Get now
.. code-block:: python
app.msgbox(app.now())
* Get now only time
.. code-block:: python
app.msgbox(app.now(True))
* Get `epoch time`_
.. code-block:: python
app.msgbox(app.get_epoch())
* Simple measure time
.. code-block:: python
app.start()
app.sleep(5)
seconds = app.end(True)
app.msgbox(seconds)
Thread
^^^^^^
You can execute any macro in thread
* Normal execution
.. code-block:: python
def show_time(seconds):
app.sleep(seconds)
app.msgbox(app.NAME)
return
def main(args=None):
show_time(5)
app.msgbox('Finish...')
return
* Run in thread
.. code-block:: python
@app.run_in_thread
def show_time(seconds):
app.sleep(seconds)
app.msgbox(app.NAME)
return
def main(args=None):
show_time(5)
app.msgbox('Finish...')
return
Dictionary <-> properties
^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
args = {
'Hidden': True,
'Password': 'letmein',
}
properties = app.dict_to_property(args)
app.msgbox(properties)
data = app.data_to_dict(properties)
app.msgbox(data)
Tuples or lists to dictionary
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
tuple_of_tuples = (
('Hidden', True),
('Password', 'letmein'),
)
data = app.data_to_dict(tuple_of_tuples)
app.msgbox(data)
list_of_lists = [
['Hidden', True],
['Password', 'letmein'],
]
data = app.data_to_dict(list_of_lists)
app.msgbox(data)
Json
^^^^
.. code-block:: python
data = {
'Hidden': True,
'Password': 'letmein',
}
json = app.json_dumps(data)
app.msgbox(json)
data = app.json_loads(json)
app.msgbox(data)
Call Macros
^^^^^^^^^^^
You can any macro, for default call macros Python.
.. code-block:: python
def show_message():
app.msgbox(app.INFO_DEBUG)
return
def main(args=None):
args = {
'library': 'test',
'name': 'show_message',
}
app.call_macro(args)
return
Of course is better call directly if both macros are the same languaje, but, you can call macro in Basic too.
.. code-block:: vbnet
Sub show_message()
MsgBox "Basic from Python"
End Sub
Call from Python with.
.. code-block:: python
args = {
'language': 'Basic',
'library': 'Standard',
'module': 'Module1',
'name': 'show_message',
}
app.call_macro(args)
Execute macro in other thread
.. code-block:: python
app.call_macro(args, True)
Call external program
^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
app_name = 'gnome-calculator'
app.run(app_name)
app.msgbox('ok')
Call command line and capture output
.. code-block:: python
args = 'ls -lh ~'
result = app.run(args, True)
app.debug(result)
.. code-block:: bash
21/06/2021 22:27:22 - DEBUG - total 1.3M
drwxr-xr-x 5 mau mau 4.0K Jun 17 13:09 Desktop
drwxr-xr-x 6 mau mau 4.0K Jun 15 12:35 Documents
drwxr-xr-x 2 mau mau 4.0K Jun 21 20:26 Downloads
drwxr-xr-x 2 mau mau 4.0K Jun 21 16:18 Pictures
drwxr-xr-x 13 mau mau 4.0K Jun 21 15:34 Projects
drwxr-xr-x 2 mau mau 4.0K May 11 18:48 Templates
drwxr-xr-x 2 mau mau 4.0K Jun 20 23:27 Videos
Call command line and capture output line by line.
.. code-block:: python
args = 'ls -lh /home/mau'
for line in app.popen(args):
app.debug(line)
.. code-block:: bash
21/06/2021 22:34:42 - DEBUG - total 1.3M
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 5 mau mau 4.0K Jun 17 13:09 Desktop
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 6 mau mau 4.0K Jun 15 12:35 Documents
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 2 mau mau 4.0K Jun 21 20:26 Downloads
21/06/2021 22:34:42 - DEBUG - -rw-r----- 1 mau mau 1.3M Jun 14 11:53 out.png
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 2 mau mau 4.0K Jun 21 16:18 Pictures
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 13 mau mau 4.0K Jun 21 15:34 Projects
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 2 mau mau 4.0K May 11 18:48 Templates
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 2 mau mau 4.0K Jun 20 23:27 Videos
Timer
^^^^^
Execute any macro every seconds.
.. code-block:: python
TIMER_NAME = 'clock'
def show_time():
app.debug(app.now(True))
return
def start_clock():
seconds = 1
macro = {
'library': 'test',
'name': 'show_time',
}
app.start_timer(TIMER_NAME, seconds, macro)
return
def stop_clock():
app.stop_timer(TIMER_NAME)
return
def main(args=None):
start_clock()
return
Execute `stop_clock` for stop timer.
.. code-block:: bash
21/06/2021 22:43:17 - INFO - Timer started... show_time
21/06/2021 22:43:18 - DEBUG - 22:43:18.080315
21/06/2021 22:43:19 - DEBUG - 22:43:19.082211
...
21/06/2021 22:43:46 - DEBUG - 22:43:46.126446
21/06/2021 22:43:47 - DEBUG - 22:43:47.128487
21/06/2021 22:43:47 - INFO - Timer stopped... show_time
Get digest
^^^^^^^^^^
.. code-block:: python
data = 'LibreOffice with Python'
digest = app.sha256(data)
app.msgbox(digest)
digest = app.sha512(data)
app.msgbox(digest)
Save and get configurations
^^^^^^^^^^^^^^^^^^^^^^^^^^^
You can save any data.
.. code-block:: python
my_app = 'my_extension'
data = {
'path': '/home/mau/work',
'save_data': True,
}
app.set_config('config', data, my_app)
app.msgbox('Save config')
data = app.get_config('config', my_app)
app.msgbox(data)
Render string
^^^^^^^^^^^^^
.. code-block:: python
template = """Hello $name
I send you this $file_name
Best regards
"""
data = {'name': 'Ingrid Bergman', 'file_name': 'letter_love.odt'}
render = app.render(template, data)
app.msgbox(render)
Encrypt decrypt
^^^^^^^^^^^^^^^
You need install library `cryptography`_
.. code-block:: python
import easymacro as app
from conf import PASSWORD
def encrypt_decrypt():
data = 'My super secret data'
token = app.encrypt(data, PASSWORD)
app.msgbox(token)
data = app.decrypt(token, PASSWORD)
app.msgbox(data)
return
Simple url open
^^^^^^^^^^^^^^^
* Get text data
.. code-block:: python
url = 'https://api.ipify.org'
data = app.url_open(url)
app.msgbox(data)
* Get json data
.. code-block:: python
url = 'https://api.ipify.org?format=json'
data = app.url_open(url, get_json=True)
app.msgbox(data)
For more complex case, you can used `requests`_ or `httpx`_
.. _epoch time: https://en.wikipedia.org/wiki/Unix_time
.. _cryptography: https://github.com/pyca/cryptography
.. _requests: https://docs.python-requests.org
.. _httpx: https://www.python-httpx.org/