Skip to content

Commit

Permalink
Using PythonPathHelper to check if a file is a valid .py or __init__ …
Browse files Browse the repository at this point in the history
…file (made some fixes when checking if it's a valid __init__ file).
  • Loading branch information
fabioz committed Aug 15, 2013
1 parent 87de591 commit 82d5d3b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.dialogs.WorkingSetConfigurationBlock;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.editor.codecompletion.revisited.PythonPathHelper;
import org.python.pydev.plugin.PyStructureConfigHelpers;
import org.python.pydev.plugin.PydevPlugin;
import org.python.pydev.plugin.preferences.PydevPrefs;
Expand Down Expand Up @@ -703,7 +704,7 @@ protected boolean validatePage() {
File[] listFiles = locPath.toFile().listFiles();
boolean foundInit = false;
for (File file : listFiles) {
if (file.getName().equals("__init__.py")) {
if (PythonPathHelper.isValidInitFile(file.getName())) {
foundInit = true;
setMessage("Project location contains an __init__.py file. Consider using the location's parent folder instead.");
break;
Expand Down Expand Up @@ -732,7 +733,7 @@ private boolean hasPyFile(File file) {
return false;
}
else {
return file.getName().endsWith(".py");
return PythonPathHelper.isValidSourceFile(file.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ protected static ModulesFoundStructure.ZipContents getFromZip(File root, IProgre
* @return if the path passed belongs to a valid python source file (checks for the extension)
*/
public static boolean isValidSourceFile(String path) {
path = path.toLowerCase();
for (String end : FileTypesPreferencesPage.getDottedValidSourceFiles()) {
if (path.endsWith(end)) {
return true;
Expand Down Expand Up @@ -422,8 +421,16 @@ public boolean accept(File dir, String name) {
* @param item the file we want to check
* @return true if the file is a valid __init__ file
*/
public static boolean isValidInitFile(String item) {
return item.toLowerCase().indexOf("__init__.") != -1 && isValidSourceFile(item);
public static boolean isValidInitFile(String path) {
for (String end : FileTypesPreferencesPage.getDottedValidSourceFiles()) {
if (path.endsWith(end)) {
if (path.lastIndexOf("__init__") == path.length() - 8 - end.length()) {
return true;
}
}
}

return false;
}

/**
Expand Down Expand Up @@ -549,7 +556,8 @@ public void loadFromFile(File pythonpatHelperFile) throws IOException {
* @param pythonpatHelperFile
*/
public void saveToFile(File pythonpatHelperFile) {
FileUtils.writeStrToFile(org.python.pydev.shared_core.string.StringUtils.join("\n", this.pythonpath), pythonpatHelperFile);
FileUtils.writeStrToFile(org.python.pydev.shared_core.string.StringUtils.join("\n", this.pythonpath),
pythonpatHelperFile);
}

public static boolean canAddAstInfoFor(ModulesKey key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static void main(String[] args) {
/**
* @see junit.framework.TestCase#setUp()
*/
@Override
public void setUp() throws Exception {
super.setUp();
CompiledModule.COMPILED_MODULES_ENABLED = false;
Expand All @@ -64,6 +65,7 @@ public void setUp() throws Exception {
/**
* @see junit.framework.TestCase#tearDown()
*/
@Override
public void tearDown() throws Exception {
CompiledModule.COMPILED_MODULES_ENABLED = true;
super.tearDown();
Expand Down Expand Up @@ -125,7 +127,7 @@ public void testModuleCompletion() {

private IToken[] getComps(Document doc, ICompletionState state) {
try {
return ((ICodeCompletionASTManager) nature.getAstManager()).getCompletionsForToken(doc, state);
return nature.getAstManager().getCompletionsForToken(doc, state);
} catch (CompletionRecursionException e) {
throw new RuntimeException(e);
}
Expand All @@ -141,7 +143,7 @@ public void testRecursionModuleCompletion() throws CompletionRecursionException
IToken[] comps = null;
Document doc = new Document(sDoc);
ICompletionState state = new CompletionState(line, col, token, nature, "");
ICodeCompletionASTManager a = (ICodeCompletionASTManager) nature.getAstManager();
ICodeCompletionASTManager a = nature.getAstManager();
comps = a.getCompletionsForToken(doc, state);
assertFalse(comps.length == 0);

Expand All @@ -157,7 +159,7 @@ public void testRecursion2() throws CompletionRecursionException {
IToken[] comps = null;
Document doc = new Document(sDoc);
ICompletionState state = new CompletionState(line, col, token, nature, "");
ICodeCompletionASTManager a = (ICodeCompletionASTManager) nature.getAstManager();
ICodeCompletionASTManager a = nature.getAstManager();
comps = a.getCompletionsForToken(doc, state);
assertEquals(0, comps.length);

Expand Down Expand Up @@ -368,4 +370,14 @@ public void testGetEncoding() {
String encoding = FileUtils.getPythonFileEncoding(new File(loc));
assertEquals("UTF-8", encoding);
}

public void testValidInitFile() throws Exception {
assertTrue(PythonPathHelper.isValidInitFile("a/__init__.py"));
assertTrue(PythonPathHelper.isValidInitFile("a/__init__/a/__init__.py"));

assertFalse(PythonPathHelper.isValidInitFile("a/__init__.bar.py"));
assertFalse(PythonPathHelper.isValidInitFile("a/__init__..py"));
assertFalse(PythonPathHelper.isValidInitFile("a/__init__/a/.py"));
assertFalse(PythonPathHelper.isValidInitFile("a/__init__/a/__init__ .py"));
}
}

0 comments on commit 82d5d3b

Please sign in to comment.