refactoring and updating models for python 3.7 -- Proxy & Scheduler

This commit is contained in:
Adolfo Gómez García 2019-08-13 11:48:32 +02:00
parent 49fd063e90
commit 2b06813176
7 changed files with 28 additions and 25 deletions

View File

@ -32,23 +32,28 @@
"""
import datetime
import logging
import typing
logger = logging.getLogger(__name__)
if typing.TYPE_CHECKING:
from .Job import Job
class JobsFactory:
_factory = None
_factory: typing.Optional['JobsFactory'] = None
_jobs: typing.Dict[str, typing.Type['Job']]
def __init__(self):
self._jobs = {}
@staticmethod
def factory():
if JobsFactory._factory is None:
def factory() -> 'JobsFactory':
if not JobsFactory._factory:
JobsFactory._factory = JobsFactory()
return JobsFactory._factory
def jobs(self):
def jobs(self) -> typing.Dict[str, typing.Type['Job']]:
return self._jobs
def insert(self, name, type_):
@ -58,7 +63,7 @@ class JobsFactory:
except Exception as e:
logger.debug('Exception at insert in JobsFactory: %s, %s', e.__class__, e)
def ensureJobsInDatabase(self):
def ensureJobsInDatabase(self) -> None:
from uds.models import Scheduler, getSqlDatetime
from uds.core.util.State import State
from uds.core import workers
@ -83,7 +88,7 @@ class JobsFactory:
except Exception as e:
logger.debug('Exception at ensureJobsInDatabase in JobsFactory: %s, %s', e.__class__, e)
def lookup(self, typeName):
def lookup(self, typeName: str) -> typing.Optional[typing.Type['Job']]:
try:
return self._jobs[typeName]
except KeyError:

View File

@ -32,13 +32,13 @@ UDS jobs related modules
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
"""
from uds.core.jobs.Job import Job
from uds.core.jobs.DelayedTask import DelayedTask
from .Job import Job
from .DelayedTask import DelayedTask
from .JobsFactory import JobsFactory
def factory():
def factory() -> JobsFactory:
"""
Returns a singleton to a jobs factory
"""
from uds.core.jobs.JobsFactory import JobsFactory
return JobsFactory.factory()

View File

@ -52,7 +52,7 @@ class OSManagersFactory:
OSManagersFactory._factory = OSManagersFactory()
return OSManagersFactory._factory
def providers(self):
def providers(self) -> typing.Dict[str, typing.Type['OSManager']]:
return self._osManagers
def insert(self, type_: typing.Type['OSManager']) -> None:

View File

@ -40,7 +40,7 @@ from uds.core.util import log
from uds.core.util import unique
from uds.models.managed_object_model import ManagedObjectModel
from uds.models.Tag import TaggingMixin
from uds.models.Proxy import Proxy
from uds.models.proxy import Proxy
from uds.core.util import connection
from uds.models.provider import Provider

View File

@ -81,7 +81,7 @@ from .Storage import Storage
from .UniqueId import UniqueId
# Workers/Schedulers related
from .Scheduler import Scheduler
from .scheduler import Scheduler
from .delayed_task import DelayedTask
# Image galery related
@ -102,7 +102,7 @@ from .account import Account
from .account_usage import AccountUsage
# Proxies
from .Proxy import Proxy
from .proxy import Proxy
# Tagging
from .Tag import Tag

View File

@ -32,17 +32,15 @@
"""
import logging
from django.db import models
from django.db.models import signals
from uds.core.util.State import State
from uds.core.environment import Environment
from uds.core.jobs.JobsFactory import JobsFactory
from uds.core import jobs
from uds.models.util import NEVER
logger = logging.getLogger(__name__)
@ -78,23 +76,23 @@ class Scheduler(models.Model):
"""
app_label = 'uds'
def getEnvironment(self):
def getEnvironment(self) -> Environment:
"""
Returns an environment valid for the record this object represents
"""
return Environment.getEnvForTableElement(self._meta.verbose_name, self.id)
def getInstance(self):
def getInstance(self) -> jobs.Job:
"""
Returns an instance of the class that this record of the Scheduler represents. This clas is derived
of uds.core.jobs.Job.Job
"""
jobInstance = JobsFactory.factory().lookup(self.name)
if jobInstance is not None:
env = self.getEnvironment()
return jobInstance(env)
else:
return None
jobInstance = jobs.factory().lookup(self.name)
if jobInstance:
return jobInstance(self.getEnvironment())
return jobs.Job(self.getEnvironment())
@staticmethod
def beforeDelete(sender, **kwargs):