def put(self, file_name): new_content = parser.parse_args()["content"] path = define_path_to_file(file_name) if os.path.isfile(path): file_service.update_file_txt(path, new_content) return {"content": file_service.read_file(path)} return {"message": "file wasn't found"}, 404
def get(self, file_name): working_dir = os.path.join(os.getcwd(), TEST_FILES_DIR) files = [file for file in os.listdir(working_dir) if os.path.isfile(f'{working_dir}/{file}')] if os.path.isfile(define_path_to_file(file_name)): return {"all_files": files} elif file_name not in files: return {"message": "file wasn't found"}, 404 else: return {"content": file_service.read_file(define_path_to_file(file_name))}
def choose_action(args): """Choose action will use and what parameters are used Args: args (dict): All parameters for terminal """ if args.action == 'read_file': file_service.read_file(args.name) elif args.action == 'create_file': file_service.create_file(length_name=args.length, extension=args.extension, content=args.content, letter=bool(args.letters), digit=bool(args.digits)) elif args.action == 'delete_file': file_service.delete_file(args.name) elif args.action == 'get_metadata_file': file_service.get_metadata_file(args.name) else: file_service.evaluate_amount('(f-5)') logger.info('Try again! You choose wrong action')
def update_file(file_name): path = define_path_to_file(file_name) if request.method == 'POST': content = request.form.get('content', '') file_service.update_file_txt(path, content) logger.info( f'File: "{file_name}" was successfully updated with content "{content}"' ) flash(f'File {file_name} was successfully updated', category='success') return redirect(url_for('index')) content = file_service.read_file(path) return render_template('update_file.html', content=content)
def app(): while True: try: choice = show_options() if choice == '1': content = enter_content() create_file(content) elif choice == '2': filename = enter_filename() read_file(filename) elif choice == '3': filename = enter_filename() delete_file(filename) elif choice == '4': filename = enter_filename() get_metadata(filename) elif choice == '5': break else: raise InvalidOption except InvalidOption: print_option_error() except FileNotFoundError: print_location_error()
#! /usr/bin/env python3 from file_service import create_file, read_file, delete_file, get_metadata from utils import * if __name__ == '__main__': flag = True while flag: try: choice = show_options() if choice == 1: content = enter_content() create_file(content) elif choice == 2: filename = enter_filename() read_file(filename) elif choice == 3: filename = enter_filename() delete_file(filename) elif choice == 4: filename = enter_filename() get_metadata(filename) elif choice == 5: flag = False else: raise InvalidOption except InvalidOption: print_option_error() continue except FileNotFoundError: print_location_error() continue
def test_read_file(): with open('test_data/test1.txt', 'w') as file: file.write('some text') data = 'some text' assert file_service.read_file('test_data/test1.txt') == data