Skip to content

Commit

Permalink
25 add options to adjust outfile variable grouping (#28)
Browse files Browse the repository at this point in the history
* [add] LICENSE (#19)

* [update] version to 0.1.1-alpha.0

* [update] README

* [update] pyproject.toml metadata

* [fix] homepage not website

* [fix] mail addr in pyproject.toml

* [version] bump to 0.1.1

* 9 setup actions to run code quality control programs (#10)

* [add] workflow

* [update] add html test coverage reports

* [update] add new line before EOF

* [update] run actin only for py3.9 and 3.10

* [update] workflow

* [update] non-interactive poetry project installation

* [update] do not spawn poetry shell

* [update] pre-commit-config

* [update] stub group config

* [update] add pydocstyle

* [update] main.yml

* [update] fix isolation issue in one unit test

* [update] default debugging mode to no-debug
  • Loading branch information
pekasen authored Oct 24, 2022
1 parent ec4ebd5 commit e671ac2
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 28 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Python package

on:
pull_request:
branches: ["trunk"]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10"] # "3.7", "3.8", "

steps:
- uses: actions/checkout@v3
- name: Setup Python # Set Python version
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
# Install pip and pytest
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install poetry
poetry install --no-interaction --no-root
- name: Test with pytest
run: poetry run pytest --cov-report html:coverage-${{ matrix.python-version }} --doctest-modules --disable-socket --cov=tegracli --capture=sys
- name: Upload pytest test results
uses: actions/upload-artifact@v3
with:
name: pytest-results-${{ matrix.python-version }}
path: |
coverage-${{ matrix.python-version }}
# Use always() to always run this step to publish test results when there are test failures
if: ${{ always() }}
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ repos:
- id: check-yaml
- id: check-added-large-files
- id: check-toml
- repo: https://github.com/PyCQA/pydocstyle
rev: 6.1.1
hooks:
- id: pydocstyle
args: [--convention=google]
# format code files with black
- repo: local
hooks:
Expand Down
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 Leibniz-Institute for Media Research | Hans-Bredow-Institute

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ A convenience wrapper around Telethon and the Telegram Client API for research p

`tegracli` uses Poetry and python >= 3.9 for building and installing.

To install using pipx, download the latest release and run `pipx install ./tegracli-x-x-x.`.
A release via pip is planned for the future, in this case install simply with `pipx install tegracli`.
To install using pipx, run the following command `pipx install tegracli`.

## How to get API keys

Expand Down
14 changes: 12 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
[tool.poetry]
name = "tegracli"
version = "0.1.0"
version = "0.1.1"
description = "A research-focused Telegram CLI application"
authors = ["Philipp Kessling"]
authors = ["Philipp Kessling <[email protected]>", "Felix Münch <[email protected]>"]
readme = "README.md"
license = "MIT"
repository = "https://github.com/Leibniz-HBI/tegracli"
homepage = "https://pypi.org/project/tegracli/"
classifiers = [
"Development Status :: 3 - Alpha",
"Environment :: Console",
"Intended Audience :: Science/Research",
]

[tool.poetry.dependencies]
python = "^3.9"
Expand All @@ -22,6 +31,7 @@ black = "^22.6.0"
isort = "^5.10.1"
pytest-cov = "^3.0.0"
coverage = "^6.4.2"
pydocstyle = "^6.1.1"

[tool.pytest.ini_options]
addopts = "--disable-socket --cov-report html:tests/coverage --cov=tegracli --capture=sys"
Expand Down
16 changes: 7 additions & 9 deletions tegracli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


@click.group()
@click.option("--debug/--no-debug", default=True)
@click.option("--debug/--no-debug", default=False)
@click.pass_context
def cli(ctx: click.Context, debug: bool):
"""Tegracli!! Retrieve messages from *Te*le*gra*m with a *CLI*!"""
Expand Down Expand Up @@ -138,7 +138,7 @@ def get( # pylint: disable=too-many-arguments

@cli.group()
def group():
"""Manage account groups"""
"""Manage account groups."""
return


Expand All @@ -165,7 +165,7 @@ def init(
name: str,
accounts: List[str],
):
"""initialize a new account group"""
"""Initialize a new account group."""
cwd = Path()
results_directory = cwd / name
params = {"limit": limit, "reverse": True}
Expand Down Expand Up @@ -208,7 +208,7 @@ def init(
@group.command()
@click.argument("groups", nargs=-1)
def reset(groups: Tuple[str]):
"""reset unreachable members for each group"""
"""Reset unreachable members for each group."""
for _group in groups:
cwd = Path()
conf = _guarded_group_load(cwd, _group)
Expand All @@ -220,7 +220,7 @@ def reset(groups: Tuple[str]):
@click.argument("groups", nargs=-1)
@click.pass_context
def run(ctx: click.Context, groups: Tuple[str]):
"""load a group configuration and run the groups operations"""
"""Load a group configuration and run the groups operations."""
run_group(ctx.obj["client"], groups)


Expand Down Expand Up @@ -308,7 +308,7 @@ def _handle_group_member(member: str, conf: Group, client: TelegramClient) -> No


def run_group(client: TelegramClient, groups: Tuple[str]):
"""runs the required operations for the specified groups."""
"""Runs the required operations for the specified groups."""
cwd = Path()

# iterate groups
Expand Down Expand Up @@ -338,9 +338,7 @@ def _guarded_group_load(cwd: Path, _name: str) -> Group:
@click.argument("queries", nargs=-1)
@click.pass_context
def search(ctx: click.Context, queries: list[str]):
"""This function searches Telegram content that is available to your account
for the specified search term(s).
"""
"""Searches Telegram content that is available to your account."""
client = get_client(ctx.obj["credentials"])

with client:
Expand Down
2 changes: 2 additions & 0 deletions tests/.stubs/tegracli_group.conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ name: behoerden
params:
limit: 100
reverse: true
unreachable_members: []
error_state: {}
44 changes: 29 additions & 15 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
""" # Command Line Interface Tests
"""Command Line Interface Tests.
This test suite tests the program's CLICK interface.
Tests should be run with click.CliRunner.
"""

# pylint: disable=redefined-outer-name
Expand All @@ -26,7 +25,7 @@

@pytest.fixture
def group_config() -> Path:
"""get a path to a valid group config"""
"""Get a path to a valid group config."""
with Path("tests/.stubs/tegracli_group.conf.yml").open(
"r", encoding="utf8"
) as file:
Expand All @@ -35,12 +34,12 @@ def group_config() -> Path:

@pytest.fixture
def runner():
"""Fixture for click.CliRunner"""
"""Fixture for click.CliRunner."""
return CliRunner()


def test_help_prompt(runner: CliRunner, tmpdir: Path):
"""Should show help if no args present"""
"""Should show help if no args present."""
with runner.isolated_filesystem(tmpdir):
result = runner.invoke(cli, ["--help"])

Expand All @@ -49,7 +48,7 @@ def test_help_prompt(runner: CliRunner, tmpdir: Path):


def test_configuration_present(runner: CliRunner, tmp_path: Path):
"""Should not throw if a configuration file is present"""
"""Should not throw if a configuration file is present."""
with runner.isolated_filesystem(temp_dir=tmp_path) as temp_dir:

conf_file = Path(temp_dir) / "tegracli.conf.yml"
Expand All @@ -72,22 +71,23 @@ def test_configuration_present(runner: CliRunner, tmp_path: Path):


def test_configuration_missing(runner: CliRunner, tmpdir: Path):
"""Should fail if no config is present in the current directory"""
"""Should fail if no config is present in the current directory."""
with runner.isolated_filesystem(tmpdir):
result = runner.invoke(cli, ["get", "channel"])
print(result.stdout)
assert result.exit_code == 127


def test_account_group_creation(runner: CliRunner, tmp_path: Path):
"""Should create a directory named by user input, read in a file with tg-accounts,
"""Should create a group.
For this it must create a directory named by user input, read in a file with tg-accounts,
resolve those user names and save messages by user in a jsonl-file.
I.e. a call to this command would look like this:
`tegracli group init --read_file account_list.csv my_little_account_list` to
load a account list from a file and write the configuration file to disk.
"""

with runner.isolated_filesystem(temp_dir=tmp_path) as temp_dir:
conf_file = Path(temp_dir) / "tegracli.conf.yml"
list_file = Path(temp_dir) / "account_list.csv"
Expand Down Expand Up @@ -130,14 +130,15 @@ def test_account_group_creation(runner: CliRunner, tmp_path: Path):


def test_account_group_run(runner: CliRunner, group_config: Path, tmp_path: Path):
"""Should create a directory named by user input, read in a file with tg-accounts,
"""Should run a group.
It must create a directory named by user input, read in a file with tg-accounts,
resolve those user names and save messages by user in a jsonl-file.
I.e. a call to this command would look like this:
`tegracli group init --read_file account_list.csv my_little_account_list` to
load a account list from a file and write the configuration file to disk.
"""

with runner.isolated_filesystem(temp_dir=tmp_path) as temp_dir:
conf_file = Path(temp_dir) / "tegracli.conf.yml"
r_folder = Path(temp_dir) / "behoerden/"
Expand Down Expand Up @@ -170,12 +171,25 @@ def test_account_group_run(runner: CliRunner, group_config: Path, tmp_path: Path
assert result.exit_code == 0 # indicating success?


def test_search(runner: CliRunner):
"""Should get search results for specified terms"""
def test_search(runner: CliRunner, tmp_path: Path):
"""Should get search results for specified terms."""
with runner.isolated_filesystem(temp_dir=tmp_path) as temp_dir:

result = runner.invoke(cli, ["search", "term"])
conf_file = Path(temp_dir) / "tegracli.conf.yml"

assert result.exit_code == 0
with conf_file.open("w") as config:
yaml.dump(
{
"api_id": 123456,
"api_hash": "wahgi231kmdma91",
"session_name": "test",
},
config,
)

result = runner.invoke(cli, ["search", "term"])

assert result.exit_code == 0


patcher.stop()

0 comments on commit e671ac2

Please sign in to comment.