scraper/github/queryManager.py
def _readGQL(self, filePath, verbose=False):
        """Read a 'pretty' formatted GraphQL query file into a one-line string.

        Removes line breaks and comments. Condenses white space.

        Args:
            filePath (str): A relative or absolute path to a file containing
                a GraphQL query.
                File may use comments and multi-line formatting.
                .. _GitHub GraphQL Explorer:
                   https://developer.github.com/v4/explorer/
            verbose (Optional[bool]): If False, prints will be suppressed.
                Defaults to False.

        Returns:
            str: A single line GraphQL query.

        """
        if not os.path.isfile(filePath):
            raise RuntimeError("Query file '%s' does not exist." % (filePath))
        lastModified = os.path.getmtime(filePath)
        absPath = os.path.abspath(filePath)
        if absPath == self.__queryPath and lastModified == self.__queryTimestamp:
            _vPrint(verbose, "Using cached query '%s'" % (os.path.basename(self.__queryPath)))
            query_in = self.__query
        else:
            _vPrint(verbose, "Reading '%s' ... " % (filePath), end="", flush=True)
            with open(filePath, "r") as q:
                # Strip all comments and newlines.
                query_in = re.sub(r'#.*(\n|\Z)', '\n', q.read())
                # Condense extra whitespace.
                query_in = re.sub(r'\s+', ' ', query_in)
                # Remove any leading or trailing whitespace.
                query_in = re.sub(r'(\A\s+)|(\s+\Z)', '', query_in)
            _vPrint(verbose, "File read!")
            self.__queryPath = absPath
            self.__queryTimestamp = lastModified
            self.__query = query_in
        return query_in
def _readGQL(self, filePath, verbose=False):
        """Read a 'pretty' formatted GraphQL query file into a one-line string.

        Removes line breaks and comments. Condenses white space.

        Args:
            filePath (str): A relative or absolute path to a file containing
                a GraphQL query.
                File may use comments and multi-line formatting.
                .. _GitHub GraphQL Explorer:
                   https://developer.github.com/v4/explorer/
            verbose (Optional[bool]): If False, prints will be suppressed.
                Defaults to False.

        Returns:
            str: A single line GraphQL query.

        """
        if not os.path.isfile(filePath):
            raise RuntimeError("Query file '%s' does not exist." % (filePath))
        lastModified = os.path.getmtime(filePath)
        absPath = os.path.abspath(filePath)
        if absPath == self.__queryPath and lastModified == self.__queryTimestamp:
            _vPrint(verbose, "Using cached query '%s'" % (os.path.basename(self.__queryPath)))
            query_in = self.__query
        else:
            _vPrint(verbose, "Reading '%s' ... " % (filePath), end="", flush=True)
            with open(filePath, "r") as q:
                # Strip all comments and newlines.
                query_in = re.sub(r'#.*(\n|\Z)', '\n', q.read())
                # Condense extra whitespace.
                query_in = re.sub(r'\s+', ' ', query_in)
                # Remove any leading or trailing whitespace.
                query_in = re.sub(r'(\A\s+)|(\s+\Z)', '', query_in)
            _vPrint(verbose, "File read!")
            self.__queryPath = absPath
            self.__queryTimestamp = lastModified
            self.__query = query_in
        return query_in
Read a 'pretty' formatted GraphQL query file into a one-line string.

        Removes line breaks and comments. Condenses white space.

        Args:
            filePath (str): A relative or absolute path to a file containing
                a GraphQL query.
                File may use comments and multi-line formatting.
                .. _GitHub GraphQL Explorer:
                   https://developer.github.com/v4/explorer/
            verbose (Optional[bool]): If False, prints will be suppressed.
                Defaults to False.

        Returns:
            str: A single line GraphQL query.