|
| 1 | +package pylint |
| 2 | + |
| 3 | +import ( |
| 4 | + "codacy/cli-v2/domain" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// getDefaultParametersForPatterns returns a map of pattern IDs to their default parameters |
| 11 | +func getDefaultParametersForPatterns(patternIDs []string) map[string][]domain.ParameterConfiguration { |
| 12 | + defaultParams := make(map[string][]domain.ParameterConfiguration) |
| 13 | + |
| 14 | + for _, patternID := range patternIDs { |
| 15 | + if params, exists := PatternDefaultParameters[patternID]; exists { |
| 16 | + defaultParams[patternID] = params |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + return defaultParams |
| 21 | +} |
| 22 | + |
| 23 | +// writePylintRCHeader writes the common header sections to the RC content |
| 24 | +func writePylintRCHeader(rcContent *strings.Builder) { |
| 25 | + rcContent.WriteString("[MASTER]\n") |
| 26 | + rcContent.WriteString("ignore=CVS\n") |
| 27 | + rcContent.WriteString("persistent=yes\n") |
| 28 | + rcContent.WriteString("load-plugins=\n\n") |
| 29 | + rcContent.WriteString("[MESSAGES CONTROL]\n") |
| 30 | + rcContent.WriteString("disable=all\n") |
| 31 | +} |
| 32 | + |
| 33 | +// writeEnabledPatterns writes the enabled patterns section to the RC content |
| 34 | +func writeEnabledPatterns(rcContent *strings.Builder, patternIDs []string) { |
| 35 | + if len(patternIDs) > 0 { |
| 36 | + rcContent.WriteString(fmt.Sprintf("enable=%s\n", strings.Join(patternIDs, ","))) |
| 37 | + } |
| 38 | + rcContent.WriteString("\n") |
| 39 | +} |
| 40 | + |
| 41 | +// writeParametersBySection writes the parameters grouped by section to the RC content |
| 42 | +func writeParametersBySection(rcContent *strings.Builder, groupedParams map[string][]domain.ParameterConfiguration) { |
| 43 | + for sectionName, params := range groupedParams { |
| 44 | + rcContent.WriteString(fmt.Sprintf("[%s]\n", sectionName)) |
| 45 | + for _, param := range params { |
| 46 | + rcContent.WriteString(fmt.Sprintf("%s=%s\n", param.Name, param.Value)) |
| 47 | + } |
| 48 | + rcContent.WriteString("\n") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// groupParametersByPatterns groups parameters from patterns into sections |
| 53 | +func groupParametersByPatterns(patterns []domain.PatternConfiguration) map[string][]domain.ParameterConfiguration { |
| 54 | + groupedParams := make(map[string][]domain.ParameterConfiguration) |
| 55 | + |
| 56 | + for _, pattern := range patterns { |
| 57 | + patternID := extractPatternId(pattern.PatternDefinition.Id) |
| 58 | + params := pattern.Parameters |
| 59 | + |
| 60 | + // If no parameters, check defaults |
| 61 | + if len(params) == 0 { |
| 62 | + if defaultParams, exists := PatternDefaultParameters[patternID]; exists { |
| 63 | + params = defaultParams |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Add parameters to their respective sections |
| 68 | + for _, param := range params { |
| 69 | + sectionName := GetParameterSection(param.Name) |
| 70 | + if sectionName == nil { |
| 71 | + log.Printf("Parameter %s has no section name", param.Name) |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + // Check if parameter already exists in section |
| 76 | + exists := false |
| 77 | + for _, existingParam := range groupedParams[*sectionName] { |
| 78 | + if existingParam.Name == param.Name { |
| 79 | + exists = true |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Only add if not already present |
| 85 | + if !exists { |
| 86 | + groupedParams[*sectionName] = append(groupedParams[*sectionName], param) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return groupedParams |
| 92 | +} |
| 93 | + |
| 94 | +func GeneratePylintRCDefault() string { |
| 95 | + var rcContent strings.Builder |
| 96 | + |
| 97 | + writePylintRCHeader(&rcContent) |
| 98 | + writeEnabledPatterns(&rcContent, DefaultPatterns) |
| 99 | + |
| 100 | + // Get default parameters for enabled patterns |
| 101 | + defaultParams := getDefaultParametersForPatterns(DefaultPatterns) |
| 102 | + |
| 103 | + // Convert default parameters to pattern configurations |
| 104 | + var patterns []domain.PatternConfiguration |
| 105 | + for patternID, params := range defaultParams { |
| 106 | + patterns = append(patterns, domain.PatternConfiguration{ |
| 107 | + PatternDefinition: domain.PatternDefinition{ |
| 108 | + Id: "PyLintPython3_" + patternID, |
| 109 | + }, |
| 110 | + Parameters: params, |
| 111 | + }) |
| 112 | + } |
| 113 | + |
| 114 | + // Group and write parameters |
| 115 | + groupedParams := groupParametersByPatterns(patterns) |
| 116 | + writeParametersBySection(&rcContent, groupedParams) |
| 117 | + |
| 118 | + return rcContent.String() |
| 119 | +} |
| 120 | + |
| 121 | +// GeneratePylintRC generates a pylintrc file content with the specified patterns enabled |
| 122 | +func GeneratePylintRC(config []domain.PatternConfiguration) string { |
| 123 | + var rcContent strings.Builder |
| 124 | + |
| 125 | + writePylintRCHeader(&rcContent) |
| 126 | + |
| 127 | + // Collect enabled pattern IDs |
| 128 | + var enabledPatternsIds []string |
| 129 | + |
| 130 | + for _, pattern := range config { |
| 131 | + patternID := extractPatternId(pattern.PatternDefinition.Id) |
| 132 | + enabledPatternsIds = append(enabledPatternsIds, patternID) |
| 133 | + } |
| 134 | + |
| 135 | + writeEnabledPatterns(&rcContent, enabledPatternsIds) |
| 136 | + |
| 137 | + // Group and write parameters |
| 138 | + groupedParams := groupParametersByPatterns(config) |
| 139 | + writeParametersBySection(&rcContent, groupedParams) |
| 140 | + |
| 141 | + return rcContent.String() |
| 142 | +} |
| 143 | + |
| 144 | +// extractPatternId returns the part of the pattern ID after the underscore |
| 145 | +// For example: "PyLintPython3_C0301" -> "C0301" |
| 146 | +func extractPatternId(fullID string) string { |
| 147 | + parts := strings.Split(fullID, "_") |
| 148 | + if len(parts) > 1 { |
| 149 | + return parts[1] |
| 150 | + } |
| 151 | + return fullID |
| 152 | +} |
0 commit comments