예제 #1
0
    def _addonMeta():
        """Get meta dictionary

        Reads in meta.json in add-on folder and returns
        resulting dictionary of user-defined metadata values.

        Note:
            Anki 2.1 stores both add-on meta data and customized
            settings in meta.json. In this module we are only dealing
            with the settings part.

        Returns:
            dict: config dictionary

        """

        try:
            meta = json.load(io.open(meta_path, encoding="utf-8"))
        except (IOError, OSError):
            meta = None
        except ValueError as e:
            print("Could not read meta.json: " + str(e))
            meta = None

        if not meta:
            meta = {"config": _addonConfigDefaults()}
            _writeAddonMeta(meta)

        return meta
예제 #2
0
 def load(self) :
     fp = Storage(self.word).getPath() + self.word + '.cdt'
     if os.path.exists(fp) :
         with open(fp, 'r') as f:
             self.data = json.load(f)
         return True
     else :
         return False
def load_prefs(self):
    # load local preferences
    self.prefs = None
    try:
        with open(prefs_path, "r") as f:
            self.prefs = json.load(f)
    except:
        # file does not exist or is corrupted: fall back to default
        with open(prefs_path, "w") as f:
            self.prefs = default_prefs
            json.dump(self.prefs, f)
예제 #4
0
def load_prefs(self):
    # load local preferences
    self.prefs = None
    try:
        with open(prefs_path, "r") as f:
            self.prefs = json.load(f)
    except:
        # file does not exist or is corrupted: fall back to default
        with open(prefs_path, "w") as f:
            self.prefs = default_prefs
            json.dump(self.prefs, f)
예제 #5
0
 def load(self) :
     fp = Storage(self.word).getPath() + self.word + '.ydp'
     if os.path.exists(fp) :
         arr_data = []
         with open(fp, 'r') as f:
             arr_data = json.load(f)
         for st in arr_data :
             e = Entity()
             e.loads(st)
             self.data.append(e)   
         return True
     else :
         return False
    def get_preferences():
        """
        Load the current preferences from disk. If no preferences file is
        found, or if it is corrupted, return the default preferences.
        """
        prefs = None
        try:
            with codecs.open(PrefHelper.get_preference_path(), encoding="utf8") as f:
                prefs = json.load(f)
        except:
            prefs = PrefHelper.get_default_preferences()
        else:
            prefs = PrefHelper.normalize_user_prefs(
                        PrefHelper.get_default_preferences(), prefs)

        return prefs
예제 #7
0
 def readConfig(self):
     """Parse user-supplied config file or fall back to defaults"""
     # start with default config and work from there:
     config = copy.deepcopy(default_config)
     if anki21:  # use Anki 2.1's inbuilt config management when available
         user_config = mw.addonManager.getConfig(__name__)
     else:
         addon_path = os.path.dirname(__file__).decode(sys_encoding)
         config_path = os.path.join(addon_path, "config.json")
         try:
             user_config = json.load(io.open(config_path, encoding="utf-8"))
         except (IOError, OSError, json.decoder.JSONDecodeError):
             user_config = {}
     if not user_config:
         self.writeConfig(default_config)
     config.update(user_config)
     self.config = config
예제 #8
0
    def load(self) :
        fp = Storage(self.word).getPath() + self.word + '.mtp'
        if os.path.exists(fp) :
            arr_data_all = []

            with open(fp, 'r') as f:
                arr_data_all = json.load(f)

            self.stars = json.loads(arr_data_all[0])
            arr_data = json.loads(arr_data_all[1])

            for st in arr_data :
                e = EntityEx()
                e.loads(st)
                self.data.append(e)   
            return True
        else :
            return False
예제 #9
0
    def _addonConfigDefaults():
        """Get default config dictionary
        Reads in config.json in add-on folder and returns
        resulting dictionary of default config values.
        Returns:
            dict: config dictionary
        Raises:
            Exception: If config.json cannot be parsed correctly.
                (The assumption being that we would end up in an
                inconsistent state if we were to return an empty
                config dictionary. This should never happen.)
        """

        try:
            return json.load(io.open(defaults_path, encoding="utf-8"))
        except (IOError, OSError, json.decoder.JSONDecodeError) as e:
            print("Could not read config.json: " + str(e))
            raise Exception("Config file could not be read: " + str(e))
예제 #10
0
    def getEntries(self, file, audioDirectory):
        if self.entries == None:
            entries = None

            try:
                with open(file) as data:
                    entries = json.load(data)
            except:
                raise EntriesParseException(file)

            entries = self.validateEntries(entries, audioDirectory)

            if entries == None:
                raise InvalidEntriesException(file)

            self.entries = entries

        return self.entries
예제 #11
0
    def _addonConfigDefaults20(self):
        """Get default config dictionary

        Reads in config.json in add-on folder and returns
        resulting dictionary of default config values.

        Returns:
            dict: config dictionary

        Raises:
            ConfigError: If config.json cannot be parsed correctly.
                (The assumption being that we would end up in an
                inconsistent state if we were to return an empty
                config dictionary. This should never happen.)

        """
        try:
            return json.load(io.open(DEFAULT_LOCAL_CONFIG_PATH,
                                     encoding="utf-8"))
        except (IOError, OSError, ValueError) as e:
            raise ConfigError("Config file could not be read: " + str(e))