Esempio n. 1
0
    def run(self):
        """
        .. versionchanged: Lab 10: Rename map to f_map to avoid shadowing
        """
        files = FileDao.get_all_files()
        if len(files) != 0:
            format_string = '{:<38} {:<7} {:<23} {:<40} {:<8}'
            # Printing the header
            self.log('', prefix=False)
            self.log(format_string.format('MemoryAddress', 'Bytes', 'Path',
                                          'Sector Memory Address',
                                          'Sector Order'),
                     prefix=False)
            self.log('', prefix=False)

            # Printing memory specs for all files
            for file in files:
                path = FileDao.get_path(file)
                sectors = file.sectors
                f_map = format_string.format(
                    file.id, FileDao.get_file_size(file), path,
                    '' if file.is_empty else sectors[0].id,
                    '' if file.is_empty else sectors[0].order)
                self.log(f_map, prefix=False)
                for sector in sectors:
                    if sector == sectors[0]:
                        continue
                    self.log(format_string.format('', '', '', str(sector.id),
                                                  str(sector.order)),
                             prefix=False)
            self.log('', prefix=False)
        else:
            self.log('Memory is Empty!', prefix=False)
Esempio n. 2
0
    def run(self):
        if self.options[0].exists:
            directory = self.context.parse(self.arguments[0].data)

            self.recursive_remove(directory)
            DB().session.commit()
        else:
            file = self.context.parse(self.arguments[0].data, is_file=True)
            FileDao.delete_file(file)
Esempio n. 3
0
    def run(self):
        filename = self.arguments[0].data
        current_directory = self.context.current_directory

        if FileDao.is_valid_filename(filename):
            if FileDao.is_unique_filename(filename, current_directory):
                file = File(name=filename, directory_id=current_directory.id)
                FileDao.create_file(file)
            else:
                raise ValueError('File already exists')
        else:
            raise ValueError('file name should not start with ' +
                             'a special character and should not' +
                             ' contain \\ / characters')
Esempio n. 4
0
    def recursive_remove(self, directory):
        """Recursively removes directories
        :param directory: Directory model object to search"""
        directories = DirectoryDao.\
            get_directories_from_current_directory(directory)
        for directory in directories:
            self.recursive_remove(directory)
            DirectoryDao.delete_directory(directory, commit=False)

        files = FileDao.get_files_from_current_directory(directory)
        for file in files:
            FileDao.delete_file(file, commit=False)

        DirectoryDao.delete_directory(directory, commit=False)
Esempio n. 5
0
    def run(self):

        dir_list = DirectoryDao.get_directories_from_current_directory(
            self.context.current_directory)
        file_list = FileDao.get_files_from_current_directory(
            self.context.current_directory)
        self.log('Directories: ', prefix=False)
        self.display_lists(dir_list)
        self.log('Files: ', prefix=False)
        self.display_lists(file_list)
Esempio n. 6
0
    def run(self, *args, **kwargs):
        path = self.arguments[0].data
        file = self.context.parse(path, True)

        if self.options[0].exists:
            self.log('Warning: Data will be overwritten', prefix=False)
            text = next(iter(args), None) or self.get_input('Start Writing: ',
                                                            prefix=False)
            self.write_to_file(file, text)
        else:
            content = FileDao.read_from_file(file)
            self.log(content, prefix=False)
Esempio n. 7
0
        def parse(self, path, is_file=False):
            """Parses path to a directory object
            :param path: Path to parse
            :param is_file: Specifies if we are looking for a file or
            directory"""
            current_directory = self.current_directory

            split_path = path.split('/')

            # Check if user started the path from root
            if split_path[0] == '':
                current_directory = DirectoryDao.get_root_directory()
                split_path.pop(0)

            for level in split_path:
                if level == '':
                    continue
                elif level == split_path[-1] and is_file:
                    file = FileDao.get_file_from_current_directory(
                        current_directory, level)
                    if file is None:
                        raise ValueError('No such file exists!')

                    return file
                elif level == '..':
                    if current_directory.id != \
                            DirectoryDao.get_root_directory().id:
                        current_directory = current_directory.directory
                elif level == '.':
                    pass
                else:
                    directory = DirectoryDao.\
                        get_directory_from_current_directory(
                            current_directory, level
                        )
                    if directory is None:
                        raise ValueError('No such directory exists!')

                    current_directory = directory

            return current_directory
Esempio n. 8
0
 def run(self):
     path = FileDao.get_path(self)
     self.log(path, prefix=False)
Esempio n. 9
0
 def write_to_file(self, file, text):
     FileDao.remove_data_in_file(file)
     FileDao.insert_data_in_file(file, text)