Coverage for src/ensae_teaching_cs/homeblog/filefunction.py: 89%

47 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-01-27 05:44 +0100

1""" 

2@file 

3@brief Helpers for files 

4""" 

5import os 

6import glob 

7import re 

8 

9 

10def liste_fichier_repertoire(folder, filter): 

11 file, fold = [], [] 

12 res = glob.glob(folder + "/" + filter) 

13 rep = glob.glob(folder + "/*") 

14 for r in rep: 

15 if r not in res and os.path.isdir(r): 

16 res.append(r) 

17 for r in res: 

18 path = r 

19 if os.path.isfile(path): 

20 file.append(path) 

21 else: 

22 fold.append(path) 

23 fi, fo = liste_fichier_repertoire(path, filter) 

24 file.extend(fi) 

25 fold.extend(fo) 

26 return file, fold 

27 

28 

29def isTemporaryFolder(path): 

30 iter = 0 

31 a, b = os.path.split(path) 

32 while len(a) > 0 and a != ".": 

33 if b.startswith("temp"): 

34 return True 

35 if b.startswith("cloudera-quickstart-demo"): 

36 return False 

37 la = a 

38 a, b = os.path.split(a) 

39 if a == la: 

40 break 

41 iter += 1 

42 if iter > 30: 

43 raise Exception( 

44 "unable to continue, too many subfolders: " + path + "\na,b: " + a + "," + b) 

45 if b.startswith("temp"): 

46 return True 

47 return False 

48 

49 

50def find_all_blogs_function(folder=".", exclude=None, allow_temp=False): 

51 if len(folder) == 0: 

52 raise ValueError("folder is empty, it should be at least '.'") 

53 file = liste_fichier_repertoire(folder, "*.html")[0] 

54 exp = re.compile("[0-9-]{10}[.]html") 

55 file = [_ for _ in file if exp.search(os.path.split(_)[-1])] 

56 file = [ 

57 _ for _ in file if "dprivate" not in _ and (allow_temp or not isTemporaryFolder(_))] 

58 if exclude is not None: 

59 file = [_ for _ in file if not exclude(_)] 

60 return file