def __init__(self): self.log = logging.getLogger(__name__) path = files.in_here("commoncolours.json") self.log.info("Reading common HTML colours from %s", path) with open(path) as fp: obj = json.load(fp) obj = {n.lower(): v.lower() for n, v in obj.items()} assert isinstance(obj, dict) # Read the dulux colours in, but don't overwrite the HTML ones. path = files.in_here("duluxcolours.txt") self.log.info("Reading the DULUX colour guide from %s", path) with open(path) as fp: for line in fp.readlines(): line = line.strip() if not line or line.startswith("#"): continue shade, _, rest = line.partition(" ") rest, _, lrv = rest.rpartition(" ") rest, _, b = rest.rpartition(" ") rest, _, g = rest.rpartition(" ") name, _, r = rest.rpartition(" ") alt_name = name.translate({c: "" for c in string.punctuation}) hex_str = to_hex(int(r), int(g), int(b), prefix="") shade = shade.lower() hex_str = hex_str.lower() name = name.lower() if shade not in obj: obj[shade] = hex_str if name not in obj: obj[name] = hex_str if alt_name not in obj: obj[alt_name] = hex_str # Do this to remove case sensitivity. self.__data = {n: from_hex(c) for n, c in obj.items()} # Reverse the list for reverse lookups. self.__reversed = {v: k for k, v in self.items()} self.log.info("Loaded %s colours!", len(self.__data))
def get_from_here(file_name, *, nested_by=0, load_now=False): """ Constructs a ConfigFile relative to the current directory the caller is defined in, and caches the data. :param file_name: the file name to open. :param nested_by: how many stack frames we are nested by. Only use this if you are implementing this routine as part of another routine. :param load_now: true if we should immediately cache. Defaults to False. :return: the config file object. """ path = files.in_here(file_name, nested_by=1 + nested_by) cf = ConfigFile(path) if load_now: cf.sync_get() return cf
def __init__(self, map_image: image.Image = None): """ Creates a mercator projection from the given Image object. This assumes that 0E,0N is at the central pixel. If no image is given, the default mercator bitmap is used. """ if map_image is None: map_image = image.open(files.in_here("mercator-small.png")) self.image = map_image self.ox, self.oy = map_image.width / 2, map_image.height / 2 # Differential of X in pixels per degree self.dx = map_image.width / 360 # Differential of Y in pixels per degree self.dy = map_image.height / 180
https://docs.google.com/document/d/18md3rLdgD9f5Wro3i7YYopJBFb_6MPCO8-0ihtxHoyM """ import aiofiles import aiohttp import neko3.cog from neko3 import files from neko3 import logging_utils from neko3 import singleton from .api import * # Thank asottile for this! _asottile_base = "https://raw.githubusercontent.com/asottile" _ffstring_url = _asottile_base + "/future-fstrings/master/future_fstrings.py" _trt_url = _asottile_base + "/tokenize-rt/master/tokenize_rt.py" _replify_path = files.in_here("replify.py") class GlobalResourceManager(logging_utils.Loggable, metaclass=singleton.SingletonMeta): def __init__(self): self._ffstrings = None self._trt = None self._replify = None async def obtain_ffstrings(self): if self._ffstrings is None: async with aiohttp.ClientSession() as session: async with session.get(_ffstring_url) as resp: self.logger.info("Fetching %s", _ffstring_url) resp.raise_for_status() self._ffstrings = await resp.text()
import os import random import aiofiles import discord from discord.ext import commands from neko3 import configuration_files from neko3 import files from neko3 import fuzzy_search from neko3 import logging_utils from neko3 import neko_commands # Relative to this directory. bindings_file = "bindings" images_directory = files.in_here("images") class MewReactsCog(neko_commands.Cog, logging_utils.Loggable): """Reactions cog.""" def __init__(self): bindings = configuration_files.get_from_here(bindings_file).sync_get() # Attempt to locate all files to ensure paths are valid. potential_targets = set() for im_list in bindings.values(): [potential_targets.add(im) for im in im_list] targets_to_path = {} for target in potential_targets: