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)
# 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")

View File

@ -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']

View File

@ -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}")

View File

@ -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',

View File

@ -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