skill-library / tools.py
Sayoyo's picture
[feat] first commit
f74f125
import os
import json
from datetime import datetime
import re
def get_skill_files():
skill_files = []
skill_doc_files = []
for root, dirs, files in os.walk("."):
if "skill.json" in files:
skill_files.append(os.path.join(root, "skill.json"))
if "skill_doc.md" in files:
skill_doc_files.append(os.path.join(root, "skill_doc.md"))
return skill_files, skill_doc_files
def get_skill_data(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
return data
def get_skill_doc_data(file_path):
if not os.path.exists(file_path):
return None
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
title = lines[0].strip().replace("# ", "") if lines else None
content = "".join(lines)
# Regular expressions to extract the desired sections
usage_pattern = r"# Usage:\n(.*?)\n\n"
parameters_pattern = r"# Parameters:\n(.*?)\n\n"
returns_pattern = r"# Returns:\n(.*?)(\n\n|$|#)"
usage = re.search(usage_pattern, content, re.DOTALL)
parameters = re.search(parameters_pattern, content, re.DOTALL)
returns_ = re.search(returns_pattern, content, re.DOTALL)
# Extract the matched content or set to None if not found
usage_content = usage.group(1).strip() if usage else None
parameters_content = parameters.group(1).strip() if parameters else None
returns_content = returns_.group(1).strip() if returns_ else None
return {
"Title": title,
"Usage": usage_content,
"Parameters": parameters_content,
"Returns": returns_content
}
def get_skill_datas():
skill_files, skill_doc_files = get_skill_files()
skills_datas=[]
for i, file in enumerate(skill_files):
skill_doc_file = skill_doc_files[i]
skill = get_skill_data(file)
skill_doc = get_skill_doc_data(skill_doc_file)
if skill_doc:
skill.update(skill_doc)
skills_datas.append(skill)
return skills_datas
def time_ago(updated_at_str):
now = datetime.now()
updated_at = datetime.strptime(updated_at_str, '%Y-%m-%d %H:%M:%S')
delta = now - updated_at
minutes = delta.total_seconds() / 60
hours = minutes / 60
days = hours / 24
months = days / 30.44 # An average month length
years = days / 365.25 # Account for leap years
if minutes < 60:
return f"{int(minutes)} minutes ago"
elif hours < 24:
return f"{int(hours)} hours ago"
elif days < 30.44:
return f"{int(days)} days ago"
elif months < 12:
return f"{int(months)} months ago"
else:
return f"{int(years)} years ago"