update & fix
This commit is contained in:
		@ -1,58 +0,0 @@
 | 
				
			|||||||
import datetime
 | 
					 | 
				
			||||||
import logging
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from django.db import transaction
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from aircox.models import Diffusion, Schedule
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
logger = logging.getLogger("aircox.commands")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
__all__ = ("Diffusions",)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class Diffusions:
 | 
					 | 
				
			||||||
    """Handle generation and update of Diffusion instances."""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    date = None
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def __init__(self, date):
 | 
					 | 
				
			||||||
        self.date = date or datetime.date.today()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def update(self):
 | 
					 | 
				
			||||||
        episodes, diffusions = [], []
 | 
					 | 
				
			||||||
        for schedule in Schedule.objects.filter(
 | 
					 | 
				
			||||||
            program__active=True, initial__isnull=True
 | 
					 | 
				
			||||||
        ):
 | 
					 | 
				
			||||||
            eps, diffs = schedule.diffusions_of_month(self.date)
 | 
					 | 
				
			||||||
            if eps:
 | 
					 | 
				
			||||||
                episodes += eps
 | 
					 | 
				
			||||||
            if diffs:
 | 
					 | 
				
			||||||
                diffusions += diffs
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            logger.info(
 | 
					 | 
				
			||||||
                "[update] %s: %d episodes, %d diffusions and reruns",
 | 
					 | 
				
			||||||
                str(schedule),
 | 
					 | 
				
			||||||
                len(eps),
 | 
					 | 
				
			||||||
                len(diffs),
 | 
					 | 
				
			||||||
            )
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        with transaction.atomic():
 | 
					 | 
				
			||||||
            logger.info(
 | 
					 | 
				
			||||||
                "[update] save %d episodes and %d diffusions",
 | 
					 | 
				
			||||||
                len(episodes),
 | 
					 | 
				
			||||||
                len(diffusions),
 | 
					 | 
				
			||||||
            )
 | 
					 | 
				
			||||||
            for episode in episodes:
 | 
					 | 
				
			||||||
                episode.save()
 | 
					 | 
				
			||||||
            for diffusion in diffusions:
 | 
					 | 
				
			||||||
                # force episode id's update
 | 
					 | 
				
			||||||
                diffusion.episode = diffusion.episode
 | 
					 | 
				
			||||||
                diffusion.save()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def clean(self):
 | 
					 | 
				
			||||||
        qs = Diffusion.objects.filter(
 | 
					 | 
				
			||||||
            type=Diffusion.TYPE_UNCONFIRMED, start__lt=self.date
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        logger.info("[clean] %d diffusions will be removed", qs.count())
 | 
					 | 
				
			||||||
        qs.delete()
 | 
					 | 
				
			||||||
@ -86,7 +86,7 @@ class WrapperMixin:
 | 
				
			|||||||
        ns_target = getattr(ns, ns_attr, None)
 | 
					        ns_target = getattr(ns, ns_attr, None)
 | 
				
			||||||
        if self.target is ns_target:
 | 
					        if self.target is ns_target:
 | 
				
			||||||
            return
 | 
					            return
 | 
				
			||||||
        elif self.target is not None:
 | 
					        elif self.target is not None and self.ns:
 | 
				
			||||||
            raise RuntimeError(
 | 
					            raise RuntimeError(
 | 
				
			||||||
                "self target already injected. It must be "
 | 
					                "self target already injected. It must be "
 | 
				
			||||||
                "`release` before `inject`."
 | 
					                "`release` before `inject`."
 | 
				
			||||||
@ -97,12 +97,14 @@ class WrapperMixin:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        self.ns = ns
 | 
					        self.ns = ns
 | 
				
			||||||
        self.ns_attr = ns_attr
 | 
					        self.ns_attr = ns_attr
 | 
				
			||||||
 | 
					        return self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def release(self):
 | 
					    def release(self):
 | 
				
			||||||
        """Remove injection from previously injected parent, reset target."""
 | 
					        """Remove injection from previously injected parent, reset target."""
 | 
				
			||||||
        if self.ns_target is self.interface:
 | 
					        if self.ns_target is self.interface:
 | 
				
			||||||
            setattr(self.ns, self.ns_attr, self.target)
 | 
					            setattr(self.ns, self.ns_attr, self.target)
 | 
				
			||||||
        self.target = None
 | 
					        self.target = None
 | 
				
			||||||
 | 
					        return self
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class SpoofMixin:
 | 
					class SpoofMixin:
 | 
				
			||||||
@ -190,6 +192,16 @@ class Interface:
 | 
				
			|||||||
            self.interface = interface
 | 
					            self.interface = interface
 | 
				
			||||||
            super().__init__(**kwargs)
 | 
					            super().__init__(**kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        def clone(self, **kwargs):
 | 
				
			||||||
 | 
					            """Return an Interface copying some values from self."""
 | 
				
			||||||
 | 
					            kwargs.update(
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    "target": self.target,
 | 
				
			||||||
 | 
					                    "funcs": self.funcs,
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            return type(self.interface)(_imeta_kw=kwargs)._imeta
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def __getitem__(self, name):
 | 
					        def __getitem__(self, name):
 | 
				
			||||||
            return self.traces[name]
 | 
					            return self.traces[name]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,10 +1,20 @@
 | 
				
			|||||||
from datetime import time, timedelta
 | 
					from datetime import time, timedelta
 | 
				
			||||||
import itertools
 | 
					import itertools
 | 
				
			||||||
 | 
					import logging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import pytest
 | 
					import pytest
 | 
				
			||||||
from model_bakery import baker
 | 
					from model_bakery import baker
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from aircox import models
 | 
					from aircox import models
 | 
				
			||||||
 | 
					from aircox.test import Interface
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@pytest.fixture
 | 
				
			||||||
 | 
					def logger():
 | 
				
			||||||
 | 
					    logger = Interface(
 | 
				
			||||||
 | 
					        logging, {"info": None, "debug": None, "error": None, "warning": None}
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    return logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@pytest.fixture
 | 
					@pytest.fixture
 | 
				
			||||||
 | 
				
			|||||||
@ -1,19 +0,0 @@
 | 
				
			|||||||
import pytest
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
from aircox.controllers.diffusions import Diffusions
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@pytest.fixture
 | 
					 | 
				
			||||||
def diffusions():
 | 
					 | 
				
			||||||
    return Diffusions
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class TestDiffusion:
 | 
					 | 
				
			||||||
    def test___init__(self):
 | 
					 | 
				
			||||||
        pass
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def test_update(self):
 | 
					 | 
				
			||||||
        pass
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def test_clean(self):
 | 
					 | 
				
			||||||
        pass
 | 
					 | 
				
			||||||
@ -1,5 +1,4 @@
 | 
				
			|||||||
from concurrent import futures
 | 
					from concurrent import futures
 | 
				
			||||||
import logging
 | 
					 | 
				
			||||||
import pytest
 | 
					import pytest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django.utils import timezone as tz
 | 
					from django.utils import timezone as tz
 | 
				
			||||||
@ -18,14 +17,6 @@ def event():
 | 
				
			|||||||
    return Interface(src_path="/tmp/src_path", dest_path="/tmp/dest_path")
 | 
					    return Interface(src_path="/tmp/src_path", dest_path="/tmp/dest_path")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@pytest.fixture
 | 
					 | 
				
			||||||
def logger():
 | 
					 | 
				
			||||||
    logger = Interface(
 | 
					 | 
				
			||||||
        logging, {"info": None, "debug": None, "error": None, "warning": None}
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
    return logger
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
@pytest.fixture
 | 
					@pytest.fixture
 | 
				
			||||||
def interfaces():
 | 
					def interfaces():
 | 
				
			||||||
    items = {
 | 
					    items = {
 | 
				
			||||||
 | 
				
			|||||||
@ -18,36 +18,6 @@ local_tz = tzlocal.get_localzone()
 | 
				
			|||||||
working_dir = os.path.join(os.path.dirname(__file__), "working_dir")
 | 
					working_dir = os.path.join(os.path.dirname(__file__), "working_dir")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def interface_wrap(obj, attr, value):
 | 
					 | 
				
			||||||
    if not isinstance(getattr(obj, "calls", None), dict):
 | 
					 | 
				
			||||||
        obj.calls = {}
 | 
					 | 
				
			||||||
    obj.calls[attr] = None
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def wrapper(*a, **kw):
 | 
					 | 
				
			||||||
        call = obj.calls.get(attr)
 | 
					 | 
				
			||||||
        if call is None:
 | 
					 | 
				
			||||||
            obj.calls[attr] = (a, kw)
 | 
					 | 
				
			||||||
        elif isinstance(call, tuple):
 | 
					 | 
				
			||||||
            obj.calls[attr] = [call, (a, kw)]
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            call.append((a, kw))
 | 
					 | 
				
			||||||
        return value
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    setattr(obj, attr, wrapper)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def interface(obj, funcs):
 | 
					 | 
				
			||||||
    """Override provided object's functions using dict of funcs, as ``{
 | 
					 | 
				
			||||||
    func_name: return_value}``.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Attribute ``obj.calls`` is a dict
 | 
					 | 
				
			||||||
    with all call done using those methods, as
 | 
					 | 
				
			||||||
    ``{func_name: (args, kwargs)}``.
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    for attr, value in funcs.items():
 | 
					 | 
				
			||||||
        interface_wrap(obj, attr, value)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class FakeSocket:
 | 
					class FakeSocket:
 | 
				
			||||||
    FAILING_ADDRESS = -1
 | 
					    FAILING_ADDRESS = -1
 | 
				
			||||||
    """Connect with this address fails."""
 | 
					    """Connect with this address fails."""
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user