def __init__(self): self.translations_updater = TranslationsUpdater() self.translations_loader = TranslationsLoader() self.translations_updater_watcher = FileWatcher(self.translations_updater) self.translations_loader_watcher = FileWatcher(self.translations_loader) self.reconfigure() setting_changed.connect(self._reconfigure) self.waiting_for_first_request = True
def test_remove_file_from_list(self): file_path = os.path.join(self.temp_dir, 'file.ext') with open(file_path, 'w'): pass operator = TestOperator((file_path,)) file_watcher = FileWatcher(operator) file_watcher.check() self.assertEqual(1, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) operator.files_list = () file_watcher.check() self.assertEqual(2, operator.list_files_calls) self.assertEqual(2, operator.execute_calls)
class PoLocalizationMiddleware(object): def __init__(self): self.translations_updater = TranslationsUpdater() self.translations_loader = TranslationsLoader() self.translations_updater_watcher = FileWatcher(self.translations_updater) self.translations_loader_watcher = FileWatcher(self.translations_loader) self.reconfigure() setting_changed.connect(self._reconfigure) self.waiting_for_first_request = True def _reconfigure(self, sender, **kwargs): self.reconfigure() def reconfigure(self): self.translations_updater.root_paths = get_translations_update_roots() self.translations_updater.locales = get_enabled_locales( excluded_locales=getattr(settings, 'UPDATE_TRANSLATIONS_EXCLUDED_LOCALES', ())) self.translations_updater.include_locations = getattr(settings, 'UPDATE_TRANSLATIONS_WITH_LOCATIONS', True) self.translations_updater.prune_obsoletes = getattr(settings, 'UPDATE_TRANSLATIONS_PRUNE_OBSOLETES', False) # Force update in case any setting has changed (which changes the output) self.translations_updater_watcher.set_dirty() self.translations_loader.locales = get_enabled_locales() self.translations_loader.locale_paths = get_translations_reload_roots() def process_request(self, request): if getattr(settings, 'AUTO_UPDATE_TRANSLATIONS', False): self.translations_updater_watcher.check() if self.waiting_for_first_request or getattr(settings, 'AUTO_RELOAD_TRANSLATIONS', settings.DEBUG): self.translations_loader_watcher.check() self.waiting_for_first_request = False
def test_touch_file(self): file_path = os.path.join(self.temp_dir, 'file.ext') with open(file_path, 'w'): pass start_time = time.time() operator = TestOperator((file_path, )) os.utime(file_path, (0, start_time)) file_watcher = FileWatcher(operator) file_watcher.check() self.assertEqual(1, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) file_watcher.check() self.assertEqual(2, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) os.utime(file_path, (0, start_time + 1)) file_watcher.check() self.assertEqual(3, operator.list_files_calls) self.assertEqual(2, operator.execute_calls)
def test_remove_file_from_list(self): file_path = os.path.join(self.temp_dir, 'file.ext') with open(file_path, 'w'): pass operator = TestOperator((file_path, )) file_watcher = FileWatcher(operator) file_watcher.check() self.assertEqual(1, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) operator.files_list = () file_watcher.check() self.assertEqual(2, operator.list_files_calls) self.assertEqual(2, operator.execute_calls)
def test_touch_file(self): file_path = os.path.join(self.temp_dir, 'file.ext') with open(file_path, 'w'): pass start_time = time.time() operator = TestOperator((file_path,)) os.utime(file_path, (0, start_time)) file_watcher = FileWatcher(operator) file_watcher.check() self.assertEqual(1, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) file_watcher.check() self.assertEqual(2, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) os.utime(file_path, (0, start_time + 1)) file_watcher.check() self.assertEqual(3, operator.list_files_calls) self.assertEqual(2, operator.execute_calls)
def test_empty(self): operator = TestOperator() file_watcher = FileWatcher(operator) file_watcher.check() self.assertEqual(1, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) file_watcher.check() self.assertEqual(2, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) file_watcher.set_dirty() self.assertEqual(2, operator.list_files_calls) self.assertEqual(1, operator.execute_calls) file_watcher.check() self.assertEqual(3, operator.list_files_calls) self.assertEqual(2, operator.execute_calls) file_watcher.check() self.assertEqual(4, operator.list_files_calls) self.assertEqual(2, operator.execute_calls)