diff --git a/etiquette/helpers.py b/etiquette/helpers.py index 4439443..3eed846 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -400,6 +400,50 @@ def seconds_to_hms(seconds): hms = ':'.join('%02d' % part for part in parts) return hms +def split_easybake_string(ebstring): + ''' + Given an easybake string, return (tagname, synonym, rename_to), where + tagname may be a full qualified name, and at least one of + synonym or rename_to will be None since both are not posible at once. + + 'languages.python' -> ('languages.python', None, None) + 'languages.python+py' -> ('languages.python', 'py', None) + 'languages.python=bestlang' -> ('languages.python', None, 'bestlang') + ''' + ebstring = ebstring.strip() + ebstring = ebstring.strip('.+=') + + if ebstring == '': + raise exceptions.EasyBakeError('No tag supplied') + + if '=' in ebstring and '+' in ebstring: + raise exceptions.EasyBakeError('Cannot rename and assign snynonym at once') + + rename_parts = ebstring.split('=') + if len(rename_parts) > 2: + raise exceptions.EasyBakeError('Too many equals signs') + + if len(rename_parts) == 2: + (ebstring, rename_to) = rename_parts + + elif len(rename_parts) == 1: + (ebstring, rename_to) = (rename_parts[0], None) + + synonym_parts = ebstring.split('+') + if len(synonym_parts) > 2: + raise exceptions.EasyBakeError('Too many plus signs') + + if len(synonym_parts) == 2: + (tagname, synonym) = synonym_parts + + elif len(synonym_parts) == 1: + (tagname, synonym) = (synonym_parts[0], None) + + if not tagname: + raise exceptions.EasyBakeError('No tag supplied') + + return (tagname, synonym, rename_to) + def sql_listify(items): ''' Given a list of strings, return a string in the form of an SQL list. diff --git a/etiquette/photodb.py b/etiquette/photodb.py index 48f50c3..df913cc 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -1160,7 +1160,7 @@ class PDBUtilMixin: else: return None - def easybake(self, ebstring, author=None): + def easybake(self, ebstring, author=None, *, commit=True): ''' Easily create tags, groups, and synonyms with a string like "group1.group2.tag+synonym" @@ -1170,70 +1170,43 @@ class PDBUtilMixin: output_notes = [] def create_or_get(name): - #print('cog', name) try: item = self.get_tag(name=name) - note = ('existing_tag', item.qualified_name()) + note = ('existing_tag', item.name) except exceptions.NoSuchTag: item = self.new_tag(name, author=author, commit=False) - note = ('new_tag', item.qualified_name()) + note = ('new_tag', item.name) output_notes.append(note) return item - ebstring = ebstring.strip() - ebstring = ebstring.strip('.+=') - if ebstring == '': - raise exceptions.EasyBakeError('No tag supplied') - - if '=' in ebstring and '+' in ebstring: - raise exceptions.EasyBakeError('Cannot rename and assign snynonym at once') - - rename_parts = ebstring.split('=') - if len(rename_parts) == 2: - (ebstring, rename_to) = rename_parts - elif len(rename_parts) == 1: - ebstring = rename_parts[0] - rename_to = None - else: - raise exceptions.EasyBakeError('Too many equals signs') - - create_parts = ebstring.split('+') - if len(create_parts) == 2: - (tag, synonym) = create_parts - elif len(create_parts) == 1: - tag = create_parts[0] - synonym = None - else: - raise exceptions.EasyBakeError('Too many plus signs') - - if not tag: - raise exceptions.EasyBakeError('No tag supplied') + (tagname, synonym, rename_to) = helpers.split_easybake_string(ebstring) if rename_to: - tag = self.get_tag(name=tag) + tag = self.get_tag(name=tagname) old_name = tag.name tag.rename(rename_to) - note = ('rename', '%s=%s' % (old_name, tag.name)) + note = ('rename', f'{old_name}={tag.name}') output_notes.append(note) else: - tag_parts = tag.split('.') + tag_parts = tagname.split('.') tags = [create_or_get(t) for t in tag_parts] for (higher, lower) in zip(tags, tags[1:]): try: lower.join_group(higher, commit=False) - note = ('join_group', '%s.%s' % (higher.name, lower.name)) + note = ('join_group', f'{higher.name}.{lower.name}') output_notes.append(note) except exceptions.GroupExists: pass tag = tags[-1] - self.log.debug('Committing - easybake') - self.commit() - if synonym: synonym = tag.add_synonym(synonym) - note = ('new_synonym', '%s+%s' % (tag.name, synonym)) + note = ('new_synonym', f'{tag.name}+{synonym}') output_notes.append(note) + + if commit: + self.log.debug('Committing - easybake') + self.commit() return output_notes