Source code for ApplicationVersioningSupport.VersionHistoryParser
# Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
# **InsertLicense** code
#----------------------------------------------------------------------------------
#! VersionHistoryParser: Helps to read and parse version history strings
#/*!
# \file VersionHistoryParser.py
# \author Jan-Martin Kuhnigk
# \date 2014-08
#*/
#----------------------------------------------------------------------------------
# associated_tests: ApplicationVersioningSupport.tests.VersionHistoryParserTest
from .VersionInfo import VersionInfo
from .VersionHistory import VersionHistory
class VersionInfoCurrentVersionNotFoundException( Exception ): pass
class VersionInfoCurrentVersionKeyNotFoundException( VersionInfoCurrentVersionNotFoundException ): pass
class VersionInfoCurrentVersionEmptyException( VersionInfoCurrentVersionNotFoundException ): pass
maxDebugStringLength = 250 # amount of characters of the string printed in case of error.
class ParsingParameters( object ):
""" Container for parsing parameters.
"""
def __init__( self ):
self.currentVersionKey = "Current Version:"
[docs]class GetVersionHistoryFromString( object ):
"""
Can parse a version history string and generate a VersionHistory object from it. Looks like
a class but can be used like a function.
:param inputString: version history string to parse
:param parsingParameters: define how to parse the file, see ParsingParameters documentation
:raises: VersionInfoCurrentVersionNotFoundException if the current version could not be found in the string
:return: The VersionHistory object read
"""
def __new__( cls, *args, **kwargs ):
self = object.__new__( cls )
self.__init__( *args, **kwargs )
self._readVersionHistoryFromInputString( )
return self._versionHistory
def __init__( self, inputString, parsingParameters = None ):
self._inputString = inputString
self._parsingParameters = parsingParameters if parsingParameters is not None else ParsingParameters( )
def _readVersionHistoryFromInputString( self ):
self._versionHistory = VersionHistory( )
self._versionHistory.setCurrentVersionInfo( self._getCurrentVersionFromInputString( ) )
def _getCurrentVersionFromInputString( self ):
currentVersionInfoLine = self._getCurrentVersionInfoLine( )
return self._getVersionInfoFromVersionInfoString( currentVersionInfoLine )
def _getCurrentVersionInfoLine( self ):
def __getExceptionMessage( baseMessage ):
debugStringLength = min( len( self._inputString ), maxDebugStringLength )
postfix = "" if len( self._inputString ) <= maxDebugStringLength else "[...]"
return "{} '{} {}'".format( baseMessage, self._inputString[ :debugStringLength ], postfix )
def __verifyNonNullVersionInfoLine( ):
if versionInfoLine is None:
exceptionMessage = __getExceptionMessage( "Current version indicator string '{}' not found in string".format(
self._parsingParameters.currentVersionKey ) )
raise VersionInfoCurrentVersionKeyNotFoundException( exceptionMessage )
def __verifyNonEmptyVersionString( ):
if len( versionInfoLine ) == 0:
exceptionMessage = __getExceptionMessage(
"Current version after key '{}' is empty in string".format( self._parsingParameters.currentVersionKey ) )
raise VersionInfoCurrentVersionEmptyException( exceptionMessage )
versionInfoLine = self._getLineAfterKey( self._parsingParameters.currentVersionKey )
__verifyNonNullVersionInfoLine( )
versionInfoLine = self._GetNormalizedVersionString( versionInfoLine )
__verifyNonEmptyVersionString( )
return versionInfoLine
def _getVersionInfoFromVersionInfoString( self, normalizedVersionInfoString ):
majorVersionIndexEnd = normalizedVersionInfoString.find( " " )
if majorVersionIndexEnd >= 0:
majorVersion = self._GetNormalizedVersionString( normalizedVersionInfoString[ 0: majorVersionIndexEnd ] )
minorVersion = self._GetNormalizedVersionString( normalizedVersionInfoString[ majorVersionIndexEnd + 1: ] )
else:
majorVersion = normalizedVersionInfoString
minorVersion = ""
return VersionInfo( majorVersion, minorVersion )
def _getLineAfterKey( self, key ):
keyIndexStart = self._inputString.find( key )
if keyIndexStart >= 0:
lineAfterKeyStart = keyIndexStart + len( key )
lineAfterKeyEnd = self._inputString.find( "\n", keyIndexStart )
if lineAfterKeyStart <= lineAfterKeyEnd:
return self._inputString[ lineAfterKeyStart: lineAfterKeyEnd ]
return None
@classmethod
def _GetNormalizedVersionString( cls, versionInfoLine ):
return versionInfoLine.strip( )