Example #1
0
 def _on_created(self, event):
     # get path
     # add action
     try:
         file_path = event.src_path
         # if it's a hidden document, don't do anything
         if not is_hidden_file(file_path) and not self.is_translation(file_path):
             relative_path = file_path.replace(self.path, '')
             title = os.path.basename(os.path.normpath(file_path))
             curr_ext = os.path.splitext(file_path)[1]
             # return if the extension should be ignored or if the path is not a file
             if curr_ext in self.ignore_ext or not os.path.isfile(file_path):
                 # logger.info("Detected a file with an extension in the ignore list, ignoring..")
                 return
             # only add or update the document if it's not a hidden document and it's a new file
             try:
                 if self.doc_manager.is_doc_new(relative_path) and self.watch_folder:
                     self.add_document(file_path, title, locale=self.locale)
                 elif self.doc_manager.is_doc_modified(relative_path, self.path):
                     self.update_content(relative_path)
                 else:
                     return
             except KeyboardInterrupt:
                 for observer in self.observers:
                     observer.stop()
             except ConnectionError:
                 print("Could not connect to remote server.")
                 restart()
             except ValueError:
                 print(sys.exc_info()[1])
                 restart()
             doc = self.doc_manager.get_doc_by_prop('file_name', relative_path)
             if doc:
                 document_id = doc['id']
             else:
                 return
             if self.locale_delimiter:
                 try:
                     # curr_locale = title.split(self.locale_delimiter)[1]
                     # todo locale detection needs to be more robust
                     curr_locale = title.split(self.locale_delimiter)[-2]
                     fixed_locale = map_locale(curr_locale)
                     if fixed_locale:
                         print ("fixed locale: ", fixed_locale)
                         # self.watch_locales.add(fixed_locale)
                         self.detected_locales[document_id] = fixed_locale
                     else:
                         logger.warning('This document\'s detected locale: {0} is not supported.'.format(curr_locale))
                 except IndexError:
                     logger.warning('Cannot detect locales from file: {0}, not adding any locales'.format(title))
             self.watch_add_target(relative_path, document_id)
             # logger.info('Added new document {0}'.format(title
         # else:
         #     print("Skipping hidden file "+file_path)
     except KeyboardInterrupt:
         for observer in self.observers:
             observer.stop()
Example #2
0
 def _on_moved(self, event):
     """Used for programs, such as gedit, that modify documents by moving (overwriting)
     the previous document with the temporary file. Only the moved event contains the name of the
     destination file."""
     try:
         event = FileSystemEvent(event.dest_path)
         self._on_modified(event)
     except KeyboardInterrupt:
         for observer in self.observers:
             observer.stop()
     except Exception as err:
         restart("Error on moved: " + str(err) + "\nRestarting watch.")
Example #3
0
 def _on_moved(self, event):
     """Used for programs, such as gedit, that modify documents by moving (overwriting)
     the previous document with the temporary file. Only the moved event contains the name of the
     destination file."""
     try:
         event = FileSystemEvent(event.dest_path)
         self._on_modified(event)
     except KeyboardInterrupt:
         for observer in self.observers:
             observer.stop()
     except Exception as err:
         restart("Error on moved: "+str(err)+"\nRestarting watch.")
Example #4
0
 def _on_modified(self, event):
     """ Notify Lingotek cloud when a previously added file is modified """
     try:
         db_entries = self.doc_manager.get_all_entries()
         in_db = False
         fn = ''
         # print event.src_path
         for entry in db_entries:
             # print entry['file_name']
             if event.src_path.endswith(entry['file_name']):
                 fn = entry['file_name']
                 in_db = True
         if not event.is_directory and in_db:
             try:
                 # check that document is added in TMS before updating
                 if self.check_remote_doc_exist(fn) and self.doc_manager.is_doc_modified(fn, self.path):
                     # logger.info('Detected local content modified: {0}'.format(fn))
                     # self.update_document_action(os.path.join(self.path, fn))
                     # logger.info('Updating remote content: {0}'.format(fn))
                     self.polled_list.remove(fn)
                     self.update_content(fn)
             except KeyboardInterrupt:
                 for observer in self.observers:
                     observer.stop()
             except ConnectionError:
                 print("Could not connect to remote server.")
                 restart()
             except ValueError:
                 print(sys.exc_info()[1])
                 restart()
     except KeyboardInterrupt:
         for observer in self.observers:
             observer.stop()
     except Exception as err:
         restart("Error on modified: "+str(err)+"\nRestarting watch.")
Example #5
0
 def _on_modified(self, event):
     """ Notify Lingotek cloud when a previously added file is modified """
     try:
         db_entries = self.doc_manager.get_all_entries()
         in_db = False
         fn = ''
         # print event.src_path
         for entry in db_entries:
             # print entry['file_name']
             if event.src_path.endswith(entry['file_name']):
                 fn = entry['file_name']
                 in_db = True
         if not event.is_directory and in_db:
             try:
                 # check that document is added in TMS before updating
                 if self.check_remote_doc_exist(
                         fn) and self.doc_manager.is_doc_modified(
                             fn, self.path):
                     #logger.info('Detected local content modified: {0}'.format(fn))
                     #self.update_document_action(os.path.join(self.path, fn))
                     #logger.info('Updating remote content: {0}'.format(fn))
                     try:
                         self.polled_list.remove(fn)
                     except Exception:
                         pass
                     self.update_content(fn)
             except KeyboardInterrupt:
                 for observer in self.observers:
                     observer.stop()
             except ConnectionError:
                 print("Could not connect to remote server.")
                 restart()
             except ValueError:
                 print(sys.exc_info()[1])
                 restart()
     except KeyboardInterrupt:
         for observer in self.observers:
             observer.stop()
     except Exception as err:
         restart("Error on modified: " + str(err) + "\nRestarting watch.")
Example #6
0
    def _on_created(self, event):
        # get path
        # add action
        try:
            db_entries = self.doc_manager.get_all_entries()
            in_db = False
            fn = ''
            for entry in db_entries:
                if event.src_path.endswith(entry['file_name']):
                    fn = entry['file_name']
                    in_db = True
            if not event.is_directory and in_db:
                self._on_modified(event)
            else:
                file_path = event.src_path
                # if created file was a downloaded translation, don't add to poll. Prevents recursion from downloaded translation files when ltk watch is running
                if file_path in self.download_file_paths:
                    self.download_file_paths.remove(file_path)
                    return
                # if it's a hidden document, don't do anything
                if not self.is_hidden_file(
                        file_path) and not self.is_translation(file_path):
                    relative_path = file_path.replace(self.path, '')
                    title = os.path.basename(os.path.normpath(file_path))
                    curr_ext = os.path.splitext(file_path)[1]
                    # return if the extension should be ignored or if the path is not a file
                    if curr_ext in self.ignore_ext or not os.path.isfile(
                            file_path):
                        # logger.info("Detected a file with an extension in the ignore list, ignoring..")
                        return
                    # only add or update the document if it's not a hidden document and it's a new file
                    try:
                        if self.doc_manager.is_doc_new(
                                relative_path,
                                self.root_path) and self.watch_folder:
                            #testing
                            #self.polled_list.add(relative_path) #test that this doesn't break other areas of watch
                            #end testing

                            self.add_document(file_path,
                                              title,
                                              locale=self.locale)

                        elif self.doc_manager.is_doc_modified(
                                relative_path, self.path):
                            self.update_content(relative_path)
                        else:
                            return
                    except KeyboardInterrupt:
                        for observer in self.observers:
                            observer.stop()
                    except ConnectionError:
                        print("Could not connect to remote server.")
                        restart()
                    except ValueError:
                        print(sys.exc_info()[1])
                        restart()
                    doc = self.doc_manager.get_doc_by_prop(
                        'file_name', relative_path)
                    if doc:
                        document_id = doc['id']
                    else:
                        return
                    if self.locale_delimiter:
                        try:
                            # curr_locale = title.split(self.locale_delimiter)[1]
                            # todo locale detection needs to be more robust
                            curr_locale = title.split(
                                self.locale_delimiter)[-2]
                            fixed_locale = map_locale(curr_locale)
                            if fixed_locale:
                                print("fixed locale: ", fixed_locale)
                                # self.watch_locales.add(fixed_locale)
                                self.detected_locales[
                                    document_id] = fixed_locale
                            else:
                                logger.warning(
                                    'This document\'s detected locale: {0} is not supported.'
                                    .format(curr_locale))
                        except IndexError:
                            logger.warning(
                                'Cannot detect locales from file: {0}, not adding any locales'
                                .format(title))
                    self.watch_add_target(relative_path, document_id)
                    # logger.info('Added new document {0}'.format(title
                # else:
                #     print("Skipping hidden file "+file_path)
        except KeyboardInterrupt:
            for observer in self.observers:
                observer.stop()
 def handleError(self):
     if self.watch:
         logger.warning("Could not connect to Lingotek")
         restart("Restarting watch", self.timeout)
     else:
         raise ConnectionFailed("Could not connect to Lingotek")
 def handleError(self):
     if self.watch:
         logger.warning("Could not connect to Lingotek")
         restart("Restarting watch", self.timeout)
     else:
         raise ConnectionFailed("Could not connect to Lingotek")