def _add_directive(block, directive, replace):
 directive = nginxparser.UnspacedList(directive)
 if ((len(directive) == 0) or (directive[0] == '#')):
  block.append(directive)
  return
 location = next((index for (index, line) in enumerate(block) if (line and (line[0] == directive[0]))), None)
 if replace:
  if (location is None):
   raise errors.MisconfigurationError('expected directive for {0} in the Nginx config but did not find it.'.format(directive[0]))
  block[location] = directive
  _comment_directive(block, location)
 else:
  directive_name = directive[0]
  directive_value = directive[1]
  if ((location is None) or (isinstance(directive_name, str) and (directive_name in REPEATABLE_DIRECTIVES))):
   block.append(directive)
   _comment_directive(block, (len(block) - 1))
  elif (block[location][1] != directive_value):
   raise errors.MisconfigurationError('tried to insert directive "{0}" but found conflicting "{1}".'.format(directive, block[location]))

def _add_directive ( block , directive , replace ) : directive = nginxparser . UnspacedList ( directive ) if ( ( len ( directive ) == 0 ) or ( directive [ 0 ] == '#' ) ) : block . append ( directive ) return location = next ( ( index for ( index , line ) in enumerate ( block ) if ( line and ( line [ 0 ] == directive [ 0 ] ) ) ) , None ) if replace : if ( location is None ) : raise errors . MisconfigurationError ( 'expected directive for {0} in the Nginx config but did not find it.' . format ( directive [ 0 ] ) ) block [ location ] = directive _comment_directive ( block , location ) else : directive_name = directive [ 0 ] directive_value = directive [ 1 ] if ( ( location is None ) or ( isinstance ( directive_name , str ) and ( directive_name in REPEATABLE_DIRECTIVES ) ) ) : block . append ( directive ) _comment_directive ( block , ( len ( block ) - 1 ) ) elif ( block [ location ] [ 1 ] != directive_value ) : raise errors . MisconfigurationError ( 'tried to insert directive "{0}" but found conflicting "{1}".' . format ( directive , block [ location ] ) )
adds or replaces a single directive in a config block .