示例#1
0
def checkPreConditions(nextPeriod, driversFileName, vehiclesFileName,
                       servicesFileName, reservationsFileName):
    """Checks the preconditions.

    Requires:
    The same as update (ommitted here to avoid redudancy)
    Ensures:
    returns bool value False if some of the conditions are not met
    and True otherwise
    """

    headerDrivers = getHeader(driversFileName)
    headerVehicles = getHeader(vehiclesFileName)
    headerServices = getHeader(servicesFileName)
    headerReservations = getHeader(reservationsFileName)

    previousPeriod = getPreviousPeriod(nextPeriod)

    # Changes the format of the period to the one in the header of files
    nextPeriodOther = changeFormatTime(nextPeriod)
    previousPeriodOther = changeFormatTime(previousPeriod)

    # NextPeriod is a str from the set 0911, 1113, ..., 1921
    if nextPeriod not in ['0911', '1113', '1315', '1517', '1719', '1921']:
        return False

    # The files whose names are driversFileName, vehiclesFileName, servicesFileName and reservationsFileName
    # concern the same company and the same day;
    elif not (headerDrivers[INDEXCompany:INDEXDate + 1] == headerVehicles[INDEXCompany:INDEXDate + 1] ==
              headerServices[INDEXCompany:INDEXDate + 1] == headerReservations[INDEXCompany:INDEXDate + 1]):
        return False

    # The file whose name is reservationsFileName concerns the period indicated by nextPeriod
    elif headerReservations[INDEXPeriod].strip() != nextPeriodOther:
        return False

    # The files whose names are driversFileName, vehiclesFileName, servicesFileName concern the period
    # immediately preceding the period indicated by nextPeriod;
    elif not (headerDrivers[INDEXPeriod].strip() == headerVehicles[INDEXPeriod].strip() ==
              headerServices[INDEXPeriod].strip() == previousPeriodOther):
        return False

    # The file name reservationsFileName ends (before the .txt extension) with
    # the string nextPeriod;
    elif reservationsFileName[-8:-4] != nextPeriod:
        return False

    # The file names driversFileName, vehiclesFileName and servicesFileName
    # end (before their .txt extension) with the string representing
    # the period immediately preceding the one indicated by nextPeriod,
    # from the set 0709, 0911, ..., 1719;
    elif not (driversFileName[-8:-4] == vehiclesFileName[-8:-4] == servicesFileName[-8:-4] == previousPeriod):
        return False

    else:
        return True
def createNewHeader(fileName, new_period):
    """Creates a new header, with the new period.

    Requires:
    fileName is string with name of the file from which we want to get the header.
    This file is organized as in the examples provided in the general
    specification (omitted here for the sake of readability)
    new_period is a string corresponding to the next period,
    in the format HHHH (see general specifications).
    Ensures:
    a string with the header of the file named fileName,
    but in which the line corresponding to the period is
    substitued for the period in new_period.
    """

    # Changes the new_period format to the one used in the files
    new_period = changeFormatTime(new_period)

    header = getHeader(fileName)

    header[INDEXPeriod] = new_period

    # Turns header into string, each line separated by commas. To understand the
    # use of commas, see outputStatus.writeServicesFile
    header = ",".join(header)

    # Deletes newlines
    header = header.replace("\n", "")

    return header