Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/github.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Trgoman API CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Run lint
run: pip install flake8 && flake8 --ignore=E501 api.py

- name: Run tests
run: pytest -v

- name: Build Docker image
run: docker build -t translate-app .

- name: Run Docker container
run: docker run --rm translate-app
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.11-slim

WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy project files
COPY . .

# Default command: run tests
CMD ["pytest", "-v"]
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 🐍 Python Translator with CI/CD 🚀

This project is a simple **Python translator client** for the [Targoman API](https://targoman.ir), fully containerized with Docker and tested automatically using **GitHub Actions (CI/CD)**.

---

## 📂 Project Structure
- `api.py` → Main code for translation
- `test_translate.py` → Unit tests (with pytest & mock)
- `Dockerfile` → Container setup
- `.github/workflows/ci.yml` → CI/CD pipeline

---

## ⚡ Features
- ✅ Translation (fa → en / en → fa)
- ✅ Unit tests with `pytest`
- ✅ Linting with `flake8`
- ✅ Dockerized environment
- ✅ GitHub Actions CI/CD

---

## 🔧 Run Locally

```bash
# Create venv
python -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Run tests
pytest -v
Binary file added __pycache__/api.cpython-312.pyc
Binary file not shown.
Binary file not shown.
39 changes: 39 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests as req
import json

URL = "Https://targoman.ir/API"


def translate(word: str = "سلام", fromLang: str = "fa", toLang: str = "en") -> dict:
payload = {
"jsonrpc": "2.0",
"method": "Targoman::translate",
"id": 1,
"params": [
"sSTargomanWUI",
word,
"%s2%s" % (fromLang.lower().strip(), toLang.lower().strip()),
"127.0.0.10",
"NMT",
True,
True,
True,
None,
"formal",
],
}

data = req.post(URL, json=payload)
data = json.loads(data.text)
return data


def Translate(word: str, fromLang: str, toLang: str) -> str:
if fromLang == "fa" and toLang == "en":
return translate(word, toLang=toLang, fromLang=fromLang)["result"]["tr"][
"base"
][0][1]
if fromLang == "en" and toLang == "fa":
return translate(word, toLang=toLang, fromLang=fromLang)["result"]["tr"][
"base"
][0][1]
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
pytest
27 changes: 27 additions & 0 deletions test_translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from unittest.mock import patch
import json

from api import Translate


# Mock response data
mock_response_fa_en = {"result": {"tr": {"base": [[0, "Hello"]]}}}

mock_response_en_fa = {"result": {"tr": {"base": [[0, "سلام"]]}}}


@patch("requests.post")
def test_translate_fa_to_en(mock_post):
# Mock the requests.post response
mock_post.return_value.text = json.dumps(mock_response_fa_en)

result = Translate("سلام", "fa", "en")
assert result == "Hello"


@patch("requests.post")
def test_translate_en_to_fa(mock_post):
mock_post.return_value.text = json.dumps(mock_response_en_fa)

result = Translate("Hello", "en", "fa")
assert result == "سلام"