Added aliases for individual patterns. Also fixed pattern download process
This commit is contained in:
parent
373d362d35
commit
ed847fd332
@ -1,6 +1,7 @@
|
|||||||
from .utils import Standalone, Update, Setup
|
from .utils import Standalone, Update, Setup, Alias
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
@ -57,15 +58,18 @@ def main():
|
|||||||
os.makedirs(config)
|
os.makedirs(config)
|
||||||
if args.setup:
|
if args.setup:
|
||||||
Setup().run()
|
Setup().run()
|
||||||
|
Alias()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
if not os.path.exists(env_file) or not os.path.exists(config_patterns_directory):
|
if not os.path.exists(env_file) or not os.path.exists(config_patterns_directory):
|
||||||
print("Please run --setup to set up your API key and download patterns.")
|
print("Please run --setup to set up your API key and download patterns.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
if not os.path.exists(config_patterns_directory):
|
if not os.path.exists(config_patterns_directory):
|
||||||
Update()
|
Update()
|
||||||
|
Alias()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
if args.update:
|
if args.update:
|
||||||
Update()
|
Update()
|
||||||
|
Alias()
|
||||||
print("Your Patterns have been updated.")
|
print("Your Patterns have been updated.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
if args.context:
|
if args.context:
|
||||||
|
@ -7,6 +7,9 @@ import platform
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from requests.exceptions import HTTPError
|
from requests.exceptions import HTTPError
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
import zipfile
|
||||||
|
import tempfile
|
||||||
|
import shutil
|
||||||
|
|
||||||
current_directory = os.path.dirname(os.path.realpath(__file__))
|
current_directory = os.path.dirname(os.path.realpath(__file__))
|
||||||
config_directory = os.path.expanduser("~/.config/fabric")
|
config_directory = os.path.expanduser("~/.config/fabric")
|
||||||
@ -214,124 +217,101 @@ class Standalone:
|
|||||||
|
|
||||||
class Update:
|
class Update:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
""" Initialize the object with default values and update patterns.
|
"""Initialize the object with default values."""
|
||||||
|
self.repo_zip_url = "https://github.com/danielmiessler/fabric/archive/refs/heads/main.zip"
|
||||||
This method initializes the object with default values for root_api_url, config_directory, and pattern_directory.
|
|
||||||
It then creates the pattern_directory if it does not exist and calls the update_patterns method to update the patterns.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
OSError: If there is an issue creating the pattern_directory.
|
|
||||||
"""
|
|
||||||
|
|
||||||
self.root_api_url = "https://api.github.com/repos/danielmiessler/fabric/contents/patterns?ref=main"
|
|
||||||
self.config_directory = os.path.expanduser("~/.config/fabric")
|
self.config_directory = os.path.expanduser("~/.config/fabric")
|
||||||
self.pattern_directory = os.path.join(
|
self.pattern_directory = os.path.join(
|
||||||
self.config_directory, "patterns")
|
self.config_directory, "patterns")
|
||||||
os.makedirs(self.pattern_directory, exist_ok=True)
|
os.makedirs(self.pattern_directory, exist_ok=True)
|
||||||
self.update_patterns() # Call the update process from a method.
|
self.update_patterns() # Start the update process immediately
|
||||||
|
|
||||||
def update_patterns(self):
|
def update_patterns(self):
|
||||||
""" Update the patterns by downloading from the GitHub directory.
|
"""Update the patterns by downloading the zip from GitHub and extracting it."""
|
||||||
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
Raises:
|
zip_path = os.path.join(temp_dir, "repo.zip")
|
||||||
HTTPError: If there is an HTTP error while downloading patterns.
|
self.download_zip(self.repo_zip_url, zip_path)
|
||||||
"""
|
extracted_folder_path = self.extract_zip(zip_path, temp_dir)
|
||||||
|
# The patterns folder will be inside "fabric-main" after extraction
|
||||||
try:
|
patterns_source_path = os.path.join(
|
||||||
self.progress_bar = tqdm(desc="Downloading Patterns…", unit="file")
|
extracted_folder_path, "fabric-main", "patterns")
|
||||||
self.get_github_directory_contents(
|
if os.path.exists(patterns_source_path):
|
||||||
self.root_api_url, self.pattern_directory
|
# If the patterns directory already exists, remove it before copying over the new one
|
||||||
)
|
if os.path.exists(self.pattern_directory):
|
||||||
# Close progress bar on success before printing the message.
|
shutil.rmtree(self.pattern_directory)
|
||||||
self.progress_bar.close()
|
shutil.copytree(patterns_source_path, self.pattern_directory)
|
||||||
except HTTPError as e:
|
print("Patterns updated successfully.")
|
||||||
# Ensure progress bar is closed on HTTPError as well.
|
|
||||||
self.progress_bar.close()
|
|
||||||
if e.response.status_code == 403:
|
|
||||||
print(
|
|
||||||
"GitHub API rate limit exceeded. Please wait before trying again."
|
|
||||||
)
|
|
||||||
sys.exit()
|
|
||||||
else:
|
else:
|
||||||
print(f"Failed to download patterns due to an HTTP error: {e}")
|
print("Patterns folder not found in the downloaded zip.")
|
||||||
sys.exit() # Exit after handling the error.
|
|
||||||
|
|
||||||
def download_file(self, url, local_path):
|
def download_zip(self, url, save_path):
|
||||||
""" Download a file from the given URL and save it to the local path.
|
"""Download the zip file from the specified URL."""
|
||||||
|
response = requests.get(url)
|
||||||
|
response.raise_for_status() # Check if the download was successful
|
||||||
|
with open(save_path, 'wb') as f:
|
||||||
|
f.write(response.content)
|
||||||
|
print("Downloaded zip file successfully.")
|
||||||
|
|
||||||
Args:
|
def extract_zip(self, zip_path, extract_to):
|
||||||
url (str): The URL of the file to be downloaded.
|
"""Extract the zip file to the specified directory."""
|
||||||
local_path (str): The local path where the file will be saved.
|
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
||||||
|
zip_ref.extractall(extract_to)
|
||||||
|
print("Extracted zip file successfully.")
|
||||||
|
return extract_to # Return the path to the extracted contents
|
||||||
|
|
||||||
Raises:
|
|
||||||
HTTPError: If an HTTP error occurs during the download process.
|
|
||||||
"""
|
|
||||||
|
|
||||||
try:
|
class Alias:
|
||||||
response = requests.get(url)
|
def __init__(self):
|
||||||
response.raise_for_status()
|
self.config_files = []
|
||||||
with open(local_path, "wb") as f:
|
home_directory = os.path.expanduser("~")
|
||||||
f.write(response.content)
|
self.patterns = os.path.join(home_directory, ".config/fabric/patterns")
|
||||||
self.progress_bar.update(1)
|
if os.path.exists(os.path.join(home_directory, ".bashrc")):
|
||||||
except HTTPError as e:
|
self.config_files.append(os.path.join(home_directory, ".bashrc"))
|
||||||
print(f"Failed to download file {url}. HTTP error: {e}")
|
if os.path.exists(os.path.join(home_directory, ".zshrc")):
|
||||||
sys.exit()
|
self.config_files.append(os.path.join(home_directory, ".zshrc"))
|
||||||
|
if os.path.exists(os.path.join(home_directory, ".bash_profile")):
|
||||||
|
self.config_files.append(os.path.join(
|
||||||
|
home_directory, ".bash_profile"))
|
||||||
|
self.remove_all_patterns()
|
||||||
|
self.add_patterns()
|
||||||
|
|
||||||
def process_item(self, item, local_dir):
|
def add(self, name, alias):
|
||||||
""" Process the given item and save it to the local directory.
|
for file in self.config_files:
|
||||||
|
with open(file, "a") as f:
|
||||||
|
f.write(f"alias {name}='{alias}'\n")
|
||||||
|
|
||||||
Args:
|
def remove(self, pattern):
|
||||||
item (dict): The item to be processed, containing information about the type, download URL, name, and URL.
|
for file in self.config_files:
|
||||||
local_dir (str): The local directory where the item will be saved.
|
# Read the whole file first
|
||||||
|
with open(file, "r") as f:
|
||||||
|
wholeFile = f.read()
|
||||||
|
|
||||||
Returns:
|
# Determine if the line to be removed is in the file
|
||||||
None
|
target_line = f"alias {pattern}='fabric --pattern {pattern}'\n"
|
||||||
|
if target_line in wholeFile:
|
||||||
|
# If the line exists, replace it with nothing (remove it)
|
||||||
|
wholeFile = wholeFile.replace(target_line, "")
|
||||||
|
|
||||||
Raises:
|
# Write the modified content back to the file
|
||||||
OSError: If there is an issue creating the new directory using os.makedirs.
|
with open(file, "w") as f:
|
||||||
"""
|
f.write(wholeFile)
|
||||||
|
|
||||||
if item["type"] == "file":
|
def remove_all_patterns(self):
|
||||||
self.download_file(
|
allPatterns = os.listdir(self.patterns)
|
||||||
item["download_url"], os.path.join(local_dir, item["name"])
|
for pattern in allPatterns:
|
||||||
)
|
self.remove(pattern)
|
||||||
elif item["type"] == "dir":
|
|
||||||
new_dir = os.path.join(local_dir, item["name"])
|
|
||||||
os.makedirs(new_dir, exist_ok=True)
|
|
||||||
self.get_github_directory_contents(item["url"], new_dir)
|
|
||||||
|
|
||||||
def get_github_directory_contents(self, api_url, local_dir):
|
def find_line(self, name):
|
||||||
""" Get the contents of a directory from GitHub API and process each item.
|
for file in self.config_files:
|
||||||
|
with open(file, "r") as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
for line in lines:
|
||||||
|
if line.strip("\n") == f"alias ${name}='{alias}'":
|
||||||
|
return line
|
||||||
|
|
||||||
Args:
|
def add_patterns(self):
|
||||||
api_url (str): The URL of the GitHub API endpoint for the directory.
|
allPatterns = os.listdir(self.patterns)
|
||||||
local_dir (str): The local directory where the contents will be processed.
|
for pattern in allPatterns:
|
||||||
|
self.add(pattern, f"fabric --pattern {pattern}")
|
||||||
Returns:
|
|
||||||
None
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
HTTPError: If an HTTP error occurs while fetching the directory contents.
|
|
||||||
If the status code is 403, it prints a message about GitHub API rate limit exceeded
|
|
||||||
and closes the progress bar. For any other status code, it prints a message
|
|
||||||
about failing to fetch directory contents due to an HTTP error.
|
|
||||||
"""
|
|
||||||
|
|
||||||
try:
|
|
||||||
response = requests.get(api_url)
|
|
||||||
response.raise_for_status()
|
|
||||||
jsonList = response.json()
|
|
||||||
for item in jsonList:
|
|
||||||
self.process_item(item, local_dir)
|
|
||||||
except HTTPError as e:
|
|
||||||
if e.response.status_code == 403:
|
|
||||||
print(
|
|
||||||
"GitHub API rate limit exceeded. Please wait before trying again."
|
|
||||||
)
|
|
||||||
self.progress_bar.close() # Ensure the progress bar is cleaned up properly
|
|
||||||
else:
|
|
||||||
print(
|
|
||||||
f"Failed to fetch directory contents due to an HTTP error: {e}")
|
|
||||||
|
|
||||||
|
|
||||||
class Setup:
|
class Setup:
|
||||||
@ -374,7 +354,6 @@ class Setup:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
Update()
|
Update()
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
""" Execute the Fabric program.
|
""" Execute the Fabric program.
|
||||||
|
1
setup.sh
1
setup.sh
@ -14,6 +14,7 @@ poetry install
|
|||||||
# List of commands to check and add or update alias for
|
# List of commands to check and add or update alias for
|
||||||
commands=("fabric" "fabric-api" "fabric-webui")
|
commands=("fabric" "fabric-api" "fabric-webui")
|
||||||
|
|
||||||
|
|
||||||
# List of shell configuration files to update
|
# List of shell configuration files to update
|
||||||
config_files=("$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.bash_profile")
|
config_files=("$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.bash_profile")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user