示例#1
0
    def preload(self):
        """
        Tries to determine USX filename pattern.
        """
        if BibleOrgSysGlobals.debugFlag or BibleOrgSysGlobals.verbosityLevel > 2:
            print("USXXMLBible preload() from {}".format(self.sourceFolder))

        # Do a preliminary check on the readability of our folder
        if not os.access(self.givenFolderName, os.R_OK):
            logging.error("USXXMLBible: File {!r} is unreadable".format(
                self.givenFolderName))

        # Find the filenames of all our books
        self.USXFilenamesObject = USXFilenames(self.givenFolderName)
        #print( "DDFSDF", self.USXFilenamesObject )
        #print( "DFSFGE", self.USXFilenamesObject.getPossibleFilenameTuples() )
        #print( "SDFSDQ", self.USXFilenamesObject.getConfirmedFilenameTuples() )
        self.possibleFilenameDict = OrderedDict()
        filenameTuples = self.USXFilenamesObject.getConfirmedFilenameTuples()
        if not filenameTuples:  # Try again
            filenameTuples = self.USXFilenamesObject.getPossibleFilenameTuples(
            )
        for BBB, filename in filenameTuples:
            self.availableBBBs.add(BBB)
            self.possibleFilenameDict[BBB] = filename
        #print( "GHJGHR", self.possibleFilenameDict ); halt

        self.preloadDone = True
示例#2
0
    def __init__(self, givenFolderName, givenName=None, encoding='utf-8'):
        """
        Create the internal USX Bible object.
        """
        # Setup and initialise the base class first
        Bible.__init__(self)
        self.objectNameString = "USX XML Bible object"
        self.objectTypeString = "USX"

        self.givenFolderName, self.givenName, self.encoding = givenFolderName, givenName, encoding  # Remember our parameters

        # Now we can set our object variables
        self.name = self.givenName
        if not self.name: self.name = os.path.basename(self.givenFolderName)
        if not self.name:
            self.name = os.path.basename(
                self.givenFolderName[:-1])  # Remove the final slash
        if not self.name: self.name = "USX Bible"

        # Do a preliminary check on the readability of our folder
        if not os.access(self.givenFolderName, os.R_OK):
            logging.error("USXXMLBible: File {!r} is unreadable".format(
                self.givenFolderName))

        # Find the filenames of all our books
        self.USXFilenamesObject = USXFilenames(self.givenFolderName)
        self.possibleFilenameDict = {}
        for BBB, filename in self.USXFilenamesObject.getConfirmedFilenames():
            self.possibleFilenameDict[BBB] = filename
示例#3
0
def USXXMLBibleFileCheck(givenFolderName,
                         strictCheck=True,
                         autoLoad=False,
                         autoLoadBooks=False):
    """
    Given a folder, search for USX Bible files or folders in the folder and in the next level down.

    Returns False if an error is found.

    if autoLoad is false (default)
        returns None, or the number of Bibles found.

    if autoLoad is true and exactly one USX Bible is found,
        returns the loaded USXXMLBible object.
    """
    if BibleOrgSysGlobals.verbosityLevel > 2:
        print("USXXMLBibleFileCheck( {}, {}, {}, {} )".format(
            givenFolderName, strictCheck, autoLoad, autoLoadBooks))
    if BibleOrgSysGlobals.debugFlag:
        assert givenFolderName and isinstance(givenFolderName, str)
    if BibleOrgSysGlobals.debugFlag: assert autoLoad in (
            True,
            False,
    )

    # Check that the given folder is readable
    if not os.access(givenFolderName, os.R_OK):
        logging.critical(
            _("USXXMLBibleFileCheck: Given {!r} folder is unreadable").format(
                givenFolderName))
        return False
    if not os.path.isdir(givenFolderName):
        logging.critical(
            _("USXXMLBibleFileCheck: Given {!r} path is not a folder").format(
                givenFolderName))
        return False

    # Find all the files and folders in this folder
    if BibleOrgSysGlobals.verbosityLevel > 3:
        print(" USXXMLBibleFileCheck: Looking for files in given {}".format(
            givenFolderName))
    foundFolders, foundFiles = [], []
    for something in os.listdir(givenFolderName):
        somepath = os.path.join(givenFolderName, something)
        if os.path.isdir(somepath):
            if something in BibleOrgSysGlobals.COMMONLY_IGNORED_FOLDERS:
                continue  # don't visit these directories
            foundFolders.append(something)
        elif os.path.isfile(somepath):
            foundFiles.append(something)

    # See if there's an USXBible project here in this given folder
    numFound = 0
    UFns = USXFilenames(
        givenFolderName
    )  # Assuming they have standard Paratext style filenames
    if BibleOrgSysGlobals.verbosityLevel > 2: print(UFns)
    #filenameTuples = UFns.getPossibleFilenameTuples( strictCheck=True )
    #print( 'P', len(filenameTuples) )
    filenameTuples = UFns.getConfirmedFilenameTuples(strictCheck=True)
    #print( 'C', len(filenameTuples) )
    if BibleOrgSysGlobals.verbosityLevel > 3:
        print("Confirmed:", len(filenameTuples), filenameTuples)
    if BibleOrgSysGlobals.verbosityLevel > 2 and filenameTuples:
        print("  Found {} USX file{}.".format(
            len(filenameTuples), '' if len(filenameTuples) == 1 else 's'))
    if filenameTuples:
        numFound += 1
    if numFound:
        if BibleOrgSysGlobals.verbosityLevel > 2:
            print("USXXMLBibleFileCheck got", numFound, givenFolderName)
        if numFound == 1 and (autoLoad or autoLoadBooks):
            uB = USXXMLBible(givenFolderName)
            if autoLoad or autoLoadBooks:
                uB.preload()  # Determine the filenames
            if autoLoadBooks: uB.loadBooks()  # Load and process the book files
            return uB
        return numFound

    # Look one level down
    numFound = 0
    foundProjects = []
    for thisFolderName in sorted(foundFolders):
        tryFolderName = os.path.join(givenFolderName, thisFolderName + '/')
        if not os.access(tryFolderName,
                         os.R_OK):  # The subfolder is not readable
            logging.warning(
                _("USXXMLBibleFileCheck: {!r} subfolder is unreadable").format(
                    tryFolderName))
            continue
        if BibleOrgSysGlobals.verbosityLevel > 3:
            print("    USXXMLBibleFileCheck: Looking for files in {}".format(
                tryFolderName))
        foundSubfolders, foundSubfiles = [], []
        for something in os.listdir(tryFolderName):
            somepath = os.path.join(givenFolderName, thisFolderName, something)
            if os.path.isdir(somepath): foundSubfolders.append(something)
            elif os.path.isfile(somepath): foundSubfiles.append(something)

        # See if there's an USX Bible with standard Paratext style filenames here in this folder
        UFns = USXFilenames(
            tryFolderName
        )  # Assuming they have standard Paratext style filenames
        if BibleOrgSysGlobals.verbosityLevel > 2: print(UFns)
        #filenameTuples = UFns.getPossibleFilenameTuples()
        filenameTuples = UFns.getConfirmedFilenameTuples(strictCheck=True)
        if BibleOrgSysGlobals.verbosityLevel > 3:
            print("Confirmed:", len(filenameTuples), filenameTuples)
        if BibleOrgSysGlobals.verbosityLevel > 2 and filenameTuples:
            print("  Found {} USX files: {}".format(len(filenameTuples),
                                                    filenameTuples))
        elif BibleOrgSysGlobals.verbosityLevel > 1 and filenameTuples and debuggingThisModule:
            print("  Found {} USX file{}".format(
                len(filenameTuples), '' if len(filenameTuples) == 1 else 's'))
        if filenameTuples:
            foundProjects.append(tryFolderName)
            numFound += 1
    if numFound:
        if BibleOrgSysGlobals.verbosityLevel > 2:
            print("USXXMLBibleFileCheck foundProjects", numFound,
                  foundProjects)
        if numFound == 1 and (autoLoad or autoLoadBooks):
            uB = USXXMLBible(foundProjects[0])
            if autoLoad or autoLoadBooks:
                uB.preload()  # Determine the filenames
            if autoLoadBooks: uB.loadBooks()  # Load and process the book files
            return uB
        return numFound