Skip to content

Commit

Permalink
Update black format in css_check.py
Browse files Browse the repository at this point in the history
  • Loading branch information
IITI-tushar authored Jan 15, 2025
1 parent b99ba8e commit 7d3128e
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions .github/workflows/css_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
from collections import namedtuple

# Define namedtuples for storing results
Violation = namedtuple('Violation', ['file_path', 'css_file', 'reason'])
CorrectImport = namedtuple('CorrectImport', ['file_path', 'css_file'])
EmbeddedViolation = namedtuple('EmbeddedViolation', ['file_path', 'css_codes'])
CSSCheckResult = namedtuple('CSSCheckResult', ['violations', 'correct_imports', 'embedded_violations'])
Violation = namedtuple("Violation", ["file_path", "css_file", "reason"])
CorrectImport = namedtuple("CorrectImport", ["file_path", "css_file"])
EmbeddedViolation = namedtuple("EmbeddedViolation", ["file_path", "css_codes"])
CSSCheckResult = namedtuple(
"CSSCheckResult", ["violations", "correct_imports", "embedded_violations"]
)


def check_embedded_css(content: str) -> list:
"""
Expand All @@ -26,8 +29,12 @@ def check_embedded_css(content: str) -> list:
embedded_css_pattern = r"#([0-9a-fA-F]{3}){1,2}" # Matches CSS color codes
return re.findall(embedded_css_pattern, content)


def check_files(
directory: str, exclude_files: list, exclude_directories: list, allowed_css_patterns: list
directory: str,
exclude_files: list,
exclude_directories: list,
allowed_css_patterns: list,
) -> CSSCheckResult:
"""
Check TypeScript files for CSS violations and correct CSS imports.
Expand Down Expand Up @@ -83,20 +90,29 @@ def check_files(

# Check if the CSS file exists
if not os.path.exists(css_file_path):
violations.append(Violation(file_path, css_file, "File not found"))
violations.append(
Violation(file_path, css_file, "File not found")
)
# Check if the CSS import matches the allowed patterns
elif any(css_file.endswith(pattern) for pattern in allowed_css_patterns):
elif any(
css_file.endswith(pattern) for pattern in allowed_css_patterns
):
correct_css_imports.append(CorrectImport(file_path, css_file))
else:
violations.append(Violation(file_path, css_file, "Invalid import"))
violations.append(
Violation(file_path, css_file, "Invalid import")
)

# Check for embedded CSS
embedded_css = check_embedded_css(content)
if embedded_css:
embedded_css_violations.append(EmbeddedViolation(file_path, embedded_css))
embedded_css_violations.append(
EmbeddedViolation(file_path, embedded_css)
)

return CSSCheckResult(violations, correct_css_imports, embedded_css_violations)


def main():
"""Run the CSS check script."""
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -140,31 +156,35 @@ def main():
if result.violations:
output.append("CSS Import Violations:")
for violation in result.violations:
output.append(f"- {violation.file_path}: {violation.css_file} ({violation.reason})")
output.append(
f"- {violation.file_path}: {violation.css_file} ({violation.reason})"
)
exit_code = 1

if result.embedded_violations:
output.append("\nEmbedded CSS Violations:")
for violation in result.embedded_violations:
output.append(f"- {violation.file_path}: {', '.join(violation.css_codes)}")
exit_code = 1
exit_code = 1

if output:
print("\n".join(output))
print("""
print(
"""
Please address the above CSS violations:
1. For invalid CSS imports, ensure you're using the correct import syntax and file paths.
2. For embedded CSS, move the CSS to appropriate stylesheet files and import them correctly.
3. Make sure to use only the allowed CSS patterns as specified in the script arguments.
4. Check that all imported CSS files exist in the specified locations.
""")
"""
)
if args.show_success and result.correct_imports:
print("\nCorrect CSS Imports:")
for import_ in result.correct_imports:
print(f"- {import_.file_path}: {import_.css_file}")

sys.exit(exit_code)


if __name__ == "__main__":
main()

0 comments on commit 7d3128e

Please sign in to comment.