mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-04-20 02:41:19 +02:00
implemented Py2 & Py3 compatible Singleton
This commit is contained in:
parent
5bb98aa007
commit
0f89ff4d63
@ -6,14 +6,10 @@ from urlparse import urlparse
|
|||||||
from logger import logger
|
from logger import logger
|
||||||
from parser import request
|
from parser import request
|
||||||
|
|
||||||
|
from nhentai.utils import Singleton
|
||||||
|
|
||||||
class Downloader(object):
|
|
||||||
_instance = None
|
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
class Downloader(Singleton):
|
||||||
if not cls._instance:
|
|
||||||
cls._instance = super(Downloader, cls).__new__(cls, *args, **kwargs)
|
|
||||||
return cls._instance
|
|
||||||
|
|
||||||
def __init__(self, path='', thread=1, timeout=30):
|
def __init__(self, path='', thread=1, timeout=30):
|
||||||
if not isinstance(thread, (int, )) or thread < 1 or thread > 10:
|
if not isinstance(thread, (int, )) or thread < 1 or thread > 10:
|
||||||
|
10
nhentai/utils.py
Normal file
10
nhentai/utils.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# coding: utf-8
|
||||||
|
class _Singleton(type):
|
||||||
|
""" A metaclass that creates a Singleton base class when called. """
|
||||||
|
_instances = {}
|
||||||
|
def __call__(cls, *args, **kwargs):
|
||||||
|
if cls not in cls._instances:
|
||||||
|
cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
|
||||||
|
return cls._instances[cls]
|
||||||
|
|
||||||
|
class Singleton(_Singleton('SingletonMeta', (object,), {})): pass
|
Loading…
x
Reference in New Issue
Block a user