Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Directory class will act as an interface to directories providing useful information about them. The objects of this class can be used by bears that operate only on directories and perform analysis based on the things like directory structure Related to #4999
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import os | ||
|
||
from coalib.parsing.Globbing import relative_recursive_glob | ||
from coala_utils.decorators import generate_eq | ||
|
||
|
||
@generate_eq('path', 'timestamp') | ||
class Directory: | ||
""" | ||
The ``Directory`` class acts as an interface to directories and | ||
provides useful information about them such as: | ||
* The directory path. | ||
* The parent directory path. | ||
* The last modified timestamp of the directory. | ||
* The children contained inside a directory. | ||
>>> import os | ||
>>> d = Directory('tests/io/DirectoryTestDir/') | ||
Get the number of files and sub-directories at the top level: | ||
>>> len(d.get_children()) | ||
3 | ||
Get the number of all the files and sub-directories recursively: | ||
>>> len(d.get_children_recursively()) | ||
5 | ||
Get the path of the ``Directory`` object: | ||
>>> os.path.basename(d.path).endswith('DirectoryTestDir') | ||
True | ||
Get the parent directory: | ||
>>> os.path.basename(d.parent).endswith('io') | ||
True | ||
Get the last modified timestamp of the directory: | ||
>>> d.timestamp == os.path.getmtime(d.path) | ||
True | ||
""" | ||
|
||
def __init__(self, path): | ||
self._path = os.path.abspath(path) | ||
self._parent = os.path.abspath(os.path.dirname(self._path)) | ||
self._timestamp = os.path.getmtime(self._path) | ||
|
||
def get_children(self): | ||
""" | ||
:return: | ||
A list of all the files and sub-directories cotained at the | ||
top level of the directory. | ||
""" | ||
return os.listdir(self._path) | ||
|
||
def get_children_recursively(self): | ||
""" | ||
:return: | ||
A list of all the files and sub-directories contained | ||
inside a directory. | ||
""" | ||
return list(relative_recursive_glob(self._path, '**'))[1:] | ||
|
||
@property | ||
def path(self): | ||
""" | ||
:return: | ||
The directory path. | ||
""" | ||
return self._path | ||
|
||
@property | ||
def parent(self): | ||
""" | ||
:return: | ||
The parent directory. | ||
""" | ||
return self._parent | ||
|
||
@property | ||
def timestamp(self): | ||
""" | ||
:return: | ||
The last modified timestamp of the directory. | ||
""" | ||
return self._timestamp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import unittest | ||
|
||
from coalib.io.Directory import Directory | ||
|
||
|
||
class DirectoryTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.test_dir_path = os.path.join(os.path.dirname(__file__), | ||
'DirectoryTestDir') | ||
self.another_test_dir_path = os.path.join(os.path.dirname(__file__), | ||
'DirectoryTestDir', 'Dir1') | ||
self.uut = Directory(self.test_dir_path) | ||
|
||
def test_equal(self): | ||
self.assertEqual(self.uut, Directory(self.test_dir_path)) | ||
self.assertNotEqual(self.uut, Directory(self.another_test_dir_path)) | ||
|
||
def test_get_children(self): | ||
self.assertEqual(len(self.uut.get_children()), 3) | ||
|
||
def test_get_children_recursively(self): | ||
self.assertEqual(len(self.uut.get_children_recursively()), 5) | ||
|
||
def test_path(self): | ||
self.assertEqual(self.uut.path, self.test_dir_path) | ||
|
||
def test_parent(self): | ||
self.assertEqual(self.uut.parent, os.path.dirname(self.test_dir_path)) | ||
|
||
def test_timestamp(self): | ||
self.assertEqual(self.uut.timestamp, | ||
os.path.getmtime(self.test_dir_path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is test file number 2. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is test file number 3. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is test file number 1. |