Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed logging #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
51 changes: 51 additions & 0 deletions root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"source": {
"categories": [
{
"name": "test 1",
"enDesc": "",
"enShortDesc": ""
}
],
"books": [
{
"title": "test 1",
"language": "en",
"versionSource": " ",
"completestatus": "in_progress",
"content": [[
"Lorem ipsum dolor sit amet.",
"The quick brown fox jumps over the lazy dog.",
"This is dssa sample text for testing purposes.",
"Dummy text goes here.",
"Just anotssher placeholder text.",
"Random words to fill space.",
"Text examplssse number seven.",
"This is onlsy a test string.",
"Another line sof dummy content.",
"Nothing to sese here, move along."
]],
"direction": "ltr"
}
]
},
"target": {
"categories": [
{
"name": "test 1 bo",
"heDesc": "",
"heShortDesc": ""
}
],
"books": [
{
"title": "test 1 bo",
"language": "bo",
"versionSource": " ",
"completestatus": "in_progress",
"content": [],
"direction": "ltr"
}
]
}
}
28 changes: 25 additions & 3 deletions src/pecha_uploader/category/extract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import urllib
from urllib.error import HTTPError

from pecha_uploader.config import baseURL, headers
from pecha_uploader.config import text_info_logger # <--- Import the Info Logger
from pecha_uploader.config import (
baseURL,
headers,
log_error,
log_info,
text_error_logger,
)


def get_category(category_name: str):
Expand All @@ -16,9 +23,24 @@ def get_category(category_name: str):
response = urllib.request.urlopen(req)
res = response.read().decode("utf-8")
if "error" not in res:
log_info(text_info_logger, f"{res}")
return {"status": True}
elif "already exists" in res["error"]:
log_info(text_info_logger, f"Category already exists : {category_name}")
return {"status": True}
return {"status": False, "error": res}
log_error(text_error_logger, f"Category check failed: {res}")
return {"status": False}
except HTTPError as e:
return {"status": False, "error": e}
error_message = e.read().decode("utf-8")
log_error(
text_error_logger,
f"HTTPError while fetching category {{category_name}}: {error_message}",
)
return {"status": False}

except Exception as e:
log_error(
text_error_logger,
f"Unexpected error while fetching category {category_name}: {str(e)}",
)
return {"status": False}
27 changes: 23 additions & 4 deletions src/pecha_uploader/category/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
from urllib.parse import urlencode
from urllib.request import Request, urlopen

from pecha_uploader.config import PECHA_API_KEY, Destination_url, headers
from pecha_uploader.config import text_info_logger # <--- Import the Info Logger
from pecha_uploader.config import (
PECHA_API_KEY,
Destination_url,
headers,
log_error,
log_info,
text_error_logger,
)


def post_category(
Expand Down Expand Up @@ -36,14 +44,25 @@ def post_category(
data = urlencode(values)
binary_data = data.encode("ascii")
req = Request(url, binary_data, headers=headers)

category_name = list(map(lambda x: x["name"], en_category_list))[-1]
try:
response = urlopen(req)
res = response.read().decode("utf-8")
if "error" not in res:
log_info(text_info_logger, f"Uploaded: {category_name}")
return {"status": True}
elif "already exists" in res:
log_info(text_info_logger, f"Category already exists: {category_name}")
return {"status": True}
return {"status": True, "error": res}
return {"status": True}
except HTTPError as e:
return {"status": False, "error": e}
# error_message = e.read().decode("utf-8")
log_error(text_error_logger, f"HTTPError while posting category: {e.code}")

return {"status": False}

except Exception as e:
log_error(
text_error_logger, f"Unexpected error while posting category: {str(e)}"
)
return {"status": False}
82 changes: 55 additions & 27 deletions src/pecha_uploader/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
from enum import Enum
from pathlib import Path
Expand All @@ -7,11 +8,11 @@

def _mkdir_if_not(path: Path):
"""Create a directory if it does not exist"""
if not path.exists():
path.mkdir(exist_ok=True, parents=True)
path.mkdir(exist_ok=True, parents=True)
return path


# Paths
BASE_PATH = _mkdir_if_not(Path.home() / ".pecha_uploader")
TEXT_PATH = _mkdir_if_not(BASE_PATH / "texts")
LINK_PATH = _mkdir_if_not(BASE_PATH / "links")
Expand All @@ -20,16 +21,58 @@ def _mkdir_if_not(path: Path):
TEXT_ERROR_LOG = TEXT_PATH / "errors.txt"
TEXT_ERROR_ID_LOG = TEXT_PATH / "errors_text_id.txt"
TEXT_SUCCESS_LOG = TEXT_PATH / "success.txt"
TEXT_INFO_LOG = TEXT_PATH / "info.txt" # <--- New Info Log

LINK_ERROR_LOG = LINK_PATH / "errors.txt"
LINK_ERROR_ID_LOG = LINK_PATH / "errors_link_id.txt"
LINK_SUCCESS_LOG = LINK_PATH / "success.txt"
LINK_INFO_LOG = LINK_PATH / "info.txt" # <--- New Info Log


# Formatter
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")


def setup_logger(name, log_file, level=logging.INFO):
"""Set up a logger for different log files"""
logger = logging.getLogger(name)
logger.setLevel(level)

# File Handler
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(level)
file_handler.setFormatter(formatter)

# Console Handler (for warnings and above)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.WARNING)
console_handler.setFormatter(formatter)

# Avoid duplicate handlers
if not logger.hasHandlers():
logger.addHandler(file_handler)
logger.addHandler(console_handler)

return logger


# Loggers for different purposes
text_error_logger = setup_logger("text_error_logger", TEXT_ERROR_LOG, logging.ERROR)
text_success_logger = setup_logger(
"text_success_logger", TEXT_SUCCESS_LOG, logging.INFO
)
text_info_logger = setup_logger("text_info_logger", TEXT_INFO_LOG, logging.INFO)
link_error_logger = setup_logger("link_error_logger", LINK_ERROR_LOG, logging.ERROR)
link_success_logger = setup_logger(
"link_success_logger", LINK_SUCCESS_LOG, logging.INFO
)
link_info_logger = setup_logger("link_info_logger", LINK_INFO_LOG, logging.INFO)


headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" # noqa
}
BASEPATH = os.path.dirname(os.path.abspath(__file__)) # path to `Pecha.org/tools`
BASEPATH = os.path.dirname(os.path.abspath(__file__)) # Path to `Pecha.org/tools`


class Destination_url(Enum):
Expand All @@ -38,37 +81,22 @@ class Destination_url(Enum):
LOCAL = "http://127.0.0.1:8000/"


def log_error(file_name: Path, text_name: str, message: str):
"""Log error to file"""
if not file_name.exists():
make_empty_file(file_name)

with open(file_name, "a", encoding="utf-8") as log_file:
log_file.write(f"{text_name} : {message}\n") # noqa
def log_error(logger, message: str):
"""Log an error message"""
logger.error(f" {message}")


def log_error_id(file_name: Path, text_name: str):
"""Log error with id to file"""
if not file_name.exists():
make_empty_file(file_name)
with open(file_name, "a", encoding="utf-8") as log_file:
log_file.write(f"{text_name}\n")
def log_success(logger, text_name: str):
"""Log a success message"""
logger.info(f"{text_name}")


def log_success(file_name: Path, text_name: str):
"""Log success to file"""
if not file_name.exists():
make_empty_file(file_name)
with open(file_name, "a", encoding="utf-8") as log_file:
log_file.write(f"{text_name}\n")


def make_empty_file(file_name: Path):
file_name.write_text("", encoding="utf-8")
def log_info(logger, message: str):
"""Log an informational message"""
logger.info(f"{message}")


def set_api_key(api_key: str):
if not api_key:
raise ValueError("PECHA API KEY is not given properly.")

os.environ["PECHA_API_KEY"] = api_key
17 changes: 14 additions & 3 deletions src/pecha_uploader/index/extract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import urllib
from urllib.error import HTTPError

from pecha_uploader.config import baseURL, headers
from pecha_uploader.config import baseURL, headers, log_error, text_error_logger


def get_index(index: str):
Expand All @@ -14,6 +14,17 @@ def get_index(index: str):
url = f"{index_url}/{prepare_index_str}?with_content_counts=1"
req = urllib.request.Request(url, method="GET", headers=headers)
try:
response = urllib.request.urlopen(req) # noqa
response = urllib.request.urlopen(req) # noqa]
return {"status": True}
except HTTPError as e:
print("Error code: ", e.code)
# Handle HTTP errors
log_error(
text_error_logger,
f"HTTP Error {e.code} occurred extracting index: {e.read().decode('utf-8')}",
)
return {"status": False}

except Exception as e:
# Handle other exceptions
log_error(text_error_logger, f"Unexpected error occurred: {e}")
return {"status": False}
24 changes: 21 additions & 3 deletions src/pecha_uploader/index/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
from typing import Dict, List
from urllib.error import HTTPError

from pecha_uploader.config import PECHA_API_KEY, Destination_url, headers
from pecha_uploader.config import text_info_logger # <--- Import the Info Logger
from pecha_uploader.config import (
PECHA_API_KEY,
Destination_url,
headers,
log_error,
log_info,
text_error_logger,
)


def post_index(
Expand Down Expand Up @@ -62,7 +70,17 @@ def post_index(
response = urllib.request.urlopen(req)
res = response.read().decode("utf-8")
if "error" in res and "already exists." not in res:
return {"status": False, "error": res}
log_error(text_error_logger, f"API error response: {res}")
return {"status": False}

log_info(text_info_logger, f"Index uploaded: {index_str}")
return {"status": True}

except HTTPError as e:
return {"status": False, "error": e.read()}
error_message = e.read().decode("utf-8")
log_error(text_error_logger, f"HTTPError while posting index: {error_message}")
return {"status": False}

except Exception as e:
log_error(text_error_logger, f"Unexpected error while posting index: {str(e)}")
return {"status": False}
26 changes: 22 additions & 4 deletions src/pecha_uploader/links/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
import urllib
from urllib.error import HTTPError

from pecha_uploader.config import PECHA_API_KEY, Destination_url, headers
from pecha_uploader.config import (
PECHA_API_KEY,
Destination_url,
headers,
log_error,
log_info,
text_error_logger,
text_info_logger,
)


def remove_links(text_title: str, destination_url: Destination_url):
Expand All @@ -21,7 +29,17 @@ def remove_links(text_title: str, destination_url: Destination_url):
req = urllib.request.Request(url, binary_key, method="DELETE", headers=headers)
try:
urllib.request.urlopen(req)
log_info(text_info_logger, f"Successfully removed link for: {text_title}")

except (HTTPError) as e:
print("Error code: ", e.code)
print("error", e.read())
except HTTPError as e:
# Handle HTTP errors
log_error(
text_error_logger,
f"HTTP Error {e.code} occurred while removing link: {e.read().decode('utf-8')}",
)
raise HTTPError(f"HTTP Error occurred while removing link: {e.code}")

except Exception as e:
# Handle other exceptions
log_error(text_error_logger, f"Unexpected error occurred: {e}")
raise Exception(f"An unexpected error occurred while removing link: {e}")
14 changes: 11 additions & 3 deletions src/pecha_uploader/links/extract.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import urllib
from urllib.error import HTTPError

from pecha_uploader.config import baseURL, headers
from pecha_uploader.config import baseURL, headers, link_error_logger, log_error


def get_link(link_name: str, with_text=1):
Expand All @@ -25,5 +25,13 @@ def get_link(link_name: str, with_text=1):
req = urllib.request.Request(url, method="GET", headers=headers)
try:
response = urllib.request.urlopen(req) # noqa
except (HTTPError) as e:
print("Error code: ", e.code)
return {"status": True, "data": response}
except HTTPError as e:
# Handle HTTP errors (e.g., 404, 500, etc.)
log_error(link_error_logger, f"HTTP Error occurred. Status code: {e.code}")
return {"status": False}

except Exception as e:
# Handle other unexpected errors (e.g., network issues, invalid URLs, etc.)
log_error(link_error_logger, f"An unexpected error occurred: {e}")
return {"status": False}
Loading