#!/usr/bin/env python
# encoding: utf-8

from moodlefuse.moodle.courses.course_handler import CourseHandler
from moodlefuse.moodle.resources.resource_handler import ResourceHandler

import os


class FileSystemTranslator(object):

    @staticmethod
    def get_position_in_filesystem_as_array(path):
        path = path.strip(str(os.path.join(os.path.expanduser('~'), 'moodle')))
        if len(path) is 0:
            return []
        path_sections = path.split("/")
        return path_sections

    @staticmethod
    def get_directory_contents_based_on_path(paath):
        location = FileSystemTranslator.get_position_in_filesystem_as_array(path)
        dirents = ['.', '..']

        if len(location) is 0:
            dirents.extend(CourseHandler.get_courses_as_array())

        if len(location) is 1:
            dirents.extend(CourseHandler.get_course_categories_as_array('testcourse'))

        if len(location) is 2:
            dirents.extend(ResourceHandler.get_file_names_as_array())

        return dirents

    @staticmethod
    def path_exists_in_moodle(path):
        location = FileSystemTranslator.get_position_in_filesystem_as_array(path)
        if len(location) is 0:
            return True

        if len(location) is 1:
            return location[0] in CourseHandler.get_courses_as_array()

        if len(location) is 2:
            return location[1] in CourseHandler.get_course_categories_as_array('testcourse')

@@ -224,16 +224,309 @@
 ject):%0A%0A
+    @staticmethod%0A    def _location_is_in_root(location):%0A        return len(location) is 0%0A%0A    @staticmethod%0A    def _location_is_in_course(location):%0A        return len(location) is 1%0A%0A    @staticmethod%0A    def _location_is_in_course_categorie(location):%0A        return len(location) is 2%0A%0A
     @sta
@@ -850,17 +850,16 @@
 _path(pa
-a
 th):%0A   
@@ -971,35 +971,73 @@
 .'%5D%0A%0A        if 
-len
+FileSystemTranslator._location_is_in_root
 (location) is 0:
@@ -1022,37 +1022,32 @@
 n_root(location)
- is 0
 :%0A            di
@@ -1097,25 +1097,24 @@
 rray())%0A
-%0A
         
 if len(l
@@ -1105,37 +1105,74 @@
         
+el
 if 
-len(location) is 1
+FileSystemTranslator._location_is_in_course(location)
 :%0A      
@@ -1252,25 +1252,24 @@
 urse'))%0A
-%0A
         
 if len(l
@@ -1260,37 +1260,84 @@
         
+el
 if 
-len(location) is 2
+FileSystemTranslator._location_is_in_course_categorie(location)
 :%0A      
@@ -1554,32 +1554,33 @@
 _as_array(path)%0A
+%0A
         if len(l
@@ -1574,34 +1574,67 @@
      if 
-len(location) is 0
+FileSystemTranslator._location_is_in_root(location)
 :%0A      
@@ -1651,25 +1651,24 @@
 rn True%0A
-%0A
         
 if len(l
@@ -1663,29 +1663,66 @@
     
+el
 if 
-len(location) is 1
+FileSystemTranslator._location_is_in_course(location)
 :%0A  
@@ -1790,25 +1790,24 @@
 array()%0A
-%0A
         
 if len(l
@@ -1802,29 +1802,76 @@
     
+el
 if 
-len(location) is 2
+FileSystemTranslator._location_is_in_course_categorie(location)
 :%0A  
@@ -1961,8 +1961,47 @@
 ourse')%0A
+        else:%0A            return False%0A

Add helper methods to file system trannslator class