Modify input text fonction and add Input Class

This commit is contained in:
Cédric Leporcq 2022-08-27 18:58:27 +02:00
parent 723f549f73
commit b7435c4eac
5 changed files with 36 additions and 38 deletions

View File

@ -494,6 +494,7 @@ class SortMedias:
self.summary = Summary(self.root) self.summary = Summary(self.root)
# Attributes # Attributes
self.input = request.Input()
self.theme = request.load_theme() self.theme = request.load_theme()
def _checkcomp(self, dest_path, src_checksum): def _checkcomp(self, dest_path, src_checksum):
@ -580,14 +581,10 @@ class SortMedias:
self.log.warning(f'Target directory {dir_path} is a file') self.log.warning(f'Target directory {dir_path} is a file')
# Rename the src_file # Rename the src_file
if self.interactive: if self.interactive:
prompt = [ answer = self.input.text(
inquirer.Text( "New name for" f"'{dir_path.name}' file"
'file_path', )
message="New name for" f"'{dir_path.name}' file", file_path = dir_path.parent / answer
),
]
answers = inquirer.prompt(prompt, theme=self.theme)
file_path = dir_path.parent / answers['file_path']
else: else:
file_path = dir_path.parent / (dir_path.name + '_file') 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): def edit_metadata(self, paths, keys, loc=None, overwrite=False):
"""Edit metadata and exif data for given key""" """Edit metadata and exif data for given key"""
if self.db.sqlite.is_empty('metadata'): if self.db.sqlite.is_empty('metadata'):
self.init(loc) self.init(loc)
for file_path, media in self.medias.get_medias_datas(paths, loc=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}'") print(f"{key}: '{value}'")
if overwrite or not value: if overwrite or not value:
# Prompt value for given key for file_path # Prompt value for given key for file_path
prompt = [ answer = self.input.text(key)
inquirer.Text('value', message=key), # Check value
]
answer = inquirer.prompt(prompt, theme=self.theme)
# answer = {'value': '03-12-2021 08:12:35'}
# Validate value
if key in ('date_original', 'date_created', 'date_modified'): if key in ('date_original', 'date_created', 'date_modified'):
# Check date format # Check date format
value = media.get_date_format(answer['value']) value = media.get_date_format(answer)
else: else:
value = answer['value'] value = answer
while not value.isalnum(): while not value.isalnum():
if not value: break if not value: break
print("Invalid entry, use alphanumeric chars") print("Invalid entry, use alphanumeric chars")

View File

@ -345,11 +345,8 @@ class Media(ReadExif):
sys.exit() sys.exit()
if not answers['date_list']: if not answers['date_list']:
prompt = [ answer = self.prompt.text("date")
inquirer.Text('date_custom', message="date"), return self.get_date_format(answer)
]
answers = inquirer.prompt(prompt, theme=self.theme)
return self.get_date_format(answers['date_custom'])
return answers['date_list'] return answers['date_list']
@ -467,17 +464,12 @@ class Media(ReadExif):
default=f'{album}', default=f'{album}',
), ),
] ]
prompt = [
inquirer.Text('custom', message="album"),
]
answers = inquirer.prompt(choices_list, theme=self.theme) answers = inquirer.prompt(choices_list, theme=self.theme)
if not answers: if not answers:
sys.exit() sys.exit()
if not answers['album']: if not answers['album']:
answers = inquirer.prompt(prompt, theme=self.theme) return self.input.text("album")
return answers['custom']
return answers['album'] return answers['album']

View File

@ -1,5 +1,6 @@
import inquirer import inquirer
from blessed import Terminal from blessed import Terminal
from colorama import init,Fore,Style,Back
term = Terminal() term = Terminal()
@ -34,6 +35,15 @@ def load_theme():
return inquirer.themes.load_theme_from_dict(custom_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: # def edit_prompt(self, key: str, value: str) -> str:
# print(f"Date conflict for file: {self.file_path}") # print(f"Date conflict for file: {self.file_path}")

View File

@ -5,6 +5,7 @@ import pytest
import inquirer import inquirer
from ordigi import cli from ordigi import cli
from ordigi.request import Input
CONTENT = "content" CONTENT = "content"
@ -88,10 +89,10 @@ class TestOrdigi:
*self.filter_options, *self.filter_options,
) )
def mockreturn(prompt, theme): def mockreturn(self, message):
return {'value': '03-12-2021 08:12:35'} return '03-12-2021 08:12:35'
monkeypatch.setattr(inquirer, 'prompt', mockreturn) monkeypatch.setattr(Input, 'text', mockreturn)
args = ( args = (
'--key', '--key',

View File

@ -8,13 +8,14 @@ import inquirer
from ordigi import LOG from ordigi import LOG
from ordigi import constants from ordigi import constants
from ordigi import utils
from ordigi.summary import Summary
from ordigi.collection import Collection, FPath, Paths from ordigi.collection import Collection, FPath, Paths
from ordigi.exiftool import ExifTool, ExifToolCaching, exiftool_is_running, terminate_exiftool from ordigi.exiftool import ExifTool, ExifToolCaching, exiftool_is_running, terminate_exiftool
from ordigi.geolocation import GeoLocation from ordigi.geolocation import GeoLocation
from ordigi.media import Media, ReadExif from ordigi.media import Media, ReadExif
from ordigi import utils from ordigi.request import Input
from .conftest import randomize_files, randomize_db from .conftest import randomize_files, randomize_db
from ordigi.summary import Summary
LOG.setLevel(10) LOG.setLevel(10)
@ -257,10 +258,10 @@ class TestCollection:
shutil.copytree(self.src_path, path) shutil.copytree(self.src_path, path)
collection = Collection(path, {'cache': False}) collection = Collection(path, {'cache': False})
def mockreturn(prompt, theme): def mockreturn(self, message):
return {'value': '03-12-2021 08:12:35'} 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) collection.edit_metadata({path}, {'date_original'}, overwrite=True)
# check if db value is set # check if db value is set
@ -278,10 +279,10 @@ class TestCollection:
collection = Collection(path, {'cache': False}) collection = Collection(path, {'cache': False})
loc = GeoLocation() loc = GeoLocation()
def mockreturn(prompt, theme): def mockreturn(self, message):
return {'value': 'lyon'} return 'lyon'
monkeypatch.setattr(inquirer, 'prompt', mockreturn) monkeypatch.setattr(Input, 'text', mockreturn)
collection.edit_metadata({path}, {'location'}, loc, True) collection.edit_metadata({path}, {'location'}, loc, True)
# check if db value is set # check if db value is set