Ejemplo n.º 1
0
    def test_usage(self):
        const = typeutils.constants(
            Id="TestConstants",
            Name="TestConstants",
            Function="test_usage",
        )

        self.assertIsInstance(const, typeutils.Constants)
        self.assertEqual(const.Id, "TestConstants")  # pylint: disable=E1101
        self.assertEqual(const.Name, "TestConstants")  # pylint: disable=E1101
        self.assertSetEqual(set(const["TestConstants"]), set(["Id", "Name"]))
        self.assertEqual(const.Function, "test_usage")  # pylint: disable=E1101
        self.assertTupleEqual(const["test_usage"], ("Function",))  # pylint: unsubscriptable-object
        self.assertIsNone(const["noting"])

        with self.assertRaisesRegexp(
            AttributeError,
            "attribute Name is not writable",
        ):
            const.Name = "error"

        with self.assertRaisesRegexp(
            AttributeError,
            "attribute Function is not writable",
        ):
            const.Function = "error"

        self.assertDictEqual(dict(const), {
            "Id": "TestConstants",
            "Name": "TestConstants",
            "Function": "test_usage",
        })
Ejemplo n.º 2
0
from textwrap import dedent
import logging
from subprocess import CalledProcessError

from ycyc.base.adapter import main_entry
from ycyc.base.resources import Regex
from ycyc.base.filetools import cd
from ycyc.base.shelltools import ShellCommands
from ycyc.base.typeutils import constants
from ycyc.base.logutils import console_only_config

console_only_config()
logger = logging.getLogger(__name__)
ErrorNo = constants(
    UserErr=-1,
    EnvErr=-2,
    SysErr=-3,
    UnknownErr=-4,
)


class Validater(object):
    EmailRex = re.compile("^%s$" % Regex.email_addr())

    @classmethod
    def is_avaliable_email(cls, email):
        return cls.EmailRex.search(email) is not None

    @classmethod
    def is_avaliable_repo(cls, path):
        if not path:
            return False
Ejemplo n.º 3
0
from django.db import models
from django.core import validators
from django.utils.translation import ugettext_lazy as _

from ycyc.base.typeutils import constants


BASE_VALIDATORS = constants(
    safety_string=validators.RegexValidator(
        r"^[^\[\]\(\)\<\>=\"\',:]+$",
        _("Not allow match in: []()<>,\'\":"),
        _("Invalid string"),
    ),
)


class BaseModel(models.Model):
    created_on = models.DateTimeField(
        auto_now_add=True,
        verbose_name=_("Created on"),
    )
    updated_on = models.DateTimeField(
        auto_now=True,
        auto_now_add=True,
        verbose_name=_("Updated on"),
    )

    def __repr__(self):
        return "<{type}: id={id}>".format(
            type=self.__class__.__name__,
            id=self.id,
Ejemplo n.º 4
0
            type_ = type_ or model.__name__
            if type_ in cls.task_mappings:
                raise TaskConflictError(type_)
            cls.task_mappings[type_] = model
            return model

        return register_wrapper

    @classmethod
    def get_task_model(cls, type_):
        if type_ not in cls.task_mappings:
            raise TaskTypeNotFound(type_)
        return cls.task_mappings.get(type_)


TaskStatus = constants(enabled="enabled", disabled="disabled")


class TaskBase(models.Model):
    update_at = models.DateTimeField(auto_now=True)
    type = models.CharField(max_length=64)
    params = models.CharField(max_length=128)
    status = models.CharField(max_length=16)

    class Meta:
        abstract = True

    def is_ready(self, context):
        raise NotImplementedError()

    def make_request(self):