Ejemplo n.º 1
0
def enable_loggers(logs):
    assert isinstance(logs, list)
    log_level = logging.WARNING
    if logs:
        for l in constants.log_namespaces:
            if l not in logs:
                hlogger.Logger(l).setLevel(log_level)
                if l in constants.log_ns_core:
                    hlogger.Logger("apscheduler").setLevel(log_level)
                    hlogger.Logger("PIL").setLevel(log_level)
                elif l in constants.log_ns_database:
                    hlogger.Logger("sqlalchemy").setLevel(log_level)
                    hlogger.Logger("sqlalchemy.pool").setLevel(log_level)
                    hlogger.Logger("sqlalchemy.engine").setLevel(log_level)
                    hlogger.Logger("sqlalchemy.orm").setLevel(log_level)
                    hlogger.Logger("alembic").setLevel(log_level)
                elif l in constants.log_ns_client:
                    hlogger.Logger("geventwebsocket").setLevel(log_level)
                elif l in constants.log_ns_network:
                    hlogger.Logger("cachecontrol").setLevel(log_level)
Ejemplo n.º 2
0
def get_plugin_logger(plugin_name, plugin_dir):
    "Create a logger for plugin"
    file_name = os.path.join(plugin_dir, "plugin.log")
    file_mode = "a"
    file_enc = 'utf-8'

    try:
        with open(file_name, 'x', encoding=file_enc) as f:  # noqa: F841
            pass
    except FileExistsError:
        pass

    l = hlogger.Logger('HPX Plugin.' + plugin_name)
    l._logger.propagate = False
    l._logger.setLevel(logging.INFO)
    fhandler = logging.FileHandler(file_name, file_mode, file_enc)
    l._logger.addHandler(fhandler)
    return l
Ejemplo n.º 3
0
"""
import typing

from collections import namedtuple
from sqlalchemy.sql.expression import func
from sqlalchemy_utils.functions import make_order_by_deterministic

from happypanda.common import utils, hlogger, exceptions, constants, config
from happypanda.core.command import Command, CommandEvent, AsyncCommand, CommandEntry, CParam
from happypanda.core.commands import io_cmd
from happypanda.core import db, async_utils, db_cache
from happypanda.interface import enums
from happypanda.interface.enums import ItemSort

log = hlogger.Logger(constants.log_ns_command + __name__)


class GetModelImage(AsyncCommand):
    """
    Fetch a database model item's image

    By default, the following models are supported

    - Gallery
    - Page
    - Grouping
    - Collection
    - GalleryFilter

    Returns a Profile database item
Ejemplo n.º 4
0
import errno
import collections
import langcodes

from inspect import ismodule, currentframe, getframeinfo
from contextlib import contextmanager
from collections import namedtuple
from gevent import ssl

from happypanda.common import constants, exceptions, hlogger, config
try:
    import winreg
except ImportError:  # only available on windows
    pass

log = hlogger.Logger(constants.log_ns_misc + __name__)

ImageSize = namedtuple("ImageSize", ['width', 'height'])

temp_dirs = []

i18n.load_path.append(constants.dir_translations)
i18n.set("file_format", "yaml")
i18n.set("filename_format", "{locale}.{namespace}.{format}")
i18n.set("error_on_missing_translation", True)


def setup_i18n():
    i18n.set("locale", config.translation_locale.value)
    i18n.set("fallback", "en_us")
Ejemplo n.º 5
0
"Contains classes/functions used to encapsulate message structures"

import enum
import json
import inspect
import arrow
import os

from datetime import datetime

from happypanda.common import constants, exceptions, utils, hlogger
from happypanda.core import db
from happypanda.core.commands import io_cmd

log = hlogger.Logger(__name__)


def finalize(msg_dict, session_id="", name=constants.server_name, error=None):
    "Finalize dict message before sending"
    enc = 'utf-8'
    msg = {
        'session': session_id,
        'name': name,
        'data': msg_dict,
    }

    if error:
        msg['error'] = error

    return bytes(json.dumps(msg), enc)
Ejemplo n.º 6
0
import os
import yaml
import gevent
import random
import rarfile

from enum import Enum
from contextlib import contextmanager
from collections import ChainMap, OrderedDict

from happypanda.common import exceptions, hlogger, constants

log = hlogger.Logger(constants.log_ns_core + __name__, process="MainProcess")


class ConfigIsolation(Enum):
    server = "server"
    client = "client"


class ConfigNode:

    default_namespaces = set()

    _cfg_nodes = {}

    def __init__(self,
                 cfg,
                 ns,
                 name,
                 value,
Ejemplo n.º 7
0
import os
import sys
import shutil
import atexit
import subprocess

from happypanda.common import constants, hlogger, clsutils, utils, config

# OS X: fix the working directory when running a mac app
# OS X: files are in [app]/Contents/MacOS/
if sys.platform.startswith('darwin') and hasattr(sys, 'frozen'):
    os.chdir(os.path.abspath(os.path.dirname(sys.executable)))

log = hlogger.Logger("HappyUpdater")


def move_replace(root_src_dir, root_dst_dir):
    for src_dir, dirs, files in os.walk(root_src_dir):
        dst_dir = src_dir.replace(root_src_dir, root_dst_dir, 1)
        if not os.path.exists(dst_dir):
            os.makedirs(dst_dir)
        for file_ in files:
            src_file = os.path.join(src_dir, file_)
            dst_file = os.path.join(dst_dir, file_)
            if os.path.exists(dst_file):
                os.remove(dst_file)
            shutil.move(src_file, dst_dir)


def launch_app(*args, **kwargs):
    try:
Ejemplo n.º 8
0
"Contains classes/functions used to encapsulate message structures"

import enum
import inspect
import arrow
import os
import itertools
import multidict

from datetime import datetime

from happypanda.common import constants, exceptions, utils, hlogger, config
from happypanda.core import db, plugins
from happypanda.core.commands import io_cmd

log = hlogger.Logger(constants.log_ns_server + __name__)


def finalize(msg_dict, session_id="", name=None, error=None):
    "Finalize dict message before sending"
    enc = 'utf-8'
    msg = {
        'session': session_id,
        'name': name if name else config.server_name.value,
        'data': msg_dict,
    }

    if error:
        msg['error'] = error

    return bytes(utils.json_dumps(msg), enc)
Ejemplo n.º 9
0
import socket
import sys
import errno
import gzip

from happypanda.common import constants, exceptions, utils, hlogger, config
from happypanda.core import message

log = hlogger.Logger(constants.log_ns_client + __name__)


class Client:
    """A common wrapper for communicating with server.

    Params:
        name -- name of client
    """
    def __init__(self, name, session_id="", client_id=""):
        self.id = client_id
        self.name = name
        # HACK: properly fix this
        self._server = utils.get_qualified_name(config.host.value,
                                                config.port.value).split(':')
        self._server = (self._server[0], int(self._server[1]))
        if config.enable_ssl.value is True or config.enable_ssl.value == "server":
            self._context = utils.create_ssl_context(webserver=True)
            self._sock = self._context.wrap_socket(
                socket.socket(socket.AF_INET, socket.SOCK_STREAM))
        else:
            self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._alive = False
Ejemplo n.º 10
0
.. autodata:: happypanda.core.commands.search_cmd.Term
    :annotation: = NamedTuple

"""
import typing

from collections import namedtuple, ChainMap

from happypanda.common import hlogger, exceptions, utils, constants, config
from happypanda.core.command import Command, CommandEvent, CommandEntry, CParam
from happypanda.core.commands import database_cmd
from happypanda.core import db


log = hlogger.Logger(constants.log_ns_search + __name__)


def _get_search_options():
    return {
        config.search_option_all: 'all',
        config.search_option_case: 'case',
        config.search_option_desc: 'desc',
        config.search_option_regex: 'regex',
        config.search_option_whole: 'whole',
    }


def get_search_options(options={}):
    d_val = {}
    u_val = {}
Ejemplo n.º 11
0
import sys
import gevent
import weakref
import collections
import arrow
import psycopg2
import functools

from psycopg2 import extensions
from gevent.socket import wait_read, wait_write
from gevent import monkey

from happypanda.common import hlogger, utils, constants
from happypanda.core import db

log = hlogger.Logger(constants.log_ns_core + __name__)
Thread = monkey.get_original("threading", "Thread")
local = monkey.get_original("threading", "local")


class Greenlet(gevent.Greenlet):
    '''
    A subclass of gevent.Greenlet which adds additional members:
     - locals: a dict of variables that are local to the "spawn tree" of
       greenlets
     - spawner: a weak-reference back to the spawner of the
       greenlet
     - stacks: a record of the stack at which the greenlet was
       spawned, and ancestors
    '''
    def __init__(self, f, *a, **kw):
Ejemplo n.º 12
0
"""

import attr
import requests
import cachecontrol
import os
import arrow
import typing

from happypanda.common import (hlogger, exceptions, constants, config)
from happypanda.core.command import CoreCommand, Command
from happypanda.core.commands import io_cmd
from happypanda.interface import enums

log = hlogger.Logger(constants.log_ns_network + __name__)


class Method(enums._APIEnum):

    #: GET
    GET = "get"
    #: POST
    POST = "post"
    #: PUT
    PUT = "put"
    #: DELETE
    DELETE = "delete"
    #: HEAD
    HEAD = "head"
    #: OPTIONS