|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "codacy/cli-v2/tools/types" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// ParsePylintPatternsFromJSON parses a JSON array of Pylint patterns into types.PylintPatternConfiguration array |
| 11 | +func ParsePylintPatternsFromJSON(jsonData []byte) ([]types.PylintPatternConfiguration, error) { |
| 12 | + var response struct { |
| 13 | + Data []struct { |
| 14 | + PatternDefinition struct { |
| 15 | + ID string `json:"id"` |
| 16 | + Parameters []struct { |
| 17 | + Name string `json:"name"` |
| 18 | + Default string `json:"default"` |
| 19 | + } `json:"parameters"` |
| 20 | + } `json:"patternDefinition"` |
| 21 | + Enabled bool `json:"enabled"` |
| 22 | + Parameters []struct { |
| 23 | + Name string `json:"name"` |
| 24 | + Value string `json:"value"` |
| 25 | + } `json:"parameters"` |
| 26 | + } `json:"data"` |
| 27 | + } |
| 28 | + |
| 29 | + if err := json.Unmarshal(jsonData, &response); err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + patterns := make([]types.PylintPatternConfiguration, len(response.Data)) |
| 34 | + for i, pattern := range response.Data { |
| 35 | + defaultParameters := make([]types.PylintPatternParameterConfiguration, len(pattern.PatternDefinition.Parameters)) |
| 36 | + for j, param := range pattern.PatternDefinition.Parameters { |
| 37 | + defaultParameters[j] = types.PylintPatternParameterConfiguration{ |
| 38 | + Name: param.Name, |
| 39 | + Value: param.Default, |
| 40 | + SectionName: GetParameterSection(param.Name), |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + parameters := make([]types.PylintPatternParameterConfiguration, len(pattern.Parameters)) |
| 45 | + for j, param := range pattern.Parameters { |
| 46 | + parameters[j] = types.PylintPatternParameterConfiguration{ |
| 47 | + Name: param.Name, |
| 48 | + Value: param.Value, |
| 49 | + SectionName: GetParameterSection(param.Name), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + patterns[i] = types.PylintPatternConfiguration{ |
| 54 | + Id: ExtractPatternID(pattern.PatternDefinition.ID), |
| 55 | + Enabled: pattern.Enabled, |
| 56 | + Parameters: setConfigurationParameters(parameters, defaultParameters), |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return patterns, nil |
| 61 | +} |
| 62 | + |
| 63 | +// FilterEnabledPatterns returns only the enabled patterns from the given slice |
| 64 | +func FilterEnabledPatterns(patterns []types.PylintPatternConfiguration) []types.PylintPatternConfiguration { |
| 65 | + enabledPatterns := make([]types.PylintPatternConfiguration, 0) |
| 66 | + for _, pattern := range patterns { |
| 67 | + if pattern.Enabled { |
| 68 | + enabledPatterns = append(enabledPatterns, pattern) |
| 69 | + } |
| 70 | + } |
| 71 | + return enabledPatterns |
| 72 | +} |
| 73 | + |
| 74 | +// DefaultPylintParameters contains the default values for common Pylint parameters |
| 75 | +var DefaultPylintParameters = map[string]string{ |
| 76 | + "max-line-length": "100", |
| 77 | + "max-doc-length": "100", |
| 78 | + "max-args": "5", |
| 79 | + "max-attributes": "7", |
| 80 | + "max-branches": "12", |
| 81 | + "max-locals": "15", |
| 82 | + "max-parents": "7", |
| 83 | + "max-public-methods": "20", |
| 84 | + "max-returns": "6", |
| 85 | + "max-statements": "50", |
| 86 | +} |
| 87 | + |
| 88 | +// ExtractPatternID returns the part of the pattern ID after the underscore |
| 89 | +// For example: "PyLintPython3_C0301" -> "C0301" |
| 90 | +func ExtractPatternID(fullID string) string { |
| 91 | + parts := strings.Split(fullID, "_") |
| 92 | + if len(parts) > 1 { |
| 93 | + return parts[1] |
| 94 | + } |
| 95 | + return fullID |
| 96 | +} |
| 97 | + |
| 98 | +// GeneratePylintRC generates a pylintrc file content with the specified patterns enabled |
| 99 | +func GeneratePylintRC(patterns []types.PylintPatternConfiguration) string { |
| 100 | + var rcContent strings.Builder |
| 101 | + |
| 102 | + // Write header |
| 103 | + rcContent.WriteString("[MASTER]\n") |
| 104 | + rcContent.WriteString("ignore=CVS\n") |
| 105 | + rcContent.WriteString("persistent=yes\n") |
| 106 | + rcContent.WriteString("load-plugins=\n\n") |
| 107 | + |
| 108 | + // Disable all patterns by default |
| 109 | + rcContent.WriteString("[MESSAGES CONTROL]\n") |
| 110 | + rcContent.WriteString("disable=all\n") |
| 111 | + |
| 112 | + // Collect all enabled pattern IDs |
| 113 | + var enabledPatterns []string |
| 114 | + for _, pattern := range patterns { |
| 115 | + if pattern.Enabled { |
| 116 | + enabledPatterns = append(enabledPatterns, ExtractPatternID(pattern.Id)) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Write all enabled patterns in a single line |
| 121 | + if len(enabledPatterns) > 0 { |
| 122 | + rcContent.WriteString(fmt.Sprintf("enable=%s\n", strings.Join(enabledPatterns, ","))) |
| 123 | + } |
| 124 | + rcContent.WriteString("\n") |
| 125 | + |
| 126 | + // Group parameters by section |
| 127 | + groupedParams := GroupParametersBySection(patterns) |
| 128 | + |
| 129 | + // Write parameters for each section |
| 130 | + for sectionName, params := range groupedParams { |
| 131 | + rcContent.WriteString(fmt.Sprintf("[%s]\n", sectionName)) |
| 132 | + for _, param := range params { |
| 133 | + value := param.Value |
| 134 | + if value == "" { |
| 135 | + // If no value is set, use default from DefaultPylintParameters |
| 136 | + if defaultVal, ok := DefaultPylintParameters[param.Name]; ok { |
| 137 | + value = defaultVal |
| 138 | + } |
| 139 | + } |
| 140 | + rcContent.WriteString(fmt.Sprintf("%s=%s\n", param.Name, value)) |
| 141 | + } |
| 142 | + rcContent.WriteString("\n") |
| 143 | + } |
| 144 | + |
| 145 | + return rcContent.String() |
| 146 | +} |
| 147 | + |
| 148 | +// setConfigurationParameters returns the first array if it's not empty, otherwise returns the second array |
| 149 | +func setConfigurationParameters(parameters, defaultParameters []types.PylintPatternParameterConfiguration) []types.PylintPatternParameterConfiguration { |
| 150 | + if len(parameters) > 0 { |
| 151 | + return parameters |
| 152 | + } |
| 153 | + return defaultParameters |
| 154 | +} |
| 155 | + |
| 156 | +// groupParametersBySection groups parameters by their section name |
| 157 | +func GroupParametersBySection(patterns []types.PylintPatternConfiguration) map[string][]types.PylintPatternParameterConfiguration { |
| 158 | + // Initialize the result map |
| 159 | + groupedParams := make(map[string][]types.PylintPatternParameterConfiguration) |
| 160 | + |
| 161 | + // Iterate through each pattern |
| 162 | + for _, pattern := range patterns { |
| 163 | + // Iterate through each parameter |
| 164 | + for _, param := range pattern.Parameters { |
| 165 | + // Skip parameters without a section name |
| 166 | + if param.SectionName == nil { |
| 167 | + continue |
| 168 | + } |
| 169 | + |
| 170 | + // Get the section name |
| 171 | + sectionName := *param.SectionName |
| 172 | + |
| 173 | + // Add the parameter to the appropriate section |
| 174 | + groupedParams[sectionName] = append(groupedParams[sectionName], param) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return groupedParams |
| 179 | +} |
0 commit comments