hydrus/hydrus/client/metadata/ClientTags.py

94 lines
2.4 KiB
Python

import collections
import threading
from hydrus.core import HydrusGlobals as HG
from hydrus.core import HydrusSerialisable
from hydrus.core import HydrusTags
from hydrus.client import ClientConstants as CC
TAG_DISPLAY_STORAGE = 0
TAG_DISPLAY_ACTUAL = 1
TAG_DISPLAY_SINGLE_MEDIA = 2
TAG_DISPLAY_SELECTION_LIST = 3
TAG_DISPLAY_IDEAL = 4
have_shown_invalid_tag_warning = False
def RenderNamespaceForUser( namespace ):
if namespace == '' or namespace is None:
return 'unnamespaced'
else:
return namespace
def RenderTag( tag, render_for_user: bool ):
if render_for_user:
new_options = HG.client_controller.new_options
if new_options.GetBoolean( 'replace_tag_underscores_with_spaces' ):
tag = tag.replace( '_', ' ' )
( namespace, subtag ) = HydrusTags.SplitTag( tag )
if namespace == '':
return subtag
else:
if render_for_user:
if new_options.GetBoolean( 'show_namespaces' ):
connector = new_options.GetString( 'namespace_connector' )
else:
return subtag
else:
connector = ':'
return namespace + connector + subtag
class ServiceKeysToTags( HydrusSerialisable.SerialisableBase, collections.defaultdict ):
SERIALISABLE_TYPE = HydrusSerialisable.SERIALISABLE_TYPE_SERVICE_KEYS_TO_TAGS
SERIALISABLE_NAME = 'Service Keys To Tags'
SERIALISABLE_VERSION = 1
def __init__( self, *args, **kwargs ):
collections.defaultdict.__init__( self, set, *args, **kwargs )
HydrusSerialisable.SerialisableBase.__init__( self )
def _GetSerialisableInfo( self ):
return [ ( service_key.hex(), list( tags ) ) for ( service_key, tags ) in self.items() ]
def _InitialiseFromSerialisableInfo( self, serialisable_info ):
for ( service_key_hex, tags_list ) in serialisable_info:
self[ bytes.fromhex( service_key_hex ) ] = set( tags_list )
HydrusSerialisable.SERIALISABLE_TYPES_TO_OBJECT_TYPES[ HydrusSerialisable.SERIALISABLE_TYPE_SERVICE_KEYS_TO_TAGS ] = ServiceKeysToTags