Skip to content

Commit

Permalink
PEP8 and pylint improvements. Tested and working with Python 3.9, 3.1…
Browse files Browse the repository at this point in the history
…1. Ongoing testing for others versions. Finally I have a dev pipeline and server to create tests. Enjoy!
  • Loading branch information
root committed Jul 6, 2024
1 parent 1d4400e commit f3b7e8a
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 196 deletions.
19 changes: 11 additions & 8 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# uglypy/cli.py
"""
uglypy - Command-line interface for running various scripts.
"""

import subprocess
import os
import argparse
from logging_setup import setup_logging, get_logger
from logging_setup import setup_logging

# Setup logging
logger = setup_logging()

def run_command(command):
"""Run a command with subprocess and log the outcome."""
logger.info(f"Running command: {' '.join(command)}")
logger.info("Running command: %s", ' '.join(command))
try:
subprocess.run(command, check=True)
logger.info(f"Command {' '.join(command)} executed successfully.")
logger.info("Command %s executed successfully.", ' '.join(command))
except subprocess.CalledProcessError as e:
logger.error(f"Command {' '.join(command)} failed: {e}")
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
logger.error("Command %s failed: %s", ' '.join(command), e)
except Exception as e: # pylint: disable=broad-except
logger.error("An unexpected error occurred: %s", e)

def run_streamlit(extra_args):
"""Run the Streamlit application."""
Expand All @@ -28,7 +31,7 @@ def run_script(script_name, extra_args):
script_path = os.path.join(os.getcwd(), script_name)

if not os.path.isfile(script_path):
logger.error(f"Error: {script_name} not found.")
logger.error("Error: %s not found.", script_name)
return

command = ["python", script_path] + extra_args
Expand Down
29 changes: 16 additions & 13 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import yaml
"""
Configuration Management for UglyFeed
"""

from pathlib import Path
import yaml

config_path = Path("config.yaml")
feeds_path = Path("input/feeds.txt")
CONFIG_PATH = Path("config.yaml")
FEEDS_PATH = Path("input/feeds.txt")

def tuple_constructor(loader, node):
"""Constructor for !!python/tuple tag."""
return tuple(loader.construct_sequence(node))

# Add the constructor to PyYAML with SafeLoader replaced by the FullLoader to handle tuples
# Add the constructor to PyYAML with FullLoader to handle tuples
yaml.add_constructor('tag:yaml.org,2002:python/tuple', tuple_constructor, Loader=yaml.FullLoader)

def load_config(config_file=config_path):
def load_config(config_file=CONFIG_PATH):
"""Load the configuration from the specified YAML file."""
if isinstance(config_file, str):
config_file = Path(config_file)

try:
if config_file.exists():
with open(config_file, "r") as f:
with open(config_file, "r", encoding='utf-8') as f:
return yaml.load(f, Loader=yaml.FullLoader) # Use yaml.FullLoader to support custom constructors
else:
return {}
return {}
except yaml.YAMLError as e:
raise Exception(f"Error loading YAML configuration: {e}")
raise Exception(f"Error loading YAML configuration: {e}") from e
except Exception as e:
raise Exception(f"Failed to load configuration from {config_file}: {e}")
raise Exception(f"Failed to load configuration from {config_file}: {e}") from e

def ensure_default_config(config_data):
"""Ensure all required keys are in the config_data with default values."""
Expand Down Expand Up @@ -123,12 +126,12 @@ def recursive_update(d, u):
def save_configuration(config_data, feeds):
"""Save configuration and feeds to file."""
try:
with open(config_path, "w") as f:
with open(CONFIG_PATH, "w", encoding='utf-8') as f:
yaml.dump(config_data, f)
with open(feeds_path, "w") as f:
with open(FEEDS_PATH, "w", encoding='utf-8') as f:
f.write(feeds)
except Exception as e:
raise Exception(f"Failed to save configuration: {e}")
raise Exception(f"Failed to save configuration: {e}") from e

# Usage example
if __name__ == "__main__":
Expand Down
60 changes: 36 additions & 24 deletions deploy_xml.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
"""
This script uploads XML files to GitHub and GitLab repositories.
"""

import os
import yaml
import requests
import base64
import logging
import requests
import yaml

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Function to load configuration from YAML or environment variables
def load_config(config_path='config.yaml'):
logging.info(f"Loading configuration from {config_path} or environment variables...")
"""
Load configuration from YAML or environment variables.
"""
logging.info("Loading configuration from %s or environment variables...", config_path)
if os.path.exists(config_path):
with open(config_path, 'r') as file:
with open(config_path, 'r', encoding='utf-8') as file:
config = yaml.safe_load(file)
else:
logging.warning(f"Configuration file {config_path} not found. Falling back to environment variables.")
logging.warning("Configuration file %s not found. Falling back to environment variables.", config_path)
config = {}

config['github_token'] = config.get('github_token', os.getenv('GITHUB_TOKEN'))
Expand All @@ -26,8 +32,10 @@ def load_config(config_path='config.yaml'):

return config

# Function to upload file to GitHub
def upload_to_github(file_path, config):
"""
Upload file to GitHub.
"""
logging.info("Uploading to GitHub...")
repo_name = config['github_repo']
token = config['github_token']
Expand All @@ -43,7 +51,7 @@ def upload_to_github(file_path, config):
content = base64.b64encode(file.read()).decode('utf-8')

# Check if the file exists in the repository
response = requests.get(url, headers=headers)
response = requests.get(url, headers=headers, timeout=10)
if response.status_code == 200:
# File exists, retrieve its SHA
sha = response.json()['sha']
Expand All @@ -54,7 +62,7 @@ def upload_to_github(file_path, config):
'branch': 'main'
}
method = requests.put
logging.info(f"File {file_name} exists in GitHub repo, updating it.")
logging.info("File %s exists in GitHub repo, updating it.", file_name)
elif response.status_code == 404:
# File does not exist, create it
data = {
Expand All @@ -63,22 +71,24 @@ def upload_to_github(file_path, config):
'branch': 'main'
}
method = requests.put
logging.info(f"File {file_name} does not exist in GitHub repo, creating it.")
logging.info("File %s does not exist in GitHub repo, creating it.", file_name)
else:
logging.error(f"GitHub file check failed: {response.text}")
logging.error("GitHub file check failed: %s", response.text)
raise Exception(f"GitHub file check failed: {response.text}")

# Upload or update the file
response = method(url, json=data, headers=headers)
response = method(url, json=data, headers=headers, timeout=10)
if response.status_code in (200, 201):
download_url = response.json()['content']['download_url']
return download_url
else:
logging.error(f"GitHub upload failed: {response.text}")
logging.error("GitHub upload failed: %s", response.text)
raise Exception(f"GitHub upload failed: {response.text}")

# Function to upload file to GitLab
def upload_to_gitlab(file_path, config):
"""
Upload file to GitLab.
"""
logging.info("Uploading to GitLab...")
repo_name = config['gitlab_repo']
token = config['gitlab_token']
Expand All @@ -88,7 +98,7 @@ def upload_to_gitlab(file_path, config):
file_name = os.path.basename(file_path)
url = f'https://gitlab.com/api/v4/projects/{repo_name}/repository/files/{file_name}'

with open(file_path, 'r') as file:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()

data = {
Expand All @@ -97,41 +107,43 @@ def upload_to_gitlab(file_path, config):
'commit_message': 'Add uglyfeed.xml'
}

response = requests.post(url, json=data, headers=headers)
response = requests.post(url, json=data, headers=headers, timeout=10)
if response.status_code == 201:
download_url = f'https://gitlab.com/{repo_name}/-/raw/main/{file_name}'
return download_url
elif response.status_code == 400 and 'already exists' in response.text:
# Update file if it already exists
logging.info("File already exists on GitLab, attempting to update...")
response = requests.put(url, json=data, headers=headers)
response = requests.put(url, json=data, headers=headers, timeout=10)
if response.status_code == 200:
download_url = f'https://gitlab.com/{repo_name}/-/raw/main/{file_name}'
return download_url
else:
logging.error(f"GitLab update failed: {response.text}")
logging.error("GitLab update failed: %s", response.text)
raise Exception(f"GitLab update failed: {response.text}")
else:
logging.error(f"GitLab upload failed: {response.text}")
logging.error("GitLab upload failed: %s", response.text)
raise Exception(f"GitLab upload failed: {response.text}")

# Main function to deploy XML file
def deploy_xml(file_path, config):
"""
Deploy XML file to GitHub and GitLab based on the configuration.
"""
urls = {}

if config.get('enable_github', False):
try:
github_url = upload_to_github(file_path, config)
urls['github'] = github_url
except Exception as e:
logging.error(f"GitHub upload error: {e}")
logging.error("GitHub upload error: %s", e)

if config.get('enable_gitlab', False):
try:
gitlab_url = upload_to_gitlab(file_path, config)
urls['gitlab'] = gitlab_url
except Exception as e:
logging.error(f"GitLab upload error: {e}")
logging.error("GitLab upload error: %s", e)

return urls

Expand All @@ -140,10 +152,10 @@ def deploy_xml(file_path, config):
config = load_config()

# File to deploy
xml_file_path = 'uglyfeeds/uglyfeed.xml'
XML_FILE_PATH = 'uglyfeeds/uglyfeed.xml'

# Deploy the XML file
urls = deploy_xml(xml_file_path, config)
urls = deploy_xml(XML_FILE_PATH, config)

# Print the URLs
if urls:
Expand Down
Loading

0 comments on commit f3b7e8a

Please sign in to comment.