From b7435c4eacd701153fa22bfa52b8d292ed4334e8 Mon Sep 17 00:00:00 2001 From: Cedric Leporcq Date: Sat, 27 Aug 2022 18:58:27 +0200 Subject: [PATCH] Modify input text fonction and add Input Class --- ordigi/collection.py | 26 ++++++++++---------------- ordigi/media.py | 14 +++----------- ordigi/request.py | 10 ++++++++++ tests/test_cli.py | 7 ++++--- tests/test_collection.py | 17 +++++++++-------- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/ordigi/collection.py b/ordigi/collection.py index c6eec7b..31d3658 100644 --- a/ordigi/collection.py +++ b/ordigi/collection.py @@ -494,6 +494,7 @@ class SortMedias: self.summary = Summary(self.root) # Attributes + self.input = request.Input() self.theme = request.load_theme() def _checkcomp(self, dest_path, src_checksum): @@ -580,14 +581,10 @@ class SortMedias: self.log.warning(f'Target directory {dir_path} is a file') # Rename the src_file if self.interactive: - prompt = [ - inquirer.Text( - 'file_path', - message="New name for" f"'{dir_path.name}' file", - ), - ] - answers = inquirer.prompt(prompt, theme=self.theme) - file_path = dir_path.parent / answers['file_path'] + answer = self.input.text( + "New name for" f"'{dir_path.name}' file" + ) + file_path = dir_path.parent / answer else: file_path = dir_path.parent / (dir_path.name + '_file') @@ -1201,6 +1198,7 @@ class Collection(SortMedias): def edit_metadata(self, paths, keys, loc=None, overwrite=False): """Edit metadata and exif data for given key""" + if self.db.sqlite.is_empty('metadata'): self.init(loc) for file_path, media in self.medias.get_medias_datas(paths, loc=loc): @@ -1225,17 +1223,13 @@ class Collection(SortMedias): print(f"{key}: '{value}'") if overwrite or not value: # Prompt value for given key for file_path - prompt = [ - inquirer.Text('value', message=key), - ] - answer = inquirer.prompt(prompt, theme=self.theme) - # answer = {'value': '03-12-2021 08:12:35'} - # Validate value + answer = self.input.text(key) + # Check value if key in ('date_original', 'date_created', 'date_modified'): # Check date format - value = media.get_date_format(answer['value']) + value = media.get_date_format(answer) else: - value = answer['value'] + value = answer while not value.isalnum(): if not value: break print("Invalid entry, use alphanumeric chars") diff --git a/ordigi/media.py b/ordigi/media.py index fb5a8a6..e2486ee 100644 --- a/ordigi/media.py +++ b/ordigi/media.py @@ -345,11 +345,8 @@ class Media(ReadExif): sys.exit() if not answers['date_list']: - prompt = [ - inquirer.Text('date_custom', message="date"), - ] - answers = inquirer.prompt(prompt, theme=self.theme) - return self.get_date_format(answers['date_custom']) + answer = self.prompt.text("date") + return self.get_date_format(answer) return answers['date_list'] @@ -467,17 +464,12 @@ class Media(ReadExif): default=f'{album}', ), ] - prompt = [ - inquirer.Text('custom', message="album"), - ] - answers = inquirer.prompt(choices_list, theme=self.theme) if not answers: sys.exit() if not answers['album']: - answers = inquirer.prompt(prompt, theme=self.theme) - return answers['custom'] + return self.input.text("album") return answers['album'] diff --git a/ordigi/request.py b/ordigi/request.py index 8d6b77a..dba4d34 100644 --- a/ordigi/request.py +++ b/ordigi/request.py @@ -1,5 +1,6 @@ import inquirer from blessed import Terminal +from colorama import init,Fore,Style,Back term = Terminal() @@ -34,6 +35,15 @@ def load_theme(): return inquirer.themes.load_theme_from_dict(custom_theme) +class Input(): + + def __init__(self): + + init() + + def text(self, message): + return input(f'{Fore.BLUE}[{Fore.YELLOW}?{Fore.BLUE}]{Fore.WHITE} {message}: ') + # def edit_prompt(self, key: str, value: str) -> str: # print(f"Date conflict for file: {self.file_path}") diff --git a/tests/test_cli.py b/tests/test_cli.py index b4dc200..7216416 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -5,6 +5,7 @@ import pytest import inquirer from ordigi import cli +from ordigi.request import Input CONTENT = "content" @@ -88,10 +89,10 @@ class TestOrdigi: *self.filter_options, ) - def mockreturn(prompt, theme): - return {'value': '03-12-2021 08:12:35'} + def mockreturn(self, message): + return '03-12-2021 08:12:35' - monkeypatch.setattr(inquirer, 'prompt', mockreturn) + monkeypatch.setattr(Input, 'text', mockreturn) args = ( '--key', diff --git a/tests/test_collection.py b/tests/test_collection.py index 22b3a93..124c2d9 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -8,13 +8,14 @@ import inquirer from ordigi import LOG from ordigi import constants +from ordigi import utils +from ordigi.summary import Summary from ordigi.collection import Collection, FPath, Paths from ordigi.exiftool import ExifTool, ExifToolCaching, exiftool_is_running, terminate_exiftool from ordigi.geolocation import GeoLocation from ordigi.media import Media, ReadExif -from ordigi import utils +from ordigi.request import Input from .conftest import randomize_files, randomize_db -from ordigi.summary import Summary LOG.setLevel(10) @@ -257,10 +258,10 @@ class TestCollection: shutil.copytree(self.src_path, path) collection = Collection(path, {'cache': False}) - def mockreturn(prompt, theme): - return {'value': '03-12-2021 08:12:35'} + def mockreturn(self, message): + return '03-12-2021 08:12:35' - monkeypatch.setattr(inquirer, 'prompt', mockreturn) + monkeypatch.setattr(Input, 'text', mockreturn) collection.edit_metadata({path}, {'date_original'}, overwrite=True) # check if db value is set @@ -278,10 +279,10 @@ class TestCollection: collection = Collection(path, {'cache': False}) loc = GeoLocation() - def mockreturn(prompt, theme): - return {'value': 'lyon'} + def mockreturn(self, message): + return 'lyon' - monkeypatch.setattr(inquirer, 'prompt', mockreturn) + monkeypatch.setattr(Input, 'text', mockreturn) collection.edit_metadata({path}, {'location'}, loc, True) # check if db value is set