Next / Previous / Contents / Shipman's homepage

12. findYears: Locate year directories

This function looks for the year subdirectories under the current working directory.

noteweb
# - - -   f i n d Y e a r s

def findYears ( txny, yearCollection ):
    '''Build the list of years.

      [ let
          year-dirs == (subdirectories of '.' with four-digit
              names in the range 1000-2999)
        in:
          (txny is an xnomo3.Txny instance) and
          (yearCollection is a YearCollection instance) ->
            sys.stderr  +:=  error messages from invalid monthly
                XML files in year-dirs
            yearCollection  :=  yearCollection with years added
                corresponding to year-dirs ]
    '''

We use os.listdir() to get a list of all the names in the current working directory, filtering those names with the YEAR_PAT regular expression to find the ones that look like year names; see Section 10.3, “YEAR_PAT: Pattern for year numbers”. Each year directory name is then passed to the YearRow constructor, so that yyyyList is now a list of YearRow instances; see Section 22, “class YearRow: Container for one year's records”.

noteweb
    #-- 1 --
    # [ yyyyList  :=  subdirectories of '.' that match YEAR_PAT ]
    yyyyList  =  [ dirName
                   for dirName in os.listdir('.')
                   if YEAR_PAT.match(dirName) is not None ]

Next we'll sort the list in chronological order.

noteweb
    #-- 2 --
    # [ yyyyList  :=  yyyyList sorted in ascending order ]
    yyyyList.sort()

See Section 21.2, “YearCollection.addYear(): Add a year” and, for the logic that searches a year directory for monthly XML files, see Section 22.8, “YearRow.readAllMonths(): Process input files for one year”.

noteweb
    #-- 3 --
    # [ yearCollection  :=  yearCollection with valid monthly
    #       data added from XML files in its members' subdirectories
    #   sys.stderr  +:=  error message(s) about invalid monthly
    #       XML files in that set, if any ]
    for yyyy in yyyyList:
        #-- 3.1 --
        # [ yearCollection  :=  yearCollection with a new YearRow
        #       added for year=yyyy
        #   yearRow  :=  that new YearRow instance ]
        yearRow  =  yearCollection.addYear ( txny, yyyy )

        #-- 3.2 --
        # [ yearRow  :=  yearRow with MonthCell instances added
        #       for XML notes files in directory yearRow.yyyy
        #       valid against birdnotes.rnc and txny
        #   sys.stderr  +:=  error message(s) for invalid XML
        #       notes files in that directory, if any ]
        yearRow.readAllMonths()