
print("Hello, I am turtle linter")


import logging
import os

import daiquiri
import rdflib

daiquiri.setup(level=logging.INFO)

logger = daiquiri.getLogger(__name__)

# This is a slightly cheaty way of tracking which paths (if any) failed --
# we append to this global list, and inspect it at the end of the script!
failures = []


def parse_turtle(path):
    """
    Try to parse the Turtle at a given path.  Raises a ValueError if it fails!
    """
    logger.info("Parsing Turtle at path %s", path)
    graph = rdflib.Graph()
    try:
        graph.parse(path, format='ttl')
    except Exception as exc:
        # Get the name of the exception class
        # e.g. rdflib.plugins.parsers.notation3.BadSyntax
        exc_name = f'{exc.__class__.__module__}.{exc.__class__.__name__}'

        # Then try to log something useful
        logger.error("Error parsing Turtle (%s)", exc_name)
        logger.error(exc)

        failures.append(path)
    else:
        logger.info("Successfully parsed Turtle!")


if __name__ == '__main__':
    for root, _, filenames in os.walk('.'):
        for f in filenames:
            if not f.endswith('.ttl'):
                continue
            path = os.path.join(root, f)

            if 'WIP' in path:
                logger.info("Skipping path %s as WIP", path)
                continue

            parse_turtle(path)

Write a proper Turtle linter
Write a proper Turtle linter

  
- print("Hello, I am turtle linter")
+ import logging
+ import os
  
+ import daiquiri
+ import rdflib
+ 
+ daiquiri.setup(level=logging.INFO)
+ 
+ logger = daiquiri.getLogger(__name__)
+ 
+ # This is a slightly cheaty way of tracking which paths (if any) failed --
+ # we append to this global list, and inspect it at the end of the script!
+ failures = []
+ 
+ 
+ def parse_turtle(path):
+     """
+     Try to parse the Turtle at a given path.  Raises a ValueError if it fails!
+     """
+     logger.info("Parsing Turtle at path %s", path)
+     graph = rdflib.Graph()
+     try:
+         graph.parse(path, format='ttl')
+     except Exception as exc:
+         # Get the name of the exception class
+         # e.g. rdflib.plugins.parsers.notation3.BadSyntax
+         exc_name = f'{exc.__class__.__module__}.{exc.__class__.__name__}'
+ 
+         # Then try to log something useful
+         logger.error("Error parsing Turtle (%s)", exc_name)
+         logger.error(exc)
+ 
+         failures.append(path)
+     else:
+         logger.info("Successfully parsed Turtle!")
+ 
+ 
+ if __name__ == '__main__':
+     for root, _, filenames in os.walk('.'):
+         for f in filenames:
+             if not f.endswith('.ttl'):
+                 continue
+             path = os.path.join(root, f)
+ 
+             if 'WIP' in path:
+                 logger.info("Skipping path %s as WIP", path)
+                 continue
+ 
+             parse_turtle(path)
+ 
Write a proper Turtle linter
## Code Before:

print("Hello, I am turtle linter")

## Instruction:
Write a proper Turtle linter
## Code After:

import logging
import os

import daiquiri
import rdflib

daiquiri.setup(level=logging.INFO)

logger = daiquiri.getLogger(__name__)

# This is a slightly cheaty way of tracking which paths (if any) failed --
# we append to this global list, and inspect it at the end of the script!
failures = []


def parse_turtle(path):
    """
    Try to parse the Turtle at a given path.  Raises a ValueError if it fails!
    """
    logger.info("Parsing Turtle at path %s", path)
    graph = rdflib.Graph()
    try:
        graph.parse(path, format='ttl')
    except Exception as exc:
        # Get the name of the exception class
        # e.g. rdflib.plugins.parsers.notation3.BadSyntax
        exc_name = f'{exc.__class__.__module__}.{exc.__class__.__name__}'

        # Then try to log something useful
        logger.error("Error parsing Turtle (%s)", exc_name)
        logger.error(exc)

        failures.append(path)
    else:
        logger.info("Successfully parsed Turtle!")


if __name__ == '__main__':
    for root, _, filenames in os.walk('.'):
        for f in filenames:
            if not f.endswith('.ttl'):
                continue
            path = os.path.join(root, f)

            if 'WIP' in path:
                logger.info("Skipping path %s as WIP", path)
                continue

            parse_turtle(path)
