def __init__(self, pipe, attente=10): self.pipe = pipe self.checklist = ThreadSafeWrapper([]) self.erreurs = ThreadSafeWrapper({}) self.attente = attente super(CheckerThread, self).__init__(name="CheckerThread " + identifiant_processus())
def __init__(self, adresse): if isinstance(adresse, tuple): adresse = AdresseIP(*adresse, check=False) if not adresse.valide: raise AdresseInvalideErreur() self._adresse = ThreadSafeWrapper(adresse) self._connecte = ThreadSafeWrapper(False)
def __init__(self, adresse, port, check=True): with self._lock: if self._creer: self._adresse = ThreadSafeWrapper(adresse) self._port = ThreadSafeWrapper(port) self._erreur = ThreadSafeWrapper(None) if check: CheckerGardien().ajouter(self) self._creer = False
def __init__(self, pipe): self.udp = UDPGardien() self.tcp = TCPGardien() self.pipe = pipe self.recherches = ThreadSafeWrapper({}) self.garde = ThreadSafeWrapper(Garde()) self.eligible = ThreadSafeWrapper(True) super(GardienThread, self).__init__(name="GardienTread " + identifiant_processus())
def __init__(self): if self._creer: super(Gardien, self).__init__() self.checker = CheckerGardien() self.checker.lancer() self._pipe, pipe = mp.Pipe() self._possession = ThreadSafeWrapper([]) self._statut = ThreadSafeWrapper({}) self._lock_ip = ThreadSafeWrapper({}) self._elections = ThreadSafeWrapper({}) self.thread = GardienThread(pipe) self.thread._gardien = self self._creer = False
class XMLThread(ThreadLogMixin, th.Thread): # Attributs nom_log = "upnp" etudes_en_cours = ThreadSafeWrapper({}) # Méthodes spéciales def __init__(self, location, parser): if not location.startswith("http://"): location = "http://" + location self.location = location self.parser_cls = parser super(XMLThread, self).__init__() # Méthodes def recup_arbre(self): reponse, ok = ClientHTTP.depuisAdresse(self.location) if ok: self.debug("{} : arbre reçu.".format(self.location)) return reponse.read().decode("utf-8") return None def run(self): with self.etudes_en_cours: if self.location in self.etudes_en_cours.objet.get( self.parser_cls, []): return else: if self.parser_cls not in self.etudes_en_cours.objet: self.etudes_en_cours.objet[self.parser_cls] = [] self.etudes_en_cours.objet[self.parser_cls].append( self.location) self.info("Analyse de l'arbre {}".format(self.location)) arbre = self.recup_arbre() if arbre == None: return self.parser = self.parser_cls(arbre) m = re.match( r"http://((([0-9]{1,3}\.?){4}(:[0-9]{1,5})?)|([a-zA-Z\.0-9-_=+?]+))/", self.location) self.parser._base_url = self.location[m.start():m.end()] self.parser.analyser() with self.etudes_en_cours: self.etudes_en_cours.objet[self.parser_cls].remove(self.location) self.info("Arbre {} analysé !".format(self.location))
def __init__(self, identifiant=None): if not self._creer: return self._lock = ThreadSafeWrapper(None, mp.RLock()) if self.gardien.status == "initial": self.gardien.lancer() self._pret = th.Event() if not self.gardien.connait(self.identifiant): self._thread = PreparationThread(target=self._elir) self._thread.start() else: self._pret.set() self._creer = False
def __init__(self, chemin, *args, **kwargs): chemin = path.abspath(chemin) if self._initier: if hasattr(self, "extension"): if self.extension != '*': chemin = joinext(chemin, self.extension) self._lock_contenu = RLock( hashlib.md5("{}:{}".format( os.uname()[1], chemin).encode("utf-8")).hexdigest()) self._chemin = ThreadSafeWrapper(chemin) self._lock_fichier = ThreadSafeWrapper(path.exists, self._lock_parent) self._creation = False self._suppression = False if not path.exists(self.chemin) and self.auto_creation: self.creer(*args, **kwargs) self._initier = False self._parametrer()
class BaseThread(th.Thread, LogMixin): # Attributs nom_log = "gardien" _arret_gardien = ThreadSafeWrapper(False) # Méthodes def join(self, timeout=None): if not self._arret_gardien.objet and ( mp.current_process().gardien.status == "actif"): with self._arret_gardien: self._arret_gardien.objet = True mp.current_process().gardien.arreter() else: super(BaseThread, self).join(timeout)
class PerifListeThread(ThreadLogMixin, th.Thread): # Attributs nom_log = "upnp.periferiques" marque = "perif" events = ThreadSafeWrapper({}) # Méthodes spéciales def __init__(self, queue, events): self.queue = self.modifs = queue self.fini = False with self.events: self.events.objet.update(events) super(PerifListeThread, self).__init__() # Méthodes def run(self): self.stock = helper.stock["periferiques"] with self.stock: while not self.fini: try: requete, opts, nopts = self.modifs.get() if requete == "fini": break elif hasattr(self, requete): getattr(self, requete)(*opts, **nopts) except queue.Empty: pass def ajout(self, uuid, id_p): with self.stock["liste"] as l: l.ajouter_valeurs(uuid=uuid, id=id_p) if not self.stock.existe(uuid): self.stock.ajouter_stock(uuid) self.events.objet[uuid].set() def arreter(self): self.fini = True self.modifs.put(("fini", [], {})) self.join()
def __new__(cls, chemin, *args, **kwargs): chemin = path.abspath(chemin) if cls.__pid_registre__ == None: cls.__pid_registre__ = mp.current_process().pid elif cls.__pid_registre__ != mp.current_process().pid: cls.__pid_registre__ = mp.current_process().pid cls.__registre__ = ThreadSafeWrapper({}) if not chemin in cls.__registre__.objet: instance = super(Base, cls).__new__(cls) instance._initier = True with cls.__registre__: cls.__registre__.objet[chemin] = instance return cls.__registre__.objet[chemin]
class PreparationThread(th.Thread): # Attributs __registre__ = ThreadSafeWrapper([]) # Méthodes spéciales def __init__(self, *args, **kwargs): with self.__registre__: self.__registre__.objet.append(self) super(PreparationThread, self).__init__(*args, **kwargs) # Méthodes def join(self): with self.__registre__: if self in self.__registre__.objet: self.__registre__.objet.remove(self) super(PreparationThread, self).join()
def __new__(cls, identifiant=None): if not identifiant: identifiant = hashlib.md5( ("{}{}".format(identifiant_processus(), time())).encode("utf-8")).hexdigest() if cls.__pid_instances__ == None: cls.__pid_instances__ = mp.current_process().pid elif cls.__pid_instances__ != mp.current_process().pid: cls.__pid_instances__ = mp.current_process().pid cls.__instances__ = ThreadSafeWrapper({}) if identifiant in cls.__instances__.objet: return cls.__instances__.objet[identifiant] self = super(RLock, cls).__new__(cls) self.identifiant = identifiant self._creer = True with cls.__instances__: cls.__instances__.objet[identifiant] = self return self
def _parametrer(self): self._ouvert = ThreadSafeWrapper(False) self._mode = ThreadSafeWrapper("")
class RLock: # Attributs __instances__ = ThreadSafeWrapper({}) __pid_instances__ = None # Méthodes spéciales def __new__(cls, identifiant=None): if not identifiant: identifiant = hashlib.md5( ("{}{}".format(identifiant_processus(), time())).encode("utf-8")).hexdigest() if cls.__pid_instances__ == None: cls.__pid_instances__ = mp.current_process().pid elif cls.__pid_instances__ != mp.current_process().pid: cls.__pid_instances__ = mp.current_process().pid cls.__instances__ = ThreadSafeWrapper({}) if identifiant in cls.__instances__.objet: return cls.__instances__.objet[identifiant] self = super(RLock, cls).__new__(cls) self.identifiant = identifiant self._creer = True with cls.__instances__: cls.__instances__.objet[identifiant] = self return self def __init__(self, identifiant=None): if not self._creer: return self._lock = ThreadSafeWrapper(None, mp.RLock()) if self.gardien.status == "initial": self.gardien.lancer() self._pret = th.Event() if not self.gardien.connait(self.identifiant): self._thread = PreparationThread(target=self._elir) self._thread.start() else: self._pret.set() self._creer = False def __del__(self): with self.__instances__: del self.__instances__.objet[self.identifiant] def __repr__(self): return "<RLock {}>".format(self.identifiant) def __eq__(self, obj): if isinstance(obj, RLock): return self.identifiant == obj.identifiant else: return False def __ne__(self, obj): self._wait_pret() if isinstance(obj, RLock): return self.identifiant != obj.identifiant else: return True def __enter__(self): while not self.acquire(): pass def __exit__(self, t, v, b): self.release() # Méthodes privées def _elir(self): while True: try: self.gardien.election(self.identifiant) break except PasDeReponse: pass self._pret.set() def _wait_pret(self): self._pret.wait() try: self._thread.join() del self._thread except AttributeError: pass # Méthodes def acquire(self, block=True, timeout=0): self._wait_pret() if timeout == None: timeout = 0 while not self._lock.prendre(): pass with self._lock: self._lock.objet = th.get_ident() if not self.gardien.connait(self.identifiant): self.gardien.election(self.identifiant) datedeb = datetime.now() while True: try: retour = self.gardien.bloquer(self.identifiant) except Exception as err: retour = False if retour or not block: break elif timeout == 0: continue elif (datetime.now() - datedeb).total_seconds() > timeout: break if not retour: if self.identifiant in self.gardien.possession: with self._lock: self._lock.objet = None self._lock.lacher() return retour def release(self): self._wait_pret() if not self.est_proprietaire(): return False if not self.gardien.connait(self.identifiant): self.gardien.election(self.identifiant) retour = self.gardien.debloquer(self.identifiant) if retour: if not self.identifiant in self.gardien.possession: with self._lock: self._lock.objet = None self._lock.lacher() return retour def est_bloque(self): self._wait_pret() return self.gardien.est_bloque(self.identifiant) def est_proprietaire(self): self._wait_pret() return self._lock.objet == th.get_ident() # Propriétés @property def gardien(self): return Gardien()
def __init__(self, *args, **kwargs): self.perifs = ThreadSafeWrapper(Table(("uuid", "id", "periferique"))) self.events = {} super(PerifServeur, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): self.periferiques = ThreadSafeWrapper({}) super(SSDPServeur, self).__init__(*args, **kwargs)
def __init__(self): self._status = ThreadSafeWrapper(0)
class Base(metaclass=abc.ABCMeta): # Attributs spéciaux __registre__ = ThreadSafeWrapper({}) # Registre des instances __pid_registre__ = None # Pour détecter les copies lors des passages entres processus __registre_classes__ = ThreadSafeWrapper({ # Registre des classes, permet de choisir la bonne classe pour le bon fichier "fichiers": {}, "dossiers": {}, }) # Attributs auto_creation = True # Si True, __init__ crée le fichier s'il n'existe pas # Méthodes spéciales def __new__(cls, chemin, *args, **kwargs): chemin = path.abspath(chemin) if cls.__pid_registre__ == None: cls.__pid_registre__ = mp.current_process().pid elif cls.__pid_registre__ != mp.current_process().pid: cls.__pid_registre__ = mp.current_process().pid cls.__registre__ = ThreadSafeWrapper({}) if not chemin in cls.__registre__.objet: instance = super(Base, cls).__new__(cls) instance._initier = True with cls.__registre__: cls.__registre__.objet[chemin] = instance return cls.__registre__.objet[chemin] @verif_type(chemin=str) def __init__(self, chemin, *args, **kwargs): chemin = path.abspath(chemin) if self._initier: if hasattr(self, "extension"): if self.extension != '*': chemin = joinext(chemin, self.extension) self._lock_contenu = RLock( hashlib.md5("{}:{}".format( os.uname()[1], chemin).encode("utf-8")).hexdigest()) self._chemin = ThreadSafeWrapper(chemin) self._lock_fichier = ThreadSafeWrapper(path.exists, self._lock_parent) self._creation = False self._suppression = False if not path.exists(self.chemin) and self.auto_creation: self.creer(*args, **kwargs) self._initier = False self._parametrer() def __repr__(self): return "<{} {}>".format(self.__class__.__name__, self.chemin) def __eq__(self, obj): if isinstance(obj, Base): return self._lock_contenu == obj._lock_contenu return False def __ne__(self, obj): if isinstance(obj, Base): return self._lock_contenu != obj._lock_contenu return True # Méthodes de classe @classmethod @abc.abstractmethod def _enregistrer_classe(cls, classe): return NotImplemented @classmethod def _recuperer_classe(cls, chemin): if not path.exists(chemin): raise FileNotFoundError("Le fichier n'existe pas") if path.isdir(chemin): params = path.join(chemin, "__params__.pystock") if path.exists(params): with open(params, "r") as f: iddos = f.read().strip() else: iddos = "Dossier" if iddos not in cls.__registre_classes__.objet["dossiers"]: iddos = "Dossier" return cls.__registre_classes__.objet["dossiers"][iddos] elif path.isfile(chemin): extension = path.splitext(chemin)[1] or '*' if extension not in cls.__registre_classes__.objet["fichiers"]: extension = '*' return cls.__registre_classes__.objet["fichiers"][extension] else: raise TypeError("Le fichier n'est pas géré") # Méthodes privées @abc.abstractmethod def _creer(self, chemin): return NotImplemented @abc.abstractmethod def _supprimer(self, chemin): return NotImplemented def _parametrer(self): pass def _lock_parent(self): try: chemin = path.dirname(self.chemin) return self._recuperer_classe(chemin)(chemin)._lock_contenu except OSError: return self._lock_contenu except TypeError: return self._lock_contenu # Méthodes def creer(self, *args, **kwargs): chemin = self.chemin with self._lock_fichier: if self.existe: raise FileExistsError("Le fichier existe déjà") self._creation = True self._creer(chemin, *args, **kwargs) self._creation = False @verif_type(nouveau_chemin=str) def deplacer(self, nouveau_chemin, ecrase=False): if path.exists(nouveau_chemin) and not ecrase: raise FileExistsError("Le nouveau chemin est déjà utilisé") chemin = self.chemin with self._chemin: self._chemin.objet = nouveau_chemin if not self.existe: with self._lock_fichier: os.replace(chemin, nouveau_chemin) def supprimer(self): chemin = self.chemin with self._lock_fichier: if not self.existe: raise FileNotFoundError("Le fichier n'existe pas") self._supression = True self._supprimer(chemin) self._suppression = False # Propriétés @property def chemin(self): return self._chemin.objet @chemin.setter def chemin(self, chemin): self.deplacer(path.abspath(chemin)) @property def nom(self): return path.basename(self.chemin) @nom.setter def nom(self, nom): self.deplacer(path.join(path.dirname(self.chemin), nom)) @property def existe(self): if self._creation: return True elif self._suppression: return False return self._lock_fichier.objet(self.chemin) @property def parent(self): if not self.existe: raise FileNotFoundError( "Le fichier n'existe pas il n'a pas de parent") try: chemin = path.dirname(self.chemin) return self._recuperer_classe(chemin)(chemin) except OSError: return None except TypeError: return None