Ejemplo n.º 1
0
class ReTagController(QtCore.QObject):
    """
    object for calling the re-tag view.
    ************************
    MANDATORY parameters: 
    ************************
    * application -> the parent qt-application object ()for installing the translator properly
    * store_path -> absolute path to the store of the item to be retagged (TIP: use the PathHelper object to resolve a relative path.)
    * item_name -> the name of the item to be renamed (exactly how it is defined in the tagfile)

    ************************
    TIP: use the PathHelper object to resolve a relative path AND to extract the item name out of it. 
    ************************
    
    ************************
    OPTIONAL parameters:
    ************************
    * standalone_application -> default = False; set this to true if there
    * verbose -> set this to true for detailed output
    (DEVEL * retag_mode -> this application could even be used for a normal tagging procedure as well.)
    
    ************************
    IMPORTANT!!!
    ************************
    the start() method must be called in order to begin with the tagging procedure
    """

    def __init__(self, application, store_path, item_name, retag_mode = True, verbose = False):
        QtCore.QObject.__init__(self)
        
        self.__log = None
        self.__main_config = None
        self.__store_config = None
        self.__tag_dialog = None
        self.__store = None
        
        self.__retag_mode = retag_mode
        
        self.__no_store_found = False

        self.__item_name = unicode(item_name)
        self.__store_path = store_path

        # the main application which has the translator installed
        self.__application = application

        self.LOG_LEVEL = logging.INFO
        if verbose:
            self.LOG_LEVEL = logging.DEBUG
            

        self.STORE_CONFIG_DIR = TsConstants.DEFAULT_STORE_CONFIG_DIR
        self.STORE_CONFIG_FILE_NAME = TsConstants.DEFAULT_STORE_CONFIG_FILENAME
        self.STORE_TAGS_FILE_NAME = TsConstants.DEFAULT_STORE_TAGS_FILENAME
        self.STORE_VOCABULARY_FILE_NAME = TsConstants.DEFAULT_STORE_VOCABULARY_FILENAME
        
        locale = unicode(QtCore.QLocale.system().name())[0:2]
        self.__translator = QtCore.QTranslator()
        if self.__translator.load("ts_" + locale + ".qm", "tsresources/"):
            self.__application.installTranslator(self.__translator)

        #get dir names for all available languages
        self.CURRENT_LANGUAGE = self.trUtf8("en")
        self.STORE_STORAGE_DIRS = []
        self.STORE_DESCRIBING_NAV_DIRS = []
        self.STORE_CATEGORIZING_NAV_DIRS = []
        self.STORE_EXPIRED_DIRS = []
        self.STORE_NAVIGATION_DIRS = []
        self.SUPPORTED_LANGUAGES = TsConstants.DEFAULT_SUPPORTED_LANGUAGES
        self.MAX_CLOUD_TAGS = TsConstants.DEFAULT_MAX_CLOUD_TAGS
        self.__store_dict = {}
        
        for lang in self.SUPPORTED_LANGUAGES: 
            self.change_language(lang)
            self.STORE_NAVIGATION_DIRS.append(self.trUtf8("navigation")) 
            self.STORE_STORAGE_DIRS.append(self.trUtf8("storage"))#self.STORE_STORAGE_DIR_EN))  
            self.STORE_DESCRIBING_NAV_DIRS.append(self.trUtf8("descriptions"))#self.STORE_DESCRIBING_NAVIGATION_DIR_EN))  
            self.STORE_CATEGORIZING_NAV_DIRS.append(self.trUtf8("categories"))#self.STORE_CATEGORIZING_NAVIGATION_DIR_EN)) 
            self.STORE_EXPIRED_DIRS.append(self.trUtf8("expired_items"))#STORE_EXPIRED_DIR_EN)) 
        ## reset language 
        self.change_language(self.CURRENT_LANGUAGE)
        
        self.__log = LogHelper.get_app_logger(self.LOG_LEVEL)
        
    def start(self):
        """
        call this method to actually start the tagging procedure
        """
        self.__init_configuration()
        
    
    def __init_configuration(self):
        """
        initializes the configuration. This method is called every time the config file changes
        """
        self.__log.info("initialize configuration")
        
        
        self.__main_config = ConfigWrapper(TsConstants.CONFIG_PATH)
        
        if self.__main_config is None:
            self.__emit_not_retagable(self.trUtf8("No config file found for the given path"))
            return
        
        ## check if there has been found an appropriate store_path in the config 
        if self.__store_path is None:
            self.__emit_not_retagable(self.trUtf8("No store found for the given path"))
            return
        else:
            self.__store_config = ConfigWrapper(self.__store_path)
        
        self.__prepare_store_params()
        
        self.CURRENT_LANGUAGE = self.__main_config.get_current_language();
        self.change_language(self.CURRENT_LANGUAGE)
        #self.__main_config.connect(self.__main_config, QtCore.SIGNAL("changed()"), self.__init_configuration)

        self.__store = Store(self.__store_config.get_store_id(), self.__store_path, 
              self.STORE_CONFIG_DIR + "/" + self.STORE_CONFIG_FILE_NAME,
              self.STORE_CONFIG_DIR + "/" + self.STORE_TAGS_FILE_NAME,
              self.STORE_CONFIG_DIR + "/" + self.STORE_VOCABULARY_FILE_NAME,
              self.STORE_NAVIGATION_DIRS,
              self.STORE_STORAGE_DIRS, 
              self.STORE_DESCRIBING_NAV_DIRS,
              self.STORE_CATEGORIZING_NAV_DIRS,
              self.STORE_EXPIRED_DIRS,
              self.__main_config.get_expiry_prefix())
        self.__store.init()
        
        if self.__tag_dialog is None:
            self.__tag_dialog = TagDialogController(self.__store.get_name(), self.__store.get_id(), self.__main_config.get_max_tags(), self.__main_config.get_tag_seperator(), self.__main_config.get_expiry_prefix())
            self.__tag_dialog.get_view().setModal(True)
            #self.__tag_dialog.set_parent(self.sender().get_view())
            self.__tag_dialog.connect(self.__tag_dialog, QtCore.SIGNAL("tag_item"), self.__tag_item_action)
            self.__tag_dialog.connect(self.__tag_dialog, QtCore.SIGNAL("handle_cancel()"), self.__handle_tag_cancel)


        ## configure the tag dialog with the according settings
        format_setting = self.__store.get_datestamp_format()
        datestamp_hidden = self.__store.get_datestamp_hidden()
        ## check if auto datestamp is enabled
        if format_setting != EDateStampFormat.DISABLED:
            self.__tag_dialog.show_datestamp(True)
            ## set the format
            format = None
            if format_setting == EDateStampFormat.DAY:
                format = TsConstants.DATESTAMP_FORMAT_DAY
            elif format_setting == EDateStampFormat.MONTH:
                format = TsConstants.DATESTAMP_FORMAT_MONTH
            
            self.__tag_dialog.set_datestamp_format(format, datestamp_hidden)
        
        self.__tag_dialog.show_category_line(self.__store.get_show_category_line())
        self.__tag_dialog.set_category_mandatory(self.__store.get_category_mandatory()) 
        
        ## check if the given item really exists in the store
        if not self.__store.item_exists(self.__item_name):
            self.__emit_not_retagable(self.trUtf8("%s: There is no such item recorded in the store" % self.__item_name))
            return 

        self.__set_tag_information_to_dialog(self.__store)
        
        if self.__retag_mode:
            self.__handle_retag_mode()
        
        self.__tag_dialog.show_dialog()
    
    def __emit_not_retagable(self, err_msg):
        self.__log.error(err_msg)
        self.emit(QtCore.SIGNAL("retag_error"))
    
    def __handle_retag(self, store_name, file_name_list, new_describing_tags, new_categorizing_tags):
        
        for file_name in file_name_list:
            ## first of all remove the old references
            self.__store.remove_file(file_name)
            ## now create the new navigation structure
            try:
                self.__store.add_item_with_tags(file_name, new_describing_tags, new_categorizing_tags)
            except InodeShortageException, e:
                self.__tag_dialog.show_message(self.trUtf8("The Number of free inodes is below the threshold of %s%" % e.get_threshold()))
                #raise
            except Exception, e:
                self.__tag_dialog.show_message(self.trUtf8("An error occurred while tagging"))
                raise
            else:
Ejemplo n.º 2
0
class ReTagController(QtCore.QObject):
    """
    object for calling the re-tag view.
    ************************
    MANDATORY parameters: 
    ************************
    * application -> the parent qt-application object ()for installing the translator properly
    * store_path -> absolute path to the store of the item to be retagged (TIP: use the PathHelper object to resolve a relative path.)
    * item_name -> the name of the item to be renamed (exactly how it is defined in the tagfile)

    ************************
    TIP: use the PathHelper object to resolve a relative path AND to extract the item name out of it. 
    ************************
    
    ************************
    OPTIONAL parameters:
    ************************
    * standalone_application -> default = False; set this to true if there
    * verbose -> set this to true for detailed output
    (DEVEL * retag_mode -> this application could even be used for a normal tagging procedure as well.)
    
    ************************
    IMPORTANT!!!
    ************************
    the start() method must be called in order to begin with the tagging procedure
    """

    def __init__(self, application, store_path, item_name, retag_mode=True, verbose=False):
        QtCore.QObject.__init__(self)

        self.__log = None
        self.__main_config = None
        self.__store_config = None
        self.__tag_dialog = None
        self.__store = None

        self.__retag_mode = retag_mode

        self.__no_store_found = False

        self.__item_name = unicode(item_name)
        self.__store_path = store_path

        # the main application which has the translator installed
        self.__application = application

        self.LOG_LEVEL = logging.INFO
        if verbose:
            self.LOG_LEVEL = logging.DEBUG

        self.STORE_CONFIG_DIR = TsConstants.DEFAULT_STORE_CONFIG_DIR
        self.STORE_CONFIG_FILE_NAME = TsConstants.DEFAULT_STORE_CONFIG_FILENAME
        self.STORE_TAGS_FILE_NAME = TsConstants.DEFAULT_STORE_TAGS_FILENAME
        self.STORE_VOCABULARY_FILE_NAME = TsConstants.DEFAULT_STORE_VOCABULARY_FILENAME

        locale = unicode(QtCore.QLocale.system().name())[0:2]
        self.__translator = QtCore.QTranslator()
        if self.__translator.load("ts_" + locale + ".qm", "tsresources/"):
            self.__application.installTranslator(self.__translator)

        # get dir names for all available languages
        self.CURRENT_LANGUAGE = self.trUtf8("en")
        self.STORE_STORAGE_DIRS = []
        self.STORE_DESCRIBING_NAV_DIRS = []
        self.STORE_CATEGORIZING_NAV_DIRS = []
        self.STORE_EXPIRED_DIRS = []
        self.STORE_NAVIGATION_DIRS = []
        self.SUPPORTED_LANGUAGES = TsConstants.DEFAULT_SUPPORTED_LANGUAGES
        self.MAX_CLOUD_TAGS = TsConstants.DEFAULT_MAX_CLOUD_TAGS
        self.__store_dict = {}

        for lang in self.SUPPORTED_LANGUAGES:
            self.change_language(lang)
            self.STORE_NAVIGATION_DIRS.append(self.trUtf8("navigation"))
            self.STORE_STORAGE_DIRS.append(self.trUtf8("storage"))  # self.STORE_STORAGE_DIR_EN))
            self.STORE_DESCRIBING_NAV_DIRS.append(
                self.trUtf8("descriptions")
            )  # self.STORE_DESCRIBING_NAVIGATION_DIR_EN))
            self.STORE_CATEGORIZING_NAV_DIRS.append(
                self.trUtf8("categories")
            )  # self.STORE_CATEGORIZING_NAVIGATION_DIR_EN))
            self.STORE_EXPIRED_DIRS.append(self.trUtf8("expired_items"))  # STORE_EXPIRED_DIR_EN))
        ## reset language
        self.change_language(self.CURRENT_LANGUAGE)

        self.__log = LogHelper.get_app_logger(self.LOG_LEVEL)

    def start(self):
        """
        call this method to actually start the tagging procedure
        """
        self.__init_configuration()

    def __init_configuration(self):
        """
        initializes the configuration. This method is called every time the config file changes
        """
        self.__log.info("initialize configuration")

        self.__main_config = ConfigWrapper(TsConstants.CONFIG_PATH)

        if self.__main_config is None:
            self.__emit_not_retagable(self.trUtf8("No config file found for the given path"))
            return

        ## check if there has been found an appropriate store_path in the config
        if self.__store_path is None:
            self.__emit_not_retagable(self.trUtf8("No store found for the given path"))
            return
        else:
            self.__store_config = ConfigWrapper(self.__store_path)

        self.__prepare_store_params()

        self.CURRENT_LANGUAGE = self.__main_config.get_current_language()
        self.change_language(self.CURRENT_LANGUAGE)
        # self.__main_config.connect(self.__main_config, QtCore.SIGNAL("changed()"), self.__init_configuration)

        self.__store = Store(
            self.__store_config.get_store_id(),
            self.__store_path,
            self.STORE_CONFIG_DIR + "/" + self.STORE_CONFIG_FILE_NAME,
            self.STORE_CONFIG_DIR + "/" + self.STORE_TAGS_FILE_NAME,
            self.STORE_CONFIG_DIR + "/" + self.STORE_VOCABULARY_FILE_NAME,
            self.STORE_NAVIGATION_DIRS,
            self.STORE_STORAGE_DIRS,
            self.STORE_DESCRIBING_NAV_DIRS,
            self.STORE_CATEGORIZING_NAV_DIRS,
            self.STORE_EXPIRED_DIRS,
            self.__main_config.get_expiry_prefix(),
        )
        self.__store.init()

        if self.__tag_dialog is None:
            self.__tag_dialog = TagDialogController(
                self.__store.get_name(),
                self.__store.get_id(),
                self.__main_config.get_max_tags(),
                self.__main_config.get_tag_seperator(),
                self.__main_config.get_expiry_prefix(),
            )
            self.__tag_dialog.get_view().setModal(True)
            # self.__tag_dialog.set_parent(self.sender().get_view())
            self.__tag_dialog.connect(self.__tag_dialog, QtCore.SIGNAL("tag_item"), self.__tag_item_action)
            self.__tag_dialog.connect(self.__tag_dialog, QtCore.SIGNAL("handle_cancel()"), self.__handle_tag_cancel)

        ## configure the tag dialog with the according settings
        format_setting = self.__store.get_datestamp_format()
        datestamp_hidden = self.__store.get_datestamp_hidden()
        ## check if auto datestamp is enabled
        if format_setting != EDateStampFormat.DISABLED:
            self.__tag_dialog.show_datestamp(True)
            ## set the format
            format = None
            if format_setting == EDateStampFormat.DAY:
                format = TsConstants.DATESTAMP_FORMAT_DAY
            elif format_setting == EDateStampFormat.MONTH:
                format = TsConstants.DATESTAMP_FORMAT_MONTH

            self.__tag_dialog.set_datestamp_format(format, datestamp_hidden)

        self.__tag_dialog.show_category_line(self.__store.get_show_category_line())
        self.__tag_dialog.set_category_mandatory(self.__store.get_category_mandatory())

        ## check if the given item really exists in the store
        if not self.__store.item_exists(self.__item_name):
            self.__emit_not_retagable(self.trUtf8("%s: There is no such item recorded in the store" % self.__item_name))
            return

        self.__set_tag_information_to_dialog(self.__store)

        if self.__retag_mode:
            self.__handle_retag_mode()

        self.__tag_dialog.show_dialog()

    def __emit_not_retagable(self, err_msg):
        self.__log.error(err_msg)
        self.emit(QtCore.SIGNAL("retag_error"))

    def __handle_retag(self, store_name, file_name_list, new_describing_tags, new_categorizing_tags):

        for file_name in file_name_list:
            ## first of all remove the old references
            self.__store.remove_file(file_name)
            ## now create the new navigation structure
            try:
                self.__store.add_item_with_tags(file_name, new_describing_tags, new_categorizing_tags)
            except InodeShortageException, e:
                self.__tag_dialog.show_message(
                    self.trUtf8("The Number of free inodes is below the threshold of %s%" % e.get_threshold())
                )
                # raise
            except Exception, e:
                self.__tag_dialog.show_message(self.trUtf8("An error occurred while tagging"))
                raise
            else:
Ejemplo n.º 3
0
    def __init_configurations(self):
        """
        initializes the configuration. This method is called every time the config file changes
        """
        self.__log.info("initialize configuration")
        ## reload config file - overwrite default settings
        self.__app_config_wrapper = ConfigWrapper(TsConstants.CONFIG_PATH)
        self.__app_config_wrapper.connect(self.__app_config_wrapper, QtCore.SIGNAL("changed()"), self.__init_configurations)
        self.__app_config_wrapper.print_app_config_to_log()
        tag_seperator = self.__app_config_wrapper.get_tag_seperator()
        if tag_seperator.strip() != "":
            self.TAG_SEPERATOR = tag_seperator
        expiry_prefix = self.__app_config_wrapper.get_expiry_prefix()
        if expiry_prefix.strip() != "":
            self.EXPIRY_PREFIX = expiry_prefix
        
        self.NUM_RECENT_TAGS = self.__app_config_wrapper.get_num_popular_tags()
        self.NUM_POPULAR_TAGS = self.__app_config_wrapper.get_num_popular_tags()
        self.MAX_TAGS = self.__app_config_wrapper.get_max_tags()
        
        self.CURRENT_LANGUAGE = self.__app_config_wrapper.get_current_language();
        if self.CURRENT_LANGUAGE is None or self.CURRENT_LANGUAGE == "":
            self.CURRENT_LANGUAGE = self.trUtf8("en")
        self.change_language(self.CURRENT_LANGUAGE)
        
        config_dir = self.__app_config_wrapper.get_store_config_directory()
        if config_dir != "":
            self.STORE_CONFIG_DIR = config_dir
        config_file_name = self.__app_config_wrapper.get_store_configfile_name()
        if config_file_name != "":
            self.STORE_CONFIG_FILE_NAME = config_file_name
        tags_file_name = self.__app_config_wrapper.get_store_tagsfile_name()
        if tags_file_name != "":
            self.STORE_TAGS_FILE_NAME = tags_file_name
        vocabulary_file_name = self.__app_config_wrapper.get_store_vocabularyfile_name()
        if vocabulary_file_name != "":
            self.STORE_VOCABULARY_FILE_NAME = vocabulary_file_name
        
       
#        self.SUPPORTED_LANGUAGES = self.__app_config_wrapper.get_supported_languages()
#        current_language = self.CURRENT_LANGUAGE
#        self.STORE_STORAGE_DIRS = []
#        self.STORE_NAVIGATION_DIRS = [] 
#        for lang in self.SUPPORTED_LANGUAGES:
#            self.change_language(lang)
#            self.STORE_STORAGE_DIRS.append(self.trUtf8("storage")) 
#            self.STORE_NAVIGATION_DIRS.append(self.trUtf8("navigation")) 
#        ## reset language
#        self.change_language(current_language)

        ## get stores from config file         
        config_store_items = self.__app_config_wrapper.get_stores()
        config_store_ids = self.__app_config_wrapper.get_store_ids()

        deleted_stores = []
        for store in self.STORES:
            id = store.get_id()
            if id in config_store_ids:
            ## update changed stores
                store.set_path(self.__app_config_wrapper.get_store_path(id), 
                               self.STORE_CONFIG_DIR + "/" + self.STORE_CONFIG_FILE_NAME,
                               self.STORE_CONFIG_DIR + "/" + self.STORE_TAGS_FILE_NAME,
                               self.STORE_CONFIG_DIR + "/" + self.STORE_VOCABULARY_FILE_NAME)
                store.change_expiry_prefix(self.EXPIRY_PREFIX)               
                config_store_ids.remove(id)             ## remove already updated items
            else:
            ## remove deleted stores
                deleted_stores.append(store)

        ## update deleted stores from global list after iterating through it
        for store in deleted_stores:
            self.STORES.remove(store)
            self.__log.debug("removed store: %s", store.get_name())
        
        ## add new stores
        for store_item in config_store_items:
            if store_item["id"] in config_store_ids:    ## new
                store = Store(store_item["id"], store_item["path"], 
                              self.STORE_CONFIG_DIR + "/" + self.STORE_CONFIG_FILE_NAME,
                              self.STORE_CONFIG_DIR + "/" + self.STORE_TAGS_FILE_NAME,
                              self.STORE_CONFIG_DIR + "/" + self.STORE_VOCABULARY_FILE_NAME,
                              self.STORE_NAVIGATION_DIRS,
                              self.STORE_STORAGE_DIRS, 
                              self.STORE_DESCRIBING_NAV_DIRS,
                              self.STORE_CATEGORIZING_NAV_DIRS,
                              self.STORE_EXPIRED_DIRS,
							  self.EXPIRY_PREFIX)

                store.connect(store, QtCore.SIGNAL("removed(PyQt_PyObject)"), self.store_removed)
                store.connect(store, QtCore.SIGNAL("renamed(PyQt_PyObject, QString)"), self.store_renamed)
                store.connect(store, QtCore.SIGNAL("file_renamed(PyQt_PyObject, QString, QString)"), self.file_renamed)
                store.connect(store, QtCore.SIGNAL("file_removed(PyQt_PyObject, QString)"), self.file_removed)
                store.connect(store, QtCore.SIGNAL("pending_operations_changed(PyQt_PyObject)"), self.pending_file_operations)
                store.connect(store, QtCore.SIGNAL("vocabulary_changed"), self.__handle_vocabulary_changed)
                store.connect(store, QtCore.SIGNAL("store_config_changed"), self.__handle_store_config_changed)

                extensions = self.__app_config_wrapper.get_additional_ignored_extension()
                ##if there comes a value from the config -> set it
                if len(extensions) > 0 and extensions[0] != "":
                    store.add_ignored_extensions(extensions)

                self.STORES.append(store)

                self.__log.debug("init store: %s", store.get_name())
                
                ## create a dialogcontroller for each store ...
                tmp_dialog = TagDialogController(store.get_name(), store.get_id(), self.MAX_TAGS, self.TAG_SEPERATOR, self.EXPIRY_PREFIX)
                
                tmp_dialog.connect(tmp_dialog, QtCore.SIGNAL("tag_item"), self.tag_item_action)
                tmp_dialog.connect(tmp_dialog, QtCore.SIGNAL("handle_cancel()"), self.handle_cancel)
                tmp_dialog.connect(tmp_dialog, QtCore.SIGNAL("open_store_admin_dialog()"), self.show_admin_dialog)
                
                self.DIALOGS[store.get_id()] = tmp_dialog
                ## call init to initialize new store instance (after adding the event handler)
                ## necessary if store was renamed during tagstore was not running (to write config)
                store.init()
                self.connect(tmp_dialog, QtCore.SIGNAL("item_selected"), self.__set_tag_information_to_dialog_wrapper)
                self.__configure_tag_dialog(store, tmp_dialog)