diff --git a/CHANGELOG.md b/CHANGELOG.md index bc749b2..e31ab99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog =================================== +0.1.3 (2022-09-11) +------------------- + +- Improvement in output string. +- Documentation. + 0.1.2 (2022-09-11) ------------------- diff --git a/README.md b/README.md index 7fc9cc0..0b6c327 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Installation Install it using ``pip`` ```shell -pip install progress-progress_updater +pip install progress-updater ``` Basic usage @@ -23,10 +23,9 @@ Basic usage ```python from progress_updater import ProgressUpdater -# create an progress_updater object updater = ProgressUpdater(task_name="My Task") -with updater(block_name="First part") as updater: +with updater(block_name="First part"): # doing things updater.notify("doing first block...") # doing more things @@ -36,16 +35,40 @@ with updater(block_name="Second part"): updater.notify("doing second block...") # doing more things -updater.raise_latest_exception() +updater.raise_latest_exception() # if exists +``` + +The output is: +```shell +- Task: My task + +- Entering First part + doing first block... + Time spent: 0h0m + Successfully completed + +- Entering Second part + doing second block... + Time spent: 0h0m + Successfully completed ``` Backends ---------- -There are three backends available to save our logs. +If you want to save the output in a Database you will need to define +a backend. There are three backends available to save our logs. -1. Mongo. See [documentation](). -2. Redis. See [documentation](). -3. SQL. See [documentation](). +1. Mongo. +2. Redis. +3. SQL. + +```python +from progress_updater.backends import MongoLog +from uuid import UUID + +log = MongoLog.get(uuid=UUID("")) +assert log.status == "SUCCESS" +``` Settings @@ -67,10 +90,11 @@ settings = MongoSettings( ) with ProgressUpdater(task_name="My Task", settings=settings) as updater: - ... + pass ``` -2. Environment variables. +2. Environment variables. + The `PU__` prefix indicates that it belongs to `ProgressUpdater`. ```shell export PU__SQL_DSN=postgresql+psycopg2://user:pass@postgres:5432/db diff --git a/docs/backends.rst b/docs/backends.rst index ebd4684..54a994c 100644 --- a/docs/backends.rst +++ b/docs/backends.rst @@ -7,7 +7,7 @@ Backends :mod:`Settings` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: updater.backends +.. automodule:: progress_updater.backends :members: Settings :exclude-members: Config, backend @@ -15,7 +15,7 @@ Backends :mod:`Mongo` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: updater.backends.mongo +.. automodule:: progress_updater.backends.mongo :members: MongoLog, MongoSettings :exclude-members: mongo_collection, backend @@ -23,7 +23,7 @@ Backends :mod:`Redis` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: updater.backends.redis +.. automodule:: progress_updater.backends.redis :members: RedisLog, RedisSettings :exclude-members: redis_connection, backend @@ -31,7 +31,7 @@ Backends :mod:`SQL` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: updater.backends.sql +.. automodule:: progress_updater.backends.sql :members: SQLLog, SQLSettings :exclude-members: sql_session, backend @@ -39,6 +39,6 @@ Backends :mod:`Base Log` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: updater.backends.log +.. automodule:: progress_updater.backends.log :members: Log, Logs :exclude-members: diff --git a/docs/index.rst b/docs/index.rst index 9b4a6ef..e3874d5 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -8,17 +8,25 @@ Welcome to progress-updater' documentation! ============================================= +Writing the progress of a task to a backend! + +Installation +------------- + +Install it using `pip`:: + + pip install progress-updater + Basic usage ------------- -Make sure you had install the package by doing ``pip install progress-updater`` and then:: +Make sure you have the `progress-updater` installed:: - from updater import ProgressUpdater + from progress_updater import ProgressUpdater - # create an updater object - updater = ProgressUpdater(task_name="My Task") + updater = ProgressUpdater(task_name="My Task", write_on_backend=False, verbose=True) - with updater(block_name="First part") as updater: + with updater(block_name="First part"): # doing things updater.notify("doing first block...") # doing more things @@ -28,17 +36,39 @@ Make sure you had install the package by doing ``pip install progress-updater`` updater.notify("doing second block...") # doing more things - updater.raise_latest_exception() + updater.raise_latest_exception() # if exists + +The output is:: + - Task: My task + + - Entering First part + doing first block... + Time spent: 0h0m + Successfully completed + + - Entering Second part + doing second block... + Time spent: 0h0m + Successfully completed Backends ---------- -There are three backends available to save our logs. +If you want to save the output in a Database you will need to define +a backend. There are three backends available to save our logs. 1. Mongo. 2. Redis. 3. SQL. +In you console:: + + from progress_updater.backends import MongoLog + from uuid import UUID + + log = MongoLog.get(uuid=UUID("")) + assert log.status == "SUCCESS" + Settings ---------- @@ -46,25 +76,25 @@ Settings There are some possible ways to pass settings to the updater. This is the priority. -1. Passing settings as parameters when creating a `ProgressUpdater` object. -Example :: +1. Passing settings as parameters when creating a `ProgressUpdater` object:: - from updater import ProgressUpdater - from updater.backends.mongo import MongoSettings + from progress_updater import ProgressUpdater + from progress_updater.backends.mongo import MongoSettings settings = MongoSettings( mongo_connection="mongodb://user:pass@mongo:27017", mongo_db="db", mongo_collection="logs", ) + with ProgressUpdater(task_name="My Task", settings=settings) as updater: pass -2. Environment variables. The `PU__` prefix indicates that it belongs to `ProgressUpdater`. -Example:: +2. Environment variables:: +The `PU__` prefix indicates that it belongs to `ProgressUpdater`:: - export PU__SQL_DSN='postgresql+psycopg2://user:pass@postgres:5432/db' - export PU__SQL_TABLE='logs' + export PU__SQL_DSN=postgresql+psycopg2://user:pass@postgres:5432/db + export PU__SQL_TABLE=logs .. toctree:: diff --git a/docs/updater.rst b/docs/updater.rst index 1ae3fa9..62367df 100644 --- a/docs/updater.rst +++ b/docs/updater.rst @@ -7,7 +7,7 @@ Progress Updater :mod:`Progress Updater` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: updater.updater +.. automodule:: progress_updater.updater :members: ProgressUpdater :exclude-members: @@ -15,6 +15,6 @@ Progress Updater :mod:`Decorator` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -.. automodule:: updater.utils +.. automodule:: progress_updater.utils :members: progress_updater :exclude-members: diff --git a/progress_updater/updater.py b/progress_updater/updater.py index dca8acb..f4fec4b 100644 --- a/progress_updater/updater.py +++ b/progress_updater/updater.py @@ -145,9 +145,9 @@ def __init__( self.notify(f"- Task: {self.task_name}") def __enter__(self, block_name: str = None) -> "ProgressUpdater": - self.block_name = block_name or "..." + self.block_name = self.__dict__.get("block_name") or "..." self.start_t = datetime.datetime.utcnow() - self.notify(f"- Entering {self.block_name}") + self.notify(f"\n- Entering {self.block_name}") return self def __exit__(self, exc_type, exc_val, exc_tb) -> bool: diff --git a/pyproject.toml b/pyproject.toml index 34d114a..0431983 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "progress_updater" -version = "0.1.2" +version = "0.1.3" description = "Progress Updater" readme = "README.md" license = "LICENSE" diff --git a/tests/test_updater/test_updater.py b/tests/test_updater/test_updater.py index a5b24cf..fb01338 100644 --- a/tests/test_updater/test_updater.py +++ b/tests/test_updater/test_updater.py @@ -15,20 +15,20 @@ def test_progress_updater_skip_backend(mongo_backend, capsys): write_on_backend=False, ) - with updater(block_name="First Block") as updater: + with updater(block_name="First Block"): updater.notify("Doing first block...") - with updater(block_name="Second Block") as updater: + with updater(block_name="Second Block"): updater.notify("Doing second block...") updater.raise_latest_exception() assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\nDoing first " - "block...\n\tTime spent: 0h0m\n\tSuccessfully " - "completed\n- Entering ...\nDoing second block" - "...\n\tTime spent: 0h0m\n\tSuccessfully completed\n" + == "- Task: My task\n\n- Entering First Block\nDoing first block..." + "\n\tTime spent: 0h0m\n\tSuccessfully completed\n\n- " + "Entering Second Block\nDoing second block...\n\tTime spent: " + "0h0m\n\tSuccessfully completed\n" ) assert not mongo_backend.count_documents(filter={}) @@ -68,10 +68,10 @@ def test_progress_updater_skip_backend_raise_exception(mongo_backend, capsys): assert isinstance(e, ZeroDivisionError) assert ( - capsys.readouterr().out == "- Task: My task\n- Entering ..." - "\nDoing first block...\n\tTime spent: " - "0h0m\n\tFailed\n\tError message: : division by zero\n" + capsys.readouterr().out + == "- Task: My task\n\n- Entering First Block\nDoing first " + "block...\n\tTime spent: 0h0m\n\tFailed\n\tError message: " + ": division by zero\n" ) assert not mongo_backend.count_documents(filter={}) @@ -100,10 +100,10 @@ def test_progress_updater_raise_exception(mongo_backend, capsys): assert isinstance(e, ZeroDivisionError) assert ( - capsys.readouterr().out == "- Task: My task\n- Entering ..." - "\nDoing first block...\n\tTime spent: " - "0h0m\n\tFailed\n\tError message: : division by zero\n" + capsys.readouterr().out + == "- Task: My task\n\n- Entering First Block\nDoing first " + "block...\n\tTime spent: 0h0m\n\tFailed\n\tError message: " + ": division by zero\n" ) assert mongo_backend.count_documents(filter={}) @@ -129,9 +129,9 @@ def test_progress_updater_passing_params_redis(redis_backend, capsys): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\nDoing first block..." - "\n\tTime spent: 0h0m\n\tSuccessfully completed\n- " - "Entering ...\nDoing second block...\n\tTime spent: " + == "- Task: My task\n\n- Entering First Block\nDoing first block..." + "\n\tTime spent: 0h0m\n\tSuccessfully completed\n\n- " + "Entering Second Block\nDoing second block...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert redis_backend.keys() @@ -158,9 +158,9 @@ def test_progress_updater_passing_params_mongo(mongo_backend, capsys): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\nDoing first block..." - "\n\tTime spent: 0h0m\n\tSuccessfully completed\n- " - "Entering ...\nDoing second block...\n\tTime spent: " + == "- Task: My task\n\n- Entering First Block\nDoing first block..." + "\n\tTime spent: 0h0m\n\tSuccessfully completed\n\n- " + "Entering Second Block\nDoing second block...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert mongo_backend.count_documents(filter={}) @@ -187,9 +187,9 @@ def test_progress_updater_passing_params_sql(sql_backend, capsys): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\nDoing first block..." - "\n\tTime spent: 0h0m\n\tSuccessfully completed\n- " - "Entering ...\nDoing second block...\n\tTime spent: " + == "- Task: My task\n\n- Entering First Block\nDoing first block..." + "\n\tTime spent: 0h0m\n\tSuccessfully completed\n\n- " + "Entering Second Block\nDoing second block...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert sql_backend.exec(select(SQLLog)).first() @@ -214,9 +214,9 @@ def test_progress_updater_env_vars_redis( assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\nDoing first block..." - "\n\tTime spent: 0h0m\n\tSuccessfully completed\n- " - "Entering ...\nDoing second block...\n\tTime spent: " + == "- Task: My task\n\n- Entering First Block\nDoing first block..." + "\n\tTime spent: 0h0m\n\tSuccessfully completed\n\n- " + "Entering Second Block\nDoing second block...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert redis_backend.keys() @@ -240,9 +240,9 @@ def test_progress_updater_env_vars_mongo( assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\nDoing first block..." - "\n\tTime spent: 0h0m\n\tSuccessfully completed\n- " - "Entering ...\nDoing second block...\n\tTime spent: " + == "- Task: My task\n\n- Entering First Block\nDoing first block..." + "\n\tTime spent: 0h0m\n\tSuccessfully completed\n\n- " + "Entering Second Block\nDoing second block...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert mongo_backend.count_documents(filter={}) @@ -264,9 +264,9 @@ def test_progress_updater_env_vars_sql(sql_backend, env_vars_sql, capsys): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\nDoing first block..." - "\n\tTime spent: 0h0m\n\tSuccessfully completed\n- " - "Entering ...\nDoing second block...\n\tTime spent: " + == "- Task: My task\n\n- Entering First Block\nDoing first block..." + "\n\tTime spent: 0h0m\n\tSuccessfully completed\n\n- " + "Entering Second Block\nDoing second block...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert sql_backend.exec(select(SQLLog)).first() diff --git a/tests/test_updater/test_utils.py b/tests/test_updater/test_utils.py index 8b94423..76d2cdf 100644 --- a/tests/test_updater/test_utils.py +++ b/tests/test_updater/test_utils.py @@ -22,7 +22,7 @@ def task(): assert ( capsys.readouterr().out - == "- Task: Task\n- Entering ...\n\tTime spent: " + == "- Task: Task\n\n- Entering ...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert redis_backend.keys() @@ -51,7 +51,7 @@ def task(): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\n\tTime spent: " + == "- Task: My task\n\n- Entering ...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert mongo_backend.count_documents(filter={}) @@ -79,7 +79,7 @@ def task(): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\n\tTime spent: " + == "- Task: My task\n\n- Entering ...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert sql_backend.exec(select(SQLLog)).first() @@ -103,7 +103,7 @@ def task(): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\n\tTime spent: " + == "- Task: My task\n\n- Entering ...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert redis_backend.keys() @@ -126,7 +126,7 @@ def task(): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\n\tTime spent: " + == "- Task: My task\n\n- Entering ...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert mongo_backend.count_documents(filter={}) @@ -147,7 +147,7 @@ def task(): assert ( capsys.readouterr().out - == "- Task: My task\n- Entering ...\n\tTime spent: " + == "- Task: My task\n\n- Entering ...\n\tTime spent: " "0h0m\n\tSuccessfully completed\n" ) assert sql_backend.exec(select(SQLLog)).first()