Esempio n. 1
0
 def parse(self, filename):
     # Open file.
     if os.path.basename(filename) != "log.txt":
         before_extension = os.path.basename(filename).split(".")[0]
         if before_extension.count("_") == 1:
             self.user_id, self.log_number = before_extension.split("_")
         else:
             self.user_id, self.machine_id, self.log_number = \
                 before_extension.split("_")                
         self.log_number = int(self.log_number)
     if filename.endswith(".bz2"):
         self.logfile = bz2.BZ2File(filename)
     else:
         self.logfile = file(filename)
     # For pre-2.0 logs, we need to hang on to the previous timestamp, as
     # this will be used as the time the card was shown, in order to
     # calculate the actual interval. (The timestamps for repetitions are
     # when the card was graded, not when it was presented to the user.)        
     self.timestamp = None
     self.previous_timestamp = None
     self.lower_timestamp_limit = 1121021345 # 2005-07-10 21:49:05.
     self.upper_timestamp_limit = time.time()
     for line in self.logfile:
         try:
             self._parse_line(line)
         except:
             print "Ignoring error while parsing line:\n%s" % line
             print traceback_string()
             sys.stdout.flush()                
 def parse(self, filename):
     # Open file.
     if os.path.basename(filename) != "log.txt":
         before_extension = os.path.basename(filename).split(".")[0]
         if before_extension.count("_") == 1:
             self.user_id, self.log_number = before_extension.split("_")
         else:
             self.user_id, self.machine_id, self.log_number = \
                 before_extension.split("_")
         self.log_number = int(self.log_number)
     if filename.endswith(".bz2"):
         self.logfile = bz2.BZ2File(filename)
     else:
         self.logfile = file(filename)
     # For pre-2.0 logs, we need to hang on to the previous timestamp, as
     # this will be used as the time the card was shown, in order to
     # calculate the actual interval. (The timestamps for repetitions are
     # when the card was graded, not when it was presented to the user.)
     self.timestamp = None
     self.previous_timestamp = None
     self.lower_timestamp_limit = 1121021345  # 2005-07-10 21:49:05.
     self.upper_timestamp_limit = time.time()
     for line in self.logfile:
         if line.strip() == "":
             continue
         try:
             self._parse_line(line)
         except:
             print("Ignoring error in file '%s' while parsing line:\n%s" %
                   (filename, line))
             print traceback_string()
             sys.stdout.flush()
Esempio n. 3
0
 def run(self):
     data_dir = self.config().data_dir
     join = os.path.join
     dir = os.listdir(unicode(join(data_dir, "history")))
     # Find out which files haven't been uploaded yet.
     dir = os.listdir(unicode(join(data_dir, "history")))
     history_files = [x for x in dir if x.endswith(".bz2")]
     uploaded = None
     try:
         upload_log = file(join(data_dir, "history", "uploaded"))
         uploaded = [x.strip() for x in upload_log]
         upload_log.close()
     except:
         uploaded = []
     to_upload = set(history_files) - set(uploaded)
     if len(to_upload) == 0:
         return
     # Upload them to our server.
     upload_log = file(join(data_dir, "history", "uploaded"), 'a')
     try:
         for f in to_upload:
             print _("Uploading"), f, "...",
             filename = join(data_dir, "history", f)
             self.upload(filename)
             print >> upload_log, f
             print _("done!")
     except:
         print _("Upload failed")
         print traceback_string()
     upload_log.close()
Esempio n. 4
0
File: pickle.py Progetto: umax/pomni
 def save(self, path=None):
     # Don't erase a database which failed to load.
     if self.load_failed == True:
         return -1
     if not path:
         path = self.config()["path"]
     path = expand_path(path, self.config().basedir)
     # Update version.
     self.global_variables["version"] = self.version
     # Work around a sip bug: don't store card types, but their ids.
     for f in self.facts:
         f.card_type = f.card_type.id
     try:
         # Write to a backup file first, as shutting down Windows can
         # interrupt the dump command and corrupt the database.
         outfile = file(path + "~", 'wb')
         db = [self.tags, self.facts, self.cards,
               self.global_variables]
         cPickle.dump(db, outfile)
         outfile.close()
         shutil.move(path + "~", path) # Should be atomic.
     except:
         raise RuntimeError, _("Unable to save file.") \
               + "\n" + traceback_string()
     self.config()["path"] = contract_path(path, self.config().basedir)
     # Work around sip bug again.
     for f in self.facts:
         f.card_type = self.card_type_by_id(f.card_type)
Esempio n. 5
0
 def run(self):
     try:
         self.serve_until_stopped()
     except socket.error:
         self.show_error(_("Unable to start web server."))
     except Exception, e:
         self.show_error(str(e) + "\n" + traceback_string())
Esempio n. 6
0
 def run(self):
     data_dir = self.config().data_dir
     join = os.path.join
     # Find out which files haven't been uploaded yet.
     dir = os.listdir(join(data_dir, "history"))
     history_files = [x for x in dir if x.endswith(".bz2")]
     uploaded_filename = join(data_dir, "history", "uploaded")
     if os.path.exists(uploaded_filename):
         upload_log = open(uploaded_filename)
         uploaded = [x.strip() for x in upload_log]
         upload_log.close()
     else:
         uploaded = []
     to_upload = set(history_files) - set(uploaded)
     if len(to_upload) == 0:
         return
     # Upload them to our server.
     upload_log = open(join(data_dir, "history", "uploaded"), 'a')
     try:
         for f in to_upload:
             print(_("Uploading"), f, "...", end=' ')
             filename = join(data_dir, "history", f)
             self.upload(filename)
             print(f, file=upload_log)
             print(_("done!"))
     except:
         print(_("Upload failed"))
         print(traceback_string())
     upload_log.close()
Esempio n. 7
0
 def run(self):
     try:
         filename = self.pronouncer.download_tmp_audio_file(\
             self.card_type, self.foreign_text)
         self.finished_signal.emit(filename)
     except Exception as e:
         self.error_signal.emit(str(e) + "\n" + traceback_string())
Esempio n. 8
0
 def run(self):
     try:
         self.serve_until_stopped()
     except socket.error:
         self.show_error(_("Unable to start web server."))
     except Exception, e:
         self.show_error(str(e) + "\n" + traceback_string())
Esempio n. 9
0
 def run(self):
     try:
         translation = self.translator.translate(\
             self.card_type, self.foreign_text, self.target_language_id)
         self.finished_signal.emit(translation)
     except Exception as e:
         self.error_signal.emit(str(e) + "\n" + traceback_string())
Esempio n. 10
0
 def save(self):
     try:
         config_file = file(os.path.join(self.basedir,
                                         "config"), 'wb')
         cPickle.dump(dict(self), config_file)
     except:
         from mnemosyne.libmnemosyne.utils import traceback_string
         raise RuntimeError, _("Unable to save config file:") \
               + "\n" + traceback_string()
Esempio n. 11
0
 def install_plugin(self):
     filename = self.main_widget().get_filename_to_open(\
         self.config()["import_plugin_dir"],
         _("Plugins") + " " + "(*.plugin)", _("Install plugin"))
     if not filename:
         return ""
     self.config()["import_plugin_dir"] = os.path.dirname(filename)
     plugin_dir = os.path.join(self.config().data_dir, "plugins")
     import zipfile
     plugin_file = zipfile.ZipFile(filename, "r")
     # Filter out safety risks.
     filenames = [filename for filename in plugin_file.namelist() \
         if not filename.startswith("/") and not ".." in filename]
     # Find actual plugin.
     plugin_file.extractall(plugin_dir, filenames)
     import re
     re_plugin = re.compile(r""".*class (.+?)\(Plugin""",
                            re.DOTALL | re.IGNORECASE)
     plugin_filename, plugin_class_name = None, None
     for filename in filenames:
         if filename.endswith(".py"):
             text = open(os.path.join(plugin_dir, filename), "r").read()
             match = re_plugin.match(text)
             if match is not None:
                 plugin_filename = filename
                 plugin_class_name = match.group(1)
                 break
     if plugin_class_name is None:
         self.main_widget().show_error(_("No plugin found!"))
         return
     # Write manifest to allow uninstalling.
     manifest = open(
         os.path.join(plugin_dir, plugin_class_name + ".manifest"), "w")
     for filename in filenames:
         if not os.path.isdir(os.path.join(plugin_dir, filename)):
             print(filename, file=manifest)
     # Make sure we don't register a plugin twice.
     for plugin in self.plugins():
         if plugin.__class__.__name__ == plugin_class_name:
             return
     # Register plugin.
     try:
         module_name = plugin_filename[:-3]
         # Schedule module for reloading. Needed to catch the case of
         # deleting a plugin and then immediately reinstalling it.
         if module_name in sys.modules:
             del sys.modules[module_name]
         __import__(module_name)
     except Exception as e:
         print(e)
         from mnemosyne.libmnemosyne.utils import traceback_string
         msg = _("Error when running plugin:") \
             + "\n" + traceback_string()
         self.main_widget().show_error(msg)
         for plugin in self.plugins():
             if plugin.__class__.__name__ == plugin_class_name:
                 self.component_manager.unregister(plugin)
Esempio n. 12
0
 def install_plugin(self):
     filename = self.main_widget().get_filename_to_open(\
         self.config()["import_plugin_dir"],
         _("Plugins") + " " + "(*.plugin)", _("Install plugin"))
     if not filename:
         return ""
     self.config()["import_plugin_dir"] = os.path.dirname(filename)
     plugin_dir = os.path.join(self.config().data_dir, "plugins")
     import zipfile
     plugin_file = zipfile.ZipFile(filename, "r")
     # Filter out safety risks.
     filenames = [filename for filename in plugin_file.namelist() \
         if not filename.startswith("/") and not ".." in filename]
     # Find actual plugin.
     plugin_file.extractall(plugin_dir, filenames)
     import re
     re_plugin = re.compile(r""".*class (.+?)\(Plugin""",
         re.DOTALL | re.IGNORECASE)
     plugin_filename, plugin_class_name = None, None
     for filename in filenames:
         if filename.endswith(".py"):
             text = file(os.path.join(plugin_dir, filename), "r").read()
             match = re_plugin.match(text)
             if match is not None:
                 plugin_filename = filename
                 plugin_class_name = match.group(1)
                 break
     if plugin_class_name is None:
         self.main_widget().show_error(_("No plugin found!"))
         return
     # Write manifest to allow uninstalling.
     manifest = file(os.path.join(plugin_dir,
         plugin_class_name + ".manifest"), "w")
     for filename in filenames:
         if not os.path.isdir(os.path.join(plugin_dir, filename)):
             print >> manifest, filename
     # Make sure we don't register a plugin twice.
     for plugin in self.plugins():
         if plugin.__class__.__name__ == plugin_class_name:
             return
     # Register plugin.
     try:
         module_name = plugin_filename[:-3]
         # Schedule module for reloading. Needed to catch the case of
         # deleting a plugin and then immediately reinstalling it.
         if module_name in sys.modules:
             del sys.modules[module_name]
         __import__(module_name)
     except:
         from mnemosyne.libmnemosyne.utils import traceback_string
         msg = _("Error when running plugin:") \
             + "\n" + traceback_string()
         self.main_widget().show_error(msg)
         for plugin in self.plugins():
             if plugin.__class__.__name__ == plugin_class_name:
                 self.component_manager.unregister(plugin)
Esempio n. 13
0
 def run(self):
     try:
         translation = self.translator.translate(\
             self.card_type, self.foreign_text, self.target_language_id)
         if translation:
             self.finished_signal.emit(translation)
         else:
             self.error_signal.emit(
                 _("Could not contact Google servers..."))
     except Exception as e:
         self.error_signal.emit(str(e) + "\n" + traceback_string())
Esempio n. 14
0
 def load(self):
     try:
         config_file = file(os.path.join(self.basedir,
                                         "config"), 'rb')
         for key, value in cPickle.load(config_file).iteritems():
             self[key] = value
         self.set_defaults()
     except:
         from mnemosyne.libmnemosyne.utils import traceback_string
         raise RuntimeError, _("Error in config:") \
               + "\n" + traceback_string()
Esempio n. 15
0
 def execute(self, sql, *args):
     if self.DEBUG:
         print((sql, args))
         t = time.time()
     try:
         self._cursor = self.connection.execute(sql, *args)
     except:
         raise MnemosyneError("SQL error: " + sql + " " + str(*args) +
                              "\n" + traceback_string())
     if self.DEBUG:
         print(("took %.3f secs" % (time.time() - t)))
     return _Sqlite3Cursor(self._cursor)
Esempio n. 16
0
 def activate_saved_plugins(self):
     for plugin in self.config()["active_plugins"]:
         try:
             for p in self.plugins():
                 if plugin == p.__class__.__name__:
                     p.activate()
                     break
         except:
             from mnemosyne.libmnemosyne.translator import _
             msg = _("Error when running plugin:") \
                   + "\n" + traceback_string()
             self.main_widget().show_error(msg)
Esempio n. 17
0
 def activate_saved_plugins(self):
     for plugin in self.config()["active_plugins"]:
         try:
             for p in self.plugins():
                 if plugin == p.__class__.__name__:
                     p.activate()
                     break
         except:
             from mnemosyne.libmnemosyne.translator import _
             msg = _("Error when running plugin:") \
                   + "\n" + traceback_string()
             self.main_widget().show_error(msg)
Esempio n. 18
0
 def run(self):
     try:
         self.activate()
         self.serve_until_stopped()
     except socket.error:
         self.show_error(_("Unable to start web server."))
     except Exception as e:
         self.show_error(str(e) + "\n" + traceback_string())
     # Clean up after stopping.
     if self.server_has_connection:
         self.database().release_connection()
     self.server_has_connection = False
     if self in self.component_manager.components[None]["main_widget"]:
         self.component_manager.components[None]["main_widget"].pop()
     database_released.wakeAll()
Esempio n. 19
0
 def execute_user_plugin_dir(self):
     # Note that we put user plugins in the data_dir and not the
     # config_dir as there could be plugins (e.g. new card types) for
     # which the database does not make sense without them.
     plugin_dir = unicode(os.path.join(self.config().data_dir, "plugins"))
     sys.path.insert(0, plugin_dir)
     for component in os.listdir(unicode(plugin_dir)):
         if component.endswith(".py"):
             try:
                 __import__(component[:-3])
             except:
                 from mnemosyne.libmnemosyne.translator import _
                 msg = _("Error when running plugin:") \
                       + "\n" + traceback_string()
                 self.main_widget().show_error(msg)
Esempio n. 20
0
 def execute_user_plugin_dir(self):
     # Note that we put user plugins in the data_dir and not the
     # config_dir as there could be plugins (e.g. new card types) for
     # which the database does not make sense without them.
     plugin_dir = unicode(os.path.join(self.config().data_dir, "plugins"))
     sys.path.insert(0, plugin_dir)
     for component in os.listdir(unicode(plugin_dir)):
         if component.endswith(".py"):
             try:
                 __import__(component[:-3])
             except:
                 from mnemosyne.libmnemosyne.translator import _
                 msg = _("Error when running plugin:") \
                       + "\n" + traceback_string()
                 self.main_widget().show_error(msg)
Esempio n. 21
0
 def run(self):
     try:
         # Libmnemosyne itself could also generate dialog messages, so
         # we temporarily override the main_widget with the threaded
         # routines in this class.
         self.mnemosyne.component_manager.components\
             [None]["main_widget"].append(self)
         self.do_work()
     except Exception as e:
         self.show_error(str(e) + "\n" + traceback_string())
     finally:
         self.mnemosyne.database().release_connection()
         self.mnemosyne.component_manager.components\
             [None]["main_widget"].pop()
     self.work_ended_signal.emit()
Esempio n. 22
0
 def _activate_plugin_for_card_type(self, card_type_id):
     found = False
     for plugin in self.plugins():
         for component in plugin.components:
             if component.component_type == "card_type" and \
                 component.id == card_type_id:
                 found = True
                 try:
                     plugin.activate()
                 except:
                     raise RuntimeError, _("Error when running plugin:") \
                         + "\n" + traceback_string()
     if not found:
         raise RuntimeError, _("Missing plugin for card type with id:") \
             + " " + card_type_id
Esempio n. 23
0
 def handle(self):
     data = self.request[0].strip()
     socket = self.request[1]
     socket.connect(self.client_address)
     sys.stdout = sys.stderr = OutputCatcher(socket)
     # We use the component manager to store some more global data there.
     mnemosyne = self.server.mnemosyne
     mnemosyne.component_manager.socket = socket
     if data != "exit()":
         try:
             exec(data)
         except:
             socket.sendall("__EXCEPTION__\n" + traceback_string())
     else:
         self.server.stopped = True
     socket.sendall("__DONE__\n")
Esempio n. 24
0
 def load_user_config(self):
     sys.path.insert(0, self.config_dir)
     config_file_c = os.path.join(self.config_dir, "config.pyc")
     if os.path.exists(config_file_c):
         os.remove(config_file_c)
     config_file = os.path.join(self.config_dir, "config.py")
     if os.path.exists(config_file):
         try:
             import config
             config = reload(config)
             for var in dir(config):
                 if var in self:
                     self[var] = getattr(config, var)
         except:
             raise RuntimeError, _("Error in config.py:") \
                       + "\n" + traceback_string()
Esempio n. 25
0
 def load_user_config(self):
     sys.path.insert(0, self.config_dir)
     config_file_c = os.path.join(self.config_dir, "config.pyc")
     if os.path.exists(config_file_c):
         os.remove(config_file_c)
     config_file = os.path.join(self.config_dir, "config.py")
     if os.path.exists(config_file):
         try:
             import config
             config = reload(config)
             for var in dir(config):
                 if var in self:
                     self[var] = getattr(config, var)
         except:
             raise RuntimeError, _("Error in config.py:") \
                       + "\n" + traceback_string()
Esempio n. 26
0
 def handle(self):
     data = self.request[0].strip()
     socket = self.request[1]
     socket.connect(self.client_address)
     sys.stdout = sys.stderr = OutputCatcher(socket)
     # We use the component manager to store some more global data there.
     mnemosyne = self.server.mnemosyne
     mnemosyne.component_manager.socket = socket
     if data != "exit()":
         try:
             exec(data)
         except:
             socket.sendall("__EXCEPTION__\n" + traceback_string())
     else:
         self.server.stopped = True
     socket.sendall("__DONE__\n")
Esempio n. 27
0
 def execute_user_plugin_dir(self):
     # Coming from 1.x, upgrade the config and don't run the old plugins.
     if self.config()["path"].endswith(".mem"):
         from mnemosyne.libmnemosyne.upgrades.upgrade_config \
              import UpgradeConfig
         UpgradeConfig(self.component_manager).run()
         return
     # Else, proceed nomally. 
     basedir = self.config().basedir
     plugindir = unicode(os.path.join(basedir, "plugins"))
     sys.path.insert(0, plugindir)
     for component in os.listdir(unicode(plugindir)):
         if component.endswith(".py"):
             try:
                 __import__(component[:-3])
             except:
                 from mnemosyne.libmnemosyne.translator import _
                 msg = _("Error when running plugin:") \
                       + "\n" + traceback_string()
                 self.main_widget().error_box(msg)
Esempio n. 28
0
 def load_user_config(self):
     sys.path.insert(0, self.basedir)
     config_file_c = os.path.join(self.basedir, "config.pyc")
     if os.path.exists(config_file_c):
         os.remove(config_file_c)
     config_file = os.path.join(self.basedir, "config.py")
     if os.path.exists(config_file):
         try:
             import config as user_config
             for var in dir(user_config):
                 if var in self:
                     self[var] = getattr(user_config, var)
         except:
             # Work around the unexplained fact that config.py cannot get 
             # imported right after it has been created.
             if self["first_run"] == True:
                 pass
             else:
                 raise RuntimeError, _("Error in config.py:") \
                       + "\n" + traceback_string()
Esempio n. 29
0
 def run(self):
     try:
         self.serve_until_stopped()
     except socket.error:
         self.show_error(_("Unable to start sync server."))
     except Exception as e:
         self.show_error(str(e) + "\n" + traceback_string())
     # Clean up after stopping.
     mutex.lock()
     server_hanging = (len(self.sessions) != 0)
     mutex.unlock()
     if server_hanging:
         if not self.server_has_connection:
             mutex.lock()
             database_released.wait(mutex)
             mutex.unlock()
         self.terminate_all_sessions()  # Does its own locking.
         self.database().release_connection()
         self.server_has_connection = False
         if self in self.component_manager.components[None]["main_widget"]:
             self.component_manager.components[None]["main_widget"].pop()
     database_released.wakeAll()
Esempio n. 30
0
File: pickle.py Progetto: umax/pomni
    def load(self, path):
        if self.is_loaded():
            self.unload()
        path = expand_path(path, self.config().basedir)
        if not os.path.exists(path):
            self.load_failed = True
            raise RuntimeError, _("File does not exist.")
        try:
            infile = file(path, 'rb')
            db = cPickle.load(infile)
            self.tags = db[0]
            self.facts = db[1]
            self.cards = db[2]
            self.global_variables = db[3]
            infile.close()
            self.load_failed = False
        except:
            self.load_failed = True
            raise RuntimeError, _("Invalid file format.") \
                  + "\n" + traceback_string()

        # Check database version.
        if self.global_variables["version"] != self.version:
            self.load_failed = True
            raise RuntimeError, _("Unable to load file: database version mismatch.")
        
        # Deal with clones and plugins, also plugins for parent classes.
        # Because of the sip bugs, card types here are actually still card
        # type ids.
        plugin_needed = set()
        clone_needed = []
        active_id = set(card_type.id for card_type in self.card_types())
        for id in set(card.fact.card_type for card in self.cards):
            while "." in id: # Move up one level of the hierarchy.
                id, child_name = id.rsplit(".", 1)          
                if id.endswith("_CLONED"):
                    id = id.replace("_CLONED", "")
                    clone_needed.append((id, child_name))
                if id not in active_id:
                    plugin_needed.add(id)
            if id not in active_id:
                plugin_needed.add(id)

        # Activate necessary plugins.
        for card_type_id in plugin_needed:
            found = False
            for plugin in self.plugins():
                for component in plugin.components:
                    if component.component_type == "card_type" and \
                           component.id == card_type_id:
                        found = True
                        try:
                            plugin.activate()
                        except:
                            self.load_failed = True
                            raise RuntimeError, \
                                  _("Error when running plugin:") \
                                  + "\n" + traceback_string()
            if not found:
                self.load_failed = True
                raise RuntimeError, \
                      _("Missing plugin for card type with id:") \
                      + " " + card_type_id
            
        # Create necessary clones.
        for parent_type_id, clone_name in clone_needed:
            parent_instance = self.card_type_by_id(parent_type_id)
            try:
                parent_instance.clone(clone_name)
            except NameError:
                # In this case the clone was already created by loading the
                # database earlier.
                pass
            
        # Work around a sip bug: don't store card types, but their ids.
        for f in self.facts:
            f.card_type = self.card_type_by_id(f.card_type)    
        self.config()["path"] = contract_path(path, self.config().basedir)
        for f in self.component_manager.get_all("hook", "after_load"):
            f.run()