Queer European MD passionate about IT
Browse Source

Support module execution

(cherry picked from commit bb9e8b5cc89abd470f3e19b538e7498779ff08b4)
Davte 4 years ago
parent
commit
80adecfd64
6 changed files with 144 additions and 48 deletions
  1. 31 19
      README.md
  2. 1 1
      ciclopibot/__init__.py
  3. 53 0
      ciclopibot/__main__.py
  4. 52 22
      ciclopibot/bot.py
  5. 6 6
      install.sh
  6. 1 0
      setup.py

+ 31 - 19
README.md

@@ -11,25 +11,37 @@ Send [`/start`](https://t.me/ciclopibot?start=00help) [@CicloPiBot](https://t.me
 * Ask for `/ciclopi` information
 
 ### "Server" side
-* Clone this repository
-```bash
-git clone ssh://git@gogs.davte.it:8445/Davte/ciclopibot.git
-# git clone https://gogs.davte.it/Davte/ciclopibot.git
-# git clone git@github.com:Davte/ciclopibot.git
-# git clone https://github.com/Davte/ciclopibot.git
-```
-* Run `install.sh`: it will help you perform the following operations.
-  * Put a Telegram bot token in gitignored `data/passwords.py` module.
-    * To get a token, ask [@BotFather](https://t.me/botfather).
-    * The bot whose token you use will act as [@CicloPiBot](https://t.me/ciclopibot) as long as you run the script.
-  * Create a python3.5+ virtual environment and install requirements.
-  * Specify `python_virtual_environment` and `python_script` variables in `my_config.sh`
-* Run `run_me.sh`
-```bash
-bash run_me.sh;
-```
-* You may edit the file and test your code with your bot.
-* Should you be satisfied of your edits enough, you may fork this repository and open a pull request.
+You may choose between method 1 (`pip`) and method 2 (`git`).
+1. Using `pip`
+    * Install ciclopibot
+    ```bash
+    pip install ciclopibot
+   ``` 
+   * Run ciclopibot as module
+   ```bash
+   python -m ciclopibot -h  # Get help 
+   python -m ciclopibot <your_token_here>
+   ```
+1. Using `git`
+    * Clone this repository
+    ```bash
+    git clone ssh://git@gogs.davte.it:8445/Davte/ciclopibot.git
+    # git clone https://gogs.davte.it/Davte/ciclopibot.git
+    # git clone git@github.com:Davte/ciclopibot.git
+    # git clone https://github.com/Davte/ciclopibot.git
+    ```
+    * Run `install.sh`: it will help you perform the following operations.
+      * Put a Telegram bot token in gitignored `data/passwords.py` module.
+        * To get a token, ask [@BotFather](https://t.me/botfather).
+        * The bot whose token you use will act as [@CicloPiBot](https://t.me/ciclopibot) as long as you run the script.
+      * Create a python3.5+ virtual environment and install requirements.
+      * Specify `python_virtual_environment` and `python_script` variables in `my_config.sh`
+    * Run `run_me.sh`
+    ```bash
+    bash run_me.sh;
+    ```
+    * You may edit the file and test your code with your bot.
+    * Should you be satisfied of your edits enough, you may fork this repository and open a pull request.
 
 ## Credits
 * [Davte](https://www.davte.it) is the creator and the main author of this repository.

+ 1 - 1
ciclopibot/__init__.py

@@ -3,7 +3,7 @@
 __author__ = "Davide Testa"
 __email__ = "davide@davte.it"
 __license__ = "GNU General Public License v3.0"
-__version__ = "1.1.17"
+__version__ = "1.2.0"
 __maintainer__ = "Davide Testa"
 __contact__ = "t.me/davte"
 

+ 53 - 0
ciclopibot/__main__.py

@@ -0,0 +1,53 @@
+"""Run a local copy of CicloPiBot."""
+
+# Standard library modules
+import argparse
+
+# Project modules
+from . import bot
+
+
+def main():
+    # Parse command-line arguments
+    cli_parser = argparse.ArgumentParser(description=__doc__,
+                                         allow_abbrev=False)
+    cli_parser.add_argument('--bot_token', '--token', '--t', type=str,
+                            default=None,
+                            required=False,
+                            help='telegram bot token')
+    cli_parser.add_argument('--path', type=str,
+                            default=None,
+                            required=False,
+                            help='path where data should be stored')
+    cli_parser.add_argument('--log_file_name', '--log', type=str,
+                            default=None,
+                            required=False,
+                            help='file to store full log')
+    cli_parser.add_argument('--errors_file_name', '--err', type=str,
+                            default=None,
+                            required=False,
+                            help='file to store error log')
+    cli_parser.add_argument('--local_host', '--host', type=str,
+                            default=None,
+                            required=False,
+                            help='local host address for linked web-app')
+    cli_parser.add_argument('--port', type=int,
+                            default=None,
+                            required=False,
+                            help='local host port for linked web-app')
+    cli_parser.add_argument('--hostname', type=str,
+                            default=None,
+                            required=False,
+                            help='host name for webhooks')
+    cli_parser.add_argument('--certificate', type=str,
+                            default=None,
+                            required=False,
+                            help='certificate for webhooks')
+    cli_arguments = vars(cli_parser.parse_args())
+    bot.main(
+        **cli_arguments
+    )
+
+
+if __name__ == '__main__':
+    main()

+ 52 - 22
ciclopibot/bot.py

@@ -10,34 +10,64 @@ import davtelepot
 
 # Project modules
 from . import ciclopi, messages
-from .data.passwords import bot_token
 from .messages import (
     default_help_messages, language_messages, supported_languages
 )
 
 
-def main():
-    path = os.path.dirname(
-        os.path.abspath(
-            __file__
+def main(bot_token: str = None,
+         path: str = None,
+         log_file_name: str = None,
+         errors_file_name: str = None,
+         local_host: str = None,
+         port: int = None,
+         hostname: str = None,
+         certificate: str = None):
+    if bot_token is None:
+        try:
+            from .data.passwords import bot_token
+        except ImportError:
+            logging.error(
+                "Missing bot token. Create a bot with t.me/BotFather and "
+                "provide its token here to run a local copy of CicloPiBot."
+            )
+            return
+    if path is None:
+        path = os.path.dirname(
+            os.path.abspath(
+                __file__
+            )
         )
-    )
-    try:
-        from .data.config import log_file_name
-    except ImportError:
-        log_file_name = 'CicloPi.info.log'
-    try:
-        from .data.config import errors_file_name
-    except ImportError:
-        errors_file_name = 'CicloPi.errors.log'
-    try:
-        from .data.config import local_host, port
-    except ImportError:
-        local_host, port = '127.0.0.1', 3000
-    try:
-        from .data.config import hostname, certificate
-    except ImportError:
-        hostname, certificate = '', None
+    if log_file_name is None:
+        try:
+            from .data.config import log_file_name
+        except ImportError:
+            log_file_name = 'CicloPi.info.log'
+    if errors_file_name is None:
+        try:
+            from .data.config import errors_file_name
+        except ImportError:
+            errors_file_name = 'CicloPi.errors.log'
+    if local_host is None:
+        try:
+            from .data.config import local_host
+        except ImportError:
+            local_host = 'localhost'
+    if port is None:
+        try:
+            from .data.config import port
+        except ImportError:
+            port = 3000
+    if hostname is None:
+        try:
+            from .data.config import hostname
+        except ImportError:
+            hostname = ''
+    if certificate is None:
+        try:
+            from .data.config import certificate
+        except ImportError:
+            certificate = None
     log_file = f"{path}/data/{log_file_name}"
     errors_file = f"{path}/data/{errors_file_name}"
 

+ 6 - 6
install.sh

@@ -1,15 +1,15 @@
 #!/bin/bash
 
 # Get current directory
-this_script_directory=$(cd `dirname $0` && pwd);
-cd "$this_script_directory";
+this_script_directory=$(cd "$(dirname "$0")" && pwd);
+cd "$this_script_directory" || exit 1;
 configuration_file="$this_script_directory/my_config.sh";
 passwords_file="$this_script_directory/ciclopibot/data/passwords.py";
 
 # Create intermediate path for passwords_file, if it does not exist
-mkdir -p "$this_script_directory/ciclopibot/data/";
+mkdir -r "$this_script_directory/ciclopibot/data/";
 
-read -p "Enter a name for your virtual environment
+read -r "Enter a name for your virtual environment
            " venv_name;
 python3 -m venv "$venv_name";
 "$venv_name"/bin/pip install -r "$this_script_directory/requirements.txt";
@@ -18,8 +18,8 @@ python3 -m venv "$venv_name";
 # Other systems may require a different path.
 echo "python_virtual_environment=\"$(pwd)/$venv_name/bin\";" >> "$configuration_file";
 echo "python_script=\"$this_script_directory/ciclopibot/bot.py\";" >> "$configuration_file";
-read -p "Enter a valid Telegram bot token           " bot_token;
-echo "bot_token = \"$bot_token\"" >> $passwords_file;
+read -r "Enter a valid Telegram bot token           " bot_token;
+echo "bot_token = \"$bot_token\"" >> "$passwords_file";
 
 # Run bot
 bash run_me.sh;

+ 1 - 0
setup.py

@@ -57,6 +57,7 @@ setuptools.setup(
     install_requires=[
         'davtelepot',
     ],
+    python_requires='>=3.5',
     classifiers=[
         "Development Status :: 5 - Production/Stable",
         "Environment :: Console",