Skip to content

Commit

Permalink
Add Directory class
Browse files Browse the repository at this point in the history
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
palash25 committed Jun 16, 2018
1 parent b06dfe4 commit 77c1dca
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
89 changes: 89 additions & 0 deletions coalib/io/Directory.py
@@ -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
34 changes: 34 additions & 0 deletions tests/io/DirectoryTest.py
@@ -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))
1 change: 1 addition & 0 deletions tests/io/DirectoryTestDir/Dir1/test2.txt
@@ -0,0 +1 @@
This is test file number 2.
1 change: 1 addition & 0 deletions tests/io/DirectoryTestDir/Dir2/test3.txt
@@ -0,0 +1 @@
This is test file number 3.
1 change: 1 addition & 0 deletions tests/io/DirectoryTestDir/test1.txt
@@ -0,0 +1 @@
This is test file number 1.

0 comments on commit 77c1dca

Please sign in to comment.