Created
October 14, 2022 09:48
-
-
Save apeyroux/9b0927c0ea28f1b098012012734905f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from abc import ABC, abstractmethod | |
from urllib.parse import urlparse | |
class InfoSource(object): | |
def get_vacation(self): | |
pass | |
def set_vacation(self): | |
pass | |
class InfoSourceMCE(InfoSource): | |
""" InfoSourceMCE class est la class "generic" """ | |
def get_vacation(self): | |
print('InfoSourceMCE.get_vacation()') | |
def get_date_avis(self): | |
print('InfoSourceMCE.get_date_avis()') | |
class InfoSourceGN(InfoSourceMCE): | |
""" InfoSourceGN class est la class "specific GN" """ | |
def get_vacation(self): | |
print('InfoSourceGN.get_vacation()') | |
class InfoSourceDGFIP(InfoSourceMCE): | |
""" InfoSourceDGFIP class est la class "specific DGFIP" """ | |
def get_date_avis(self): | |
print('InfoSourceDGFIP.get_date_avis()') | |
class DataSource(ABC): | |
def __init__(self, url): | |
self.url = url | |
@abstractmethod | |
def get_list(self, key): | |
pass | |
@abstractmethod | |
def get(self, key): | |
pass | |
class DataSourceMCE(DataSource): | |
""" Utilise des fichiers locaux par defaut """ | |
def __init__(self, url): | |
super().__init__(url) | |
self.url = url | |
def get(self, key): | |
with open(key) as f: | |
return f.readline() | |
def get_list(self, key): | |
"""Get list from file from MCE""" | |
with open(key) as f: | |
return f.readlines() | |
class DataSourceNFS(DataSourceMCE): | |
""" Exemple d'utilisation NFS """ | |
def __init__(self, url): | |
super().__init__(url) | |
def get(self): | |
print('DataSourceNFS.get() {}'.format(urlparse(self.url).path)) | |
def get_list(self, key): | |
print('DataSourceNFS.get_list() {}'.format(urlparse(self.url).path)) | |
class DataSourceFactory(): | |
@staticmethod | |
def get_datasource(url): | |
p = urlparse(url) | |
if p.scheme == 'nfs': | |
return DataSourceNFS(url) | |
if p.scheme == 'http': | |
return DataSourceNFS(url) | |
class InfoSourceFactory(): | |
@staticmethod | |
def get_infosource(source): | |
if source == "GN": | |
return InfoSourceGN() | |
if source == "DGFIP": | |
return InfoSourceDGFIP() | |
return InfoSourceMCE() | |
if __name__ == "__main__": | |
infosource = InfoSourceFactory.get_infosource("GN") | |
infosource.get_vacation() | |
infosource.get_date_avis() | |
DataSourceFactory.get_datasource( | |
"nfs://localhost:8080/toto.txt" | |
).get_list("test.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment