From 3a664eee208cd6c07cd70c2f152f80780be835f6 Mon Sep 17 00:00:00 2001
From: Moul <moul@moul.re>
Date: Thu, 6 May 2021 23:16:51 +0200
Subject: [PATCH] [mod] #160: examples: Allow to pass files path in f() prompt
 as arg

Reduce usage message
Add missing exits
---
 examples/create_public_key.py                 |  1 +
 examples/load_binary_encrypted_message.py     | 20 +++++++++----------
 examples/load_binary_signed_message.py        | 20 +++++++++----------
 .../load_cleartext_ascii_armor_message.py     |  1 +
 examples/load_credentials_file.py             | 14 +++++++------
 .../load_encrypted_ascii_armor_message.py     |  1 +
 examples/load_local_blockchain.py             |  2 ++
 examples/load_scuttlebutt_file.py             | 14 +++++++------
 examples/save_and_load_private_key_file.py    | 15 +++++++++++---
 .../save_and_load_private_key_file_ewif.py    |  5 ++++-
 .../save_and_load_private_key_file_pubsec.py  |  4 +++-
 .../save_and_load_private_key_file_wif.py     |  9 +++++++--
 examples/save_binary_encrypted_message.py     |  2 ++
 examples/save_binary_signed_message.py        |  1 +
 .../save_cleartext_ascii_armor_message.py     |  2 ++
 .../save_encrypted_ascii_armor_message.py     |  2 ++
 16 files changed, 72 insertions(+), 41 deletions(-)

diff --git a/examples/create_public_key.py b/examples/create_public_key.py
index 7ed15a28..cd4345b7 100644
--- a/examples/create_public_key.py
+++ b/examples/create_public_key.py
@@ -20,6 +20,7 @@ from duniterpy.key import SigningKey
 
 ################################################
 
+
 def create_public_key():
     # prompt hidden user entry
     salt = getpass.getpass("Enter your passphrase (salt): ")
diff --git a/examples/load_binary_encrypted_message.py b/examples/load_binary_encrypted_message.py
index 1853cafa..e1072da0 100644
--- a/examples/load_binary_encrypted_message.py
+++ b/examples/load_binary_encrypted_message.py
@@ -21,17 +21,14 @@ import sys
 from duniterpy.key import SigningKey
 
 
-def load_binary_encrypted_message():
-    if len(sys.argv) < 2:
-        print(
-            """
-        Usage:
-            python decrypt_message.py ENCRYPTED_MESSAGE_FILEPATH
-        """
-        )
-
-    # capture encrypted message filepath argument
-    signed_message_path = sys.argv[1]
+def load_binary_encrypted_message(signed_message_path=None):
+    if not signed_message_path:
+        if len(sys.argv) < 2:
+            print("Usage: python decrypt_message.py ENCRYPTED_MESSAGE_FILEPATH")
+            sys.exit(1)
+
+        # capture encrypted message filepath argument
+        signed_message_path = sys.argv[1]
 
     # prompt hidden user entry
     salt = getpass.getpass("Enter your passphrase (salt): ")
@@ -55,5 +52,6 @@ def load_binary_encrypted_message():
 
     print(message)
 
+
 if __name__ == "__main__":
     load_binary_encrypted_message()
diff --git a/examples/load_binary_signed_message.py b/examples/load_binary_signed_message.py
index 857390fa..e8fbf907 100644
--- a/examples/load_binary_signed_message.py
+++ b/examples/load_binary_signed_message.py
@@ -19,17 +19,15 @@ import sys
 
 from duniterpy.key import VerifyingKey
 
-def load_binary_signed_message():
-    if len(sys.argv) < 2:
-        print(
-            """
-        Usage:
-            python verify_signed_message.py SIGNED_MESSAGE_FILEPATH
-        """
-        )
-
-    # capture signed message filepath argument
-    signed_message_path = sys.argv[1]
+
+def load_binary_signed_message(signed_message_path=None):
+    if not signed_message_path:
+        if len(sys.argv) < 2:
+            print("Usage: python verify_signed_message.py SIGNED_MESSAGE_FILEPATH")
+            sys.exit(1)
+
+        # capture signed message filepath argument
+        signed_message_path = sys.argv[1]
 
     # ask public key of the signer
     pubkeyBase58 = input("Enter public key of the message issuer: ")
diff --git a/examples/load_cleartext_ascii_armor_message.py b/examples/load_cleartext_ascii_armor_message.py
index cd87bc03..4952c340 100644
--- a/examples/load_cleartext_ascii_armor_message.py
+++ b/examples/load_cleartext_ascii_armor_message.py
@@ -23,6 +23,7 @@ CLEARTEXT_AA_MESSAGE_PATH = "/tmp/duniter_cleartext_aa_message.txt"
 
 ################################################
 
+
 def load_cleartext_ascii_armor_message():
     # Ask public key of the issuer
     pubkeyBase58 = input("Enter public key of the message issuer: ")
diff --git a/examples/load_credentials_file.py b/examples/load_credentials_file.py
index 70e6a49f..311d316f 100644
--- a/examples/load_credentials_file.py
+++ b/examples/load_credentials_file.py
@@ -19,13 +19,15 @@ import sys
 
 from duniterpy.key import SigningKey
 
-def load_credentials_file():
-    if len(sys.argv) < 2:
-        print("Usage: python load_credentials_file.py FILEPATH")
-        sys.exit(1)
 
-    # capture filepath argument
-    credentials_filepath = sys.argv[1]
+def load_credentials_file(credentials_filepath=None):
+    if not credentials_filepath:
+        if len(sys.argv) < 2:
+            print("Usage: python load_credentials_file.py FILEPATH")
+            sys.exit(1)
+
+        # capture filepath argument
+        credentials_filepath = sys.argv[1]
 
     # create SigningKey instance from file
     signing_key_instance = SigningKey.from_credentials_file(
diff --git a/examples/load_encrypted_ascii_armor_message.py b/examples/load_encrypted_ascii_armor_message.py
index 44234bda..7c3600b4 100644
--- a/examples/load_encrypted_ascii_armor_message.py
+++ b/examples/load_encrypted_ascii_armor_message.py
@@ -25,6 +25,7 @@ ENCRYPTED_AA_MESSAGE_PATH = "/tmp/duniter_aa_encrypted_message.txt"
 
 ################################################
 
+
 def load_encrypted_ascii_armor_message():
     # Ask public key of the recipient
     pubkeyBase58 = input("Enter public key of the message issuer: ")
diff --git a/examples/load_local_blockchain.py b/examples/load_local_blockchain.py
index 517b6416..212f72e8 100644
--- a/examples/load_local_blockchain.py
+++ b/examples/load_local_blockchain.py
@@ -21,6 +21,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from duniterpy.helpers.blockchain import load
 
+
 def load_local_blockchain():
     bc = load()  # gets blockchain iterator
     b = next(bc)  # gets block
@@ -30,5 +31,6 @@ def load_local_blockchain():
     print(f"third block number is: {next(bc).number}")  # should return 2
     # (and so on)
 
+
 if __name__ == "__main__":
     load_local_blockchain()
diff --git a/examples/load_scuttlebutt_file.py b/examples/load_scuttlebutt_file.py
index 48cff9a8..cf11c006 100644
--- a/examples/load_scuttlebutt_file.py
+++ b/examples/load_scuttlebutt_file.py
@@ -19,13 +19,15 @@ import sys
 
 from duniterpy.key import SigningKey
 
-def load_scuttlebutt_file():
-    if len(sys.argv) < 2:
-        print("Usage: python load_scuttlebutt_file.py FILEPATH")
-        sys.exit(1)
 
-    # capture filepath argument
-    scuttlebutt_filepath = sys.argv[1]
+def load_scuttlebutt_file(scuttlebutt_filepath=None):
+    if not scuttlebutt_filepath:
+        if len(sys.argv) < 2:
+            print("Usage: python load_scuttlebutt_file.py FILEPATH")
+            sys.exit(1)
+
+        # capture filepath argument
+        scuttlebutt_filepath = sys.argv[1]
 
     # create SigningKey instance from file
     signing_key_instance = SigningKey.from_ssb_file(
diff --git a/examples/save_and_load_private_key_file.py b/examples/save_and_load_private_key_file.py
index d135f0e5..8757e841 100644
--- a/examples/save_and_load_private_key_file.py
+++ b/examples/save_and_load_private_key_file.py
@@ -38,6 +38,7 @@ PRIVATE_KEYS_FILE_PATH = os.path.join(home_path, ".duniter_account_private_keys.
 
 ################################################
 
+
 def save_and_load_private_key_file():
     # prompt hidden user entry
     salt = getpass.getpass("Enter your passphrase (salt): ")
@@ -60,13 +61,21 @@ def save_and_load_private_key_file():
     signer.save_private_key(PRIVATE_KEYS_FILE_PATH)
 
     # document saved
-    print("Private keys for public key %s saved in %s" % (pubkey, PRIVATE_KEYS_FILE_PATH))
+    print(
+        "Private keys for public key %s saved in %s" % (pubkey, PRIVATE_KEYS_FILE_PATH)
+    )
 
     # load private keys from file
-    loaded_signer = SigningKey.from_private_key(PRIVATE_KEYS_FILE_PATH)  # type: SigningKey
+    loaded_signer = SigningKey.from_private_key(
+        PRIVATE_KEYS_FILE_PATH
+    )  # type: SigningKey
 
     # check public key from file
-    print("Public key %s loaded from file %s" % (loaded_signer.pubkey, PRIVATE_KEYS_FILE_PATH))
+    print(
+        "Public key %s loaded from file %s"
+        % (loaded_signer.pubkey, PRIVATE_KEYS_FILE_PATH)
+    )
+
 
 if __name__ == "__main__":
     save_and_load_private_key_file()
diff --git a/examples/save_and_load_private_key_file_ewif.py b/examples/save_and_load_private_key_file_ewif.py
index 009c9172..c564b968 100644
--- a/examples/save_and_load_private_key_file_ewif.py
+++ b/examples/save_and_load_private_key_file_ewif.py
@@ -38,6 +38,7 @@ PRIVATE_KEY_FILE_PATH = os.path.join(home_path, ".duniter_account_ewif_v1.dunite
 
 ################################################
 
+
 def save_and_load_private_key_file_ewif():
     # prompt hidden user entry
     salt = getpass.getpass("Enter your passphrase (salt): ")
@@ -64,7 +65,8 @@ def save_and_load_private_key_file_ewif():
 
     # document saved
     print(
-        "Private key for public key %s saved in %s" % (signer.pubkey, PRIVATE_KEY_FILE_PATH)
+        "Private key for public key %s saved in %s"
+        % (signer.pubkey, PRIVATE_KEY_FILE_PATH)
     )
 
     try:
@@ -83,5 +85,6 @@ def save_and_load_private_key_file_ewif():
         print(error)
         sys.exit(1)
 
+
 if __name__ == "__main__":
     save_and_load_private_key_file_ewif()
diff --git a/examples/save_and_load_private_key_file_pubsec.py b/examples/save_and_load_private_key_file_pubsec.py
index a42dd2ec..6b402a59 100644
--- a/examples/save_and_load_private_key_file_pubsec.py
+++ b/examples/save_and_load_private_key_file_pubsec.py
@@ -62,7 +62,8 @@ def save_and_load_private_key_file_pubsec():
 
     # document saved
     print(
-        "Private key for public key %s saved in %s" % (signer.pubkey, PRIVATE_KEY_FILE_PATH)
+        "Private key for public key %s saved in %s"
+        % (signer.pubkey, PRIVATE_KEY_FILE_PATH)
     )
 
     try:
@@ -79,5 +80,6 @@ def save_and_load_private_key_file_pubsec():
         print(error)
         sys.exit(1)
 
+
 if __name__ == "__main__":
     save_and_load_private_key_file_pubsec()
diff --git a/examples/save_and_load_private_key_file_wif.py b/examples/save_and_load_private_key_file_wif.py
index aa9e479b..cfa2f051 100644
--- a/examples/save_and_load_private_key_file_wif.py
+++ b/examples/save_and_load_private_key_file_wif.py
@@ -38,6 +38,7 @@ PRIVATE_KEY_FILE_PATH = os.path.join(home_path, ".duniter_account_wif_v1.duniter
 
 ################################################
 
+
 def save_and_load_private_key_file_wif():
     # prompt hidden user entry
     salt = getpass.getpass("Enter your passphrase (salt): ")
@@ -61,12 +62,15 @@ def save_and_load_private_key_file_wif():
 
     # document saved
     print(
-        "Private key for public key %s saved in %s" % (signer.pubkey, PRIVATE_KEY_FILE_PATH)
+        "Private key for public key %s saved in %s"
+        % (signer.pubkey, PRIVATE_KEY_FILE_PATH)
     )
 
     try:
         # load private keys from file
-        loaded_signer = SigningKey.from_wif_file(PRIVATE_KEY_FILE_PATH)  # type: SigningKey
+        loaded_signer = SigningKey.from_wif_file(
+            PRIVATE_KEY_FILE_PATH
+        )  # type: SigningKey
 
         # check public key from file
         print(
@@ -78,5 +82,6 @@ def save_and_load_private_key_file_wif():
         print(error)
         sys.exit(1)
 
+
 if __name__ == "__main__":
     save_and_load_private_key_file_wif()
diff --git a/examples/save_binary_encrypted_message.py b/examples/save_binary_encrypted_message.py
index 38c88d15..50188e5b 100644
--- a/examples/save_binary_encrypted_message.py
+++ b/examples/save_binary_encrypted_message.py
@@ -23,6 +23,7 @@ ENCRYPTED_MESSAGE_FILENAME = "/tmp/duniter_encrypted_message.bin"
 
 ################################################
 
+
 def save_binary_encrypted_message():
     # Ask public key of the recipient
     pubkeyBase58 = input("Enter public key of the message recipient: ")
@@ -40,5 +41,6 @@ def save_binary_encrypted_message():
 
     print("Encrypted message saved in file ./{0}".format(ENCRYPTED_MESSAGE_FILENAME))
 
+
 if __name__ == "__main__":
     save_binary_encrypted_message()
diff --git a/examples/save_binary_signed_message.py b/examples/save_binary_signed_message.py
index 0b6aef70..517c310d 100644
--- a/examples/save_binary_signed_message.py
+++ b/examples/save_binary_signed_message.py
@@ -27,6 +27,7 @@ SIGNED_MESSAGE_FILENAME = "/tmp/duniter_signed_message.bin"
 
 ################################################
 
+
 def save_binary_signed_message():
     # prompt hidden user entry
     salt = getpass.getpass("Enter your passphrase (salt): ")
diff --git a/examples/save_cleartext_ascii_armor_message.py b/examples/save_cleartext_ascii_armor_message.py
index 6d26dfcb..b402edd4 100644
--- a/examples/save_cleartext_ascii_armor_message.py
+++ b/examples/save_cleartext_ascii_armor_message.py
@@ -28,6 +28,7 @@ CLEARTEXT_AA_MESSAGE_PATH = "/tmp/duniter_cleartext_aa_message.txt"
 
 ################################################
 
+
 def save_cleartext_ascii_armor_message():
     # prompt hidden user entry
     salt = getpass.getpass("Enter your passphrase (salt): ")
@@ -60,5 +61,6 @@ def save_cleartext_ascii_armor_message():
         )
     )
 
+
 if __name__ == "__main__":
     save_cleartext_ascii_armor_message()
diff --git a/examples/save_encrypted_ascii_armor_message.py b/examples/save_encrypted_ascii_armor_message.py
index c45cd44e..b99be229 100644
--- a/examples/save_encrypted_ascii_armor_message.py
+++ b/examples/save_encrypted_ascii_armor_message.py
@@ -26,6 +26,7 @@ ENCRYPTED_AA_MESSAGE_PATH = "/tmp/duniter_aa_encrypted_message.txt"
 
 ################################################
 
+
 def save_encrypted_ascii_armor_message():
     # Ask public key of the recipient
     pubkeyBase58 = input("Enter public key of the message recipient: ")
@@ -64,5 +65,6 @@ def save_encrypted_ascii_armor_message():
         )
     )
 
+
 if __name__ == "__main__":
     save_encrypted_ascii_armor_message()
-- 
GitLab