Exemple #1
0
 async def __aenter__(self):
     await self.__lock.acquire()
     self._raw_value = await self
     if not isinstance(self._raw_value, (dict, list)):
         raise errors.ConfigIllegalOperation('Context manager value must be mutable.')
     self.__original_value = deepcopy(self._raw_value)
     return self._raw_value
Exemple #2
0
 def guild(self, source: Union[int, discord.Guild, commands.Context]) -> Group:
     if isinstance(source, commands.Context):
         if not source.guild:
             raise errors.ConfigIllegalOperation('The context object does not have an associated guild.')
         source = source.guild.id
     elif isinstance(source, discord.Guild):
         source = source.id
     return self._get_category(self.GUILD, str(source))
Exemple #3
0
 def voicechannel(self, source: Union[int, discord.VoiceChannel, discord.Member]) -> Group:
     if isinstance(source, discord.Member):
         if source.voice.channel:
             source = source.voice.channel.id
         else:
             raise errors.ConfigIllegalOperation('The member is not in a voice channel.')
     elif isinstance(source, discord.VoiceChannel):
         source = source.id
     return self._get_category(self.VOICECHANNEL, str(source))
Exemple #4
0
 async def _set(self, *path: str, value: Any):
     partial = self._data
     value_copy = json.loads(json.dumps(value))
     async with self._lock:
         try:
             for p in path[:-1]:
                 partial = partial.setdefault(p, {})
             partial[path[-1]] = value_copy
             await self._save()
         except AttributeError:
             raise errors.ConfigIllegalOperation(f'Failed to change {self.cog_name}.{".".join(path)}".')
Exemple #5
0
    def __call__(cls, cog_name: str):
        if cog_name is None:
            raise errors.ConfigIllegalOperation(
                'You must provide a cog name when instantiating or getting a config.')

        if cog_name in cls._config_cache:
            cls._cache_log.debug(f'Found cached Config object for cog `{cog_name}`')
            return cls._config_cache[cog_name]

        cls._cache_log.debug(f'Creating new Config instance for cog `{cog_name}`')
        inst = super(_ConfigMeta, cls).__call__(cog_name)
        cls._config_cache[cog_name] = inst
        return inst
Exemple #6
0
 def custom(self, name: str) -> Group:
     if name in (self.GLOBAL, self.GUILD, self.TEXTCHANNEL):
         raise errors.ConfigIllegalOperation('Use the correct method to get a Group.')
     return self._get_category(name)
Exemple #7
0
 def register_custom(self, name: str, **values):
     if name in (self.GLOBAL, self.GUILD, self.TEXTCHANNEL, self.VOICECHANNEL, self.ROLE):
         raise errors.ConfigIllegalOperation('Use the correct method to register default values.')
     self._register_defaults(name, **values)
Exemple #8
0
 def get_config(cls, cog_name: str = None, cog_instance: commands.Cog = None) -> "Config":
     if cog_name is None:
         if cog_instance is None:
             raise errors.ConfigIllegalOperation('You must provide either a cog name or instance.')
         cog_name = type(cog_instance).__name__
     return cls(cog_name.lower())
Exemple #9
0
 async def set(self, value: Dict[str, Any]):
     if not isinstance(value, dict):
         raise errors.ConfigIllegalOperation('Failed to set value of a group to a non-dictionary object.')
     await super(Group, self).set(value)