Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F10359848
PythonImportsLinter.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
PythonImportsLinter.php
View Options
<?php
/**
* Copyright 2016 Pinterest, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Lints for illegal Python module imports.
*/
final
class
PythonImportsLinter
extends
ArcanistLinter
{
const
LINT_IMPORT
=
1
;
private
$pattern
;
private
$message
;
public
function
getInfoName
()
{
return
'Python Imports Linter'
;
}
public
function
getInfoDescription
()
{
return
pht
(
'Lints for illegal Python module imports.'
);
}
public
function
getLinterName
()
{
return
'PYTHON-IMPORTS'
;
}
public
function
getLinterConfigurationName
()
{
return
'python-imports'
;
}
public
function
getLintNameMap
()
{
return
array
(
self
::
LINT_IMPORT
=>
pht
(
'Illegal Import'
),
);
}
public
function
getLinterConfigurationOptions
()
{
$options
=
array
(
'python-imports.pattern'
=>
array
(
'type'
=>
'string'
,
'help'
=>
pht
(
'Module name pattern.'
),
),
'python-imports.message'
=>
array
(
'type'
=>
'optional string'
,
'help'
=>
pht
(
'Message to display when caught.'
),
),
);
return
$options
+
parent
::
getLinterConfigurationOptions
();
}
public
function
setLinterConfigurationValue
(
$key
,
$value
)
{
switch
(
$key
)
{
case
'python-imports.pattern'
:
$this
->
pattern
=
$value
;
return
;
case
'python-imports.message'
:
$this
->
message
=
$value
;
return
;
default
:
return
parent
::
setLinterConfigurationValue
(
$key
,
$value
);
}
}
public
function
lintPath
(
$path
)
{
$lines
=
phutil_split_lines
(
$this
->
getData
(
$path
),
false
);
$regex
=
"/^
\s
*(from|import)
\s
+(?P<module>{$this->pattern})
\b
/"
;
foreach
(
$lines
as
$lineno
=>
$line
)
{
$matches
=
array
();
if
(
preg_match
(
$regex
,
$line
,
$matches
,
PREG_OFFSET_CAPTURE
))
{
list
(
$module
,
$offset
)
=
$matches
[
'module'
];
$this
->
raiseLintAtLine
(
$lineno
+
1
,
$offset
+
1
,
self
::
LINT_IMPORT
,
pht
(
'This line imports a module ("%s") that is not allowed in '
.
'this file. %s'
,
$module
,
$this
->
message
),
$module
);
}
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Nov 8, 02:39 (15 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1033936
Default Alt Text
PythonImportsLinter.php (2 KB)
Attached To
Mode
R124 arcanist-linters
Attached
Detach File
Event Timeline
Log In to Comment