Changeset View
Changeset View
Standalone View
Standalone View
arcanist/linter/PythonFormatLinter.php
<?php | <?php | ||||
/** | /** | ||||
* Uses the lint-python-format.py script to update from old % format style to | * Uses the lint-python-format.py script to update from old % format style to | ||||
* new .format(). | * new .format(). | ||||
*/ | */ | ||||
final class PythonFormatLinter extends ArcanistExternalLinter { | final class PythonFormatLinter extends ArcanistExternalLinter | ||||
{ | |||||
public function getInfoName() { | public function getInfoName() | ||||
{ | |||||
return 'lint-python-format'; | return 'lint-python-format'; | ||||
} | } | ||||
public function getInfoURI() { | public function getInfoURI() | ||||
{ | |||||
return ''; | return ''; | ||||
} | } | ||||
public function getInfoDescription() { | public function getInfoDescription() | ||||
{ | |||||
return pht('Convert python string formatting from % to .format().'); | return pht('Convert python string formatting from % to .format().'); | ||||
} | } | ||||
public function getLinterName() { | public function getLinterName() | ||||
{ | |||||
return 'lint-python-format'; | return 'lint-python-format'; | ||||
} | } | ||||
public function getLinterConfigurationName() { | public function getLinterConfigurationName() | ||||
{ | |||||
return 'lint-python-format'; | return 'lint-python-format'; | ||||
} | } | ||||
public function getLinterConfigurationOptions() { | public function getLinterConfigurationOptions() | ||||
{ | |||||
$options = array(); | $options = array(); | ||||
return $options + parent::getLinterConfigurationOptions(); | return $options + parent::getLinterConfigurationOptions(); | ||||
} | } | ||||
public function getDefaultBinary() { | public function getDefaultBinary() | ||||
{ | |||||
return Filesystem::resolvePath( | return Filesystem::resolvePath( | ||||
'test/lint/lint-python-format.py', | 'test/lint/lint-python-format.py', | ||||
$this->getProjectRoot()); | $this->getProjectRoot() | ||||
); | |||||
} | } | ||||
public function shouldUseInterpreter() { | public function shouldUseInterpreter() | ||||
{ | |||||
return true; | return true; | ||||
} | } | ||||
public function getDefaultInterpreter() { | public function getDefaultInterpreter() | ||||
{ | |||||
return "python3"; | return "python3"; | ||||
} | } | ||||
public function getInstallInstructions() { | public function getInstallInstructions() | ||||
return pht('The test/lint/lint-python-format.py script is part of the bitcoin-abc project'); | { | ||||
return pht('The test/lint/lint-python-format.py script is part of the '. | |||||
'bitcoin-abc project'); | |||||
} | } | ||||
public function shouldExpectCommandErrors() { | public function shouldExpectCommandErrors() | ||||
{ | |||||
return false; | return false; | ||||
} | } | ||||
protected function getMandatoryFlags() { | protected function getMandatoryFlags() | ||||
{ | |||||
return array(); | return array(); | ||||
} | } | ||||
protected function parseLinterOutput($path, $err, $stdout, $stderr) { | protected function parseLinterOutput($path, $err, $stdout, $stderr) | ||||
{ | |||||
$pattern = '/\((\d+)\) ([\s\S]+?)=> (.+)/'; | $pattern = '/\((\d+)\) ([\s\S]+?)=> (.+)/'; | ||||
$found = preg_match_all($pattern, $stdout, $snippets, | $found = preg_match_all( | ||||
$flags = PREG_SET_ORDER); | $pattern, | ||||
$stdout, | |||||
$snippets, | |||||
$flags = PREG_SET_ORDER | |||||
); | |||||
/* | /* | ||||
* Matched snippets $snippets are organized like this: | * Matched snippets $snippets are organized like this: | ||||
* [0] The complete mask | * [0] The complete mask | ||||
* [1] The line number | * [1] The line number | ||||
* [2] The original snippet | * [2] The original snippet | ||||
* [3] The replacement snippet | * [3] The replacement snippet | ||||
*/ | */ | ||||
if (!$found) { | if (!$found) { | ||||
return array(); | return array(); | ||||
} | } | ||||
$messages = []; | $messages = []; | ||||
foreach($snippets as $snippet) { | foreach ($snippets as $snippet) { | ||||
$messages[] = id(new ArcanistLintMessage()) | $messages[] = id(new ArcanistLintMessage()) | ||||
->setPath($path) | ->setPath($path) | ||||
->setLine($snippet[1]) | ->setLine($snippet[1]) | ||||
->setChar(1) | ->setChar(1) | ||||
->setGranularity(ArcanistLinter::GRANULARITY_FILE) | ->setGranularity(ArcanistLinter::GRANULARITY_FILE) | ||||
->setCode('PYFMT') | ->setCode('PYFMT') | ||||
->setSeverity(ArcanistLintSeverity::SEVERITY_AUTOFIX) | ->setSeverity(ArcanistLintSeverity::SEVERITY_AUTOFIX) | ||||
->setName('Old string format notation') | ->setName('Old string format notation') | ||||
->setDescription("'$path' uses old style string formatting.") | ->setDescription("'$path' uses old style string formatting.") | ||||
->setOriginalText(rtrim($snippet[2])) | ->setOriginalText(rtrim($snippet[2])) | ||||
->setReplacementText($snippet[3]); | ->setReplacementText($snippet[3]); | ||||
} | } | ||||
return $messages; | return $messages; | ||||
} | } | ||||
} | } | ||||
No newline at end of file |