#!/bin/env python
from hash import Hash
from helpers import unset_empty
from json import dumps
from os import path


class Core(object):
    """A simple script to get Security Hash Algorithms into JSON format

    :param path: A string, the path of the file or the directory we have to return.
    :param algorithm: A string, the algorithm to use when hashing.
    """

    def __init__(self, path, algorithm='sha512'):
        self.path = path
        self.algorithm = algorithm

    def hierarchy(self, hash_of_file):
        """Build the dictionary of data

        :param hash_of_file: A dict, Hash of the current file.
        """

        result = {}
        local_result = result

        hierarchy = unset_empty(self.path.split('/'))
        for funilrys in hierarchy:
            if funilrys == hierarchy[-1]:
                local_result = local_result.setdefault(funilrys, hash_of_file)
            else:
                local_result = local_result.setdefault(funilrys, {})

        return result

    def get(self):
        """Brain of the program"""

        result = {}
        file_hash = {}

        if path.isfile(self.path):
            file_hash = Hash(self.path, self.algorithm, False).get()
            result = self.hierarchy(file_hash)

        return dumps(result, indent=4)

@@ -64,16 +64,31 @@
 et_empty
+, combine_dicts
 %0Afrom js
@@ -122,16 +122,22 @@
 ort path
+, walk
 %0A%0A%0Aclass
@@ -459,16 +459,49 @@
  = path%0A
+        self.ppath = %5Bself.path%5D%0A
         
@@ -704,24 +704,25 @@
 lt = %7B%7D%0A
+%0A
         
 local_re
@@ -717,30 +717,31 @@
     
-local_result = result%0A
+for item in self.ppath:
 %0A   
@@ -745,16 +745,20 @@
         
+    
 hierarch
@@ -773,25 +773,20 @@
 t_empty(
-self.path
+item
 .split('
@@ -790,16 +790,55 @@
 t('/'))%0A
+            local_result = result%0A%0A    
         
@@ -864,16 +864,20 @@
 rarchy:%0A
+    
         
@@ -918,32 +918,36 @@
                 
+  
+  
 local_result = l
@@ -961,32 +961,57 @@
 sult.setdefault(
+%0A                        
 funilrys, hash_o
@@ -1034,14 +1034,22 @@
     
+   
+ 
 else:%0A
+    
     
@@ -1152,246 +1152,1075 @@
  get
-(self):%0A        %22%22%22Brain of the program%22%22%22%0A%0A        result = %7B%7D%0A        file_hash = %7B%7D%0A%0A        if path.isfile(self.path):%0A            file_hash = Hash(self.path, self.algorithm, False).get()%0A            result = self.hierarchy(file_hash)
+_file_under_directory(self, root, file):%0A        %22%22%22Get the file(s) which is/are under the current directory%0A%0A        :param root: A string, the current root%0A        :param file: A string, the current file%0A        %22%22%22%0A%0A        result = %7B%7D%0A        path_to_file = path.join(root, file)%0A        self.ppath = unset_empty(path_to_file.split(self.path))%0A        file_hash = Hash(path_to_file, self.algorithm).get()%0A        result = self.hierarchy(file_hash)%0A%0A        return result%0A%0A    def get(self):%0A        %22%22%22Brain of the program. In charge to print the results into JSON format%22%22%22%0A%0A        result = %7B%7D%0A        file_hash = %7B%7D%0A%0A        if path.isfile(self.path):%0A            file_hash = Hash(self.path, self.algorithm, False).get()%0A            result = self.hierarchy(file_hash, True)%0A        elif path.isdir(self.path):%0A            for root, dirs, files in walk(self.path):%0A                for file in files:%0A                    sub = self.get_file_under_directory(root, file)%0A                    result = combine_dicts(sub, result)%0A        else:%0A            return None
 %0A%0A  

add Core().get_file_under_directory() + add directory walk + review/rewrite Core().hierarchy()