Skip to content

Commit

Permalink
Merge pull request #247 from ma10/improve-axe-core-repo-handling-2024…
Browse files Browse the repository at this point in the history
…0514

axe-rules.rstに表示するタイムスタンプとバージョンについて、submoduleが指すcommitのものを取得するように変更
  • Loading branch information
ma10 authored May 14, 2024
2 parents 5294df1 + 7197a34 commit 67b1398
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
60 changes: 36 additions & 24 deletions tools/yaml2rst/a11y_guidelines_initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import json
import time
import yaml
from git import Repo
import git
from jsonschema import validate, ValidationError, RefResolver
from a11y_guidelines import Category, WcagSc, InfoRef, Guideline, Check, Faq, FaqTag, CheckTool, AxeRule, RelationshipManager
from constants import CHECK_TOOLS, DEQUE_URL
from constants import CHECK_TOOLS, AXE_CORE
from path import get_src_path

def setup_instances(settings):
Expand Down Expand Up @@ -43,37 +43,49 @@ def setup_instances(settings):
for entity_type, srcdir, schema_filename, constructor in entity_config:
process_entity_files(srcdir, src_path['schema'], schema_filename, resolver, constructor)

process_axe_rules(src_path['axe_rules'], src_path['axe_msg_ja'], src_path['axe_pkg'], DEQUE_URL)
process_axe_rules(basedir, AXE_CORE)

rel = RelationshipManager()
rel.resolve_faqs()
return rel

def process_axe_rules(axe_rules_dir, axe_msg_ja_file, axe_pkg_file, base_url):
try:
file_content = read_file_content(axe_msg_ja_file)
except Exception as e:
handle_file_error(e, axe_msg_ja_file)
def process_axe_rules(basedir, AXE_CORE):
root_repo = git.Repo(basedir)
submodule = None
for sm in root_repo.submodules:
if sm.name == AXE_CORE['submodule_name']:
submodule = sm
break

if submodule is None:
raise ValueError(f'Submodule with name {AXE_CORE["submodule_name"]} not found.')

axe_base = os.path.join(basedir, submodule.path)
axe_commit_id = submodule.hexsha
axe_repo = git.Repo(axe_base)
axe_commit = axe_repo.commit(axe_commit_id)

# Get message file
blob = axe_commit.tree / AXE_CORE['msg_ja_file']
file_content = blob.data_stream.read().decode('utf-8')
messages_ja = json.loads(file_content)
rule_files = ls_dir(axe_rules_dir, '.json')
for rule_file in rule_files:
try:
file_content = read_file_content(rule_file)
except Exception as e:
handle_file_error(e, rule_file)

# Get rule files
tree = axe_commit.tree / AXE_CORE['rules_dir']
rule_blobs = [item for item in tree.traverse() if item.type == 'blob' and item.path.endswith('.json')]
for blob in rule_blobs:
file_content = blob.data_stream.read().decode('utf-8')
parsed_data = json.loads(file_content)
AxeRule(parsed_data, messages_ja)
try:
file_content = read_file_content(axe_pkg_file)
except Exception as e:
handle_file_error(e, axe_pkg_file)

# Get the package file
blob = axe_commit.tree / AXE_CORE['pkg_file']
file_content = blob.data_stream.read().decode('utf-8')
parsed_data = json.loads(file_content)
version = parsed_data['version']
AxeRule.version = version
AxeRule.major_version = re.sub(r'(\d+)\.(\d+)\.\d+', r'\1.\2', version)
AxeRule.deque_url = base_url
for item in Repo(os.path.dirname(axe_pkg_file)).iter_commits('develop', max_count=1):
AxeRule.timestamp = time.strftime("%F %T%z", time.localtime(item.authored_date))
AxeRule.version = parsed_data['version']
AxeRule.major_version = re.sub(r'(\d+)\.(\d+)\.\d+', r'\1.\2', parsed_data['version'])
AxeRule.deque_url = AXE_CORE['deque_url']
AxeRule.timestamp = time.strftime("%F %T%z", time.localtime(axe_commit.authored_date))

def ls_dir(dirname, extension=None):
files = []
Expand Down
8 changes: 7 additions & 1 deletion tools/yaml2rst/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,10 @@
}

# for axe rules
DEQUE_URL = 'https://dequeuniversity.com/rules/axe/'
AXE_CORE = {
'submodule_name': 'vendor/axe-core',
'deque_url': 'https://dequeuniversity.com/rules/axe/',
'msg_ja_file': 'locales/ja.json',
'pkg_file': 'package.json',
'rules_dir': 'lib/rules'
}
5 changes: 1 addition & 4 deletions tools/yaml2rst/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ def get_src_path(basedir):
'wcag_sc': os.path.join(json_basedir, 'wcag-sc.json'),
'gl_categories': os.path.join(json_basedir, 'guideline-categories.json'),
'faq_tags': os.path.join(json_basedir, 'faq-tags.json'),
'info': os.path.join(json_basedir, 'info.json'),
'axe_rules': os.path.join(basedir, AXE_SRC_BASE, AXE_RULES_DIR),
'axe_msg_ja': os.path.join(basedir, AXE_SRC_BASE, AXE_LOCALE_DIR, AXE_LOCALE_JA_FILE),
'axe_pkg': os.path.join(basedir, AXE_SRC_BASE, AXE_PKG_FILE)
'info': os.path.join(json_basedir, 'info.json')
}
return src_path

0 comments on commit 67b1398

Please sign in to comment.