Coverage for src/hods/config/tree.py: 100.00%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

42 statements  

1"""hods - home directory synchronization. 

2 

3Copyright (C) 2016-2020 Mathias Stelzer <knoppo@rolln.de> 

4 

5hods is free software: you can redistribute it and/or modify 

6it under the terms of the GNU General Public License as published by 

7the Free Software Foundation, either version 3 of the License, or 

8(at your option) any later version. 

9 

10hods is distributed in the hope that it will be useful, 

11but WITHOUT ANY WARRANTY; without even the implied warranty of 

12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

13GNU General Public License for more details. 

14 

15You should have received a copy of the GNU General Public License 

16along with this program. If not, see <http://www.gnu.org/licenses/>. 

17""" 

18import logging 

19 

20from hods.config.feature import Feature 

21from hods.node import RootDirectoryNode 

22 

23 

24logger = logging.getLogger(__name__) 

25 

26 

27class SourceTree(RootDirectoryNode): 

28 """The source tree root node.""" 

29 

30 child_directory_class = Feature 

31 

32 child_file_class = None 

33 

34 children_key = 'features' 

35 

36 sort_children = False 

37 

38 clear_on_scan = False 

39 

40 def __init__(self, settings): 

41 """Initialize configuration directory ~/.hods. 

42 

43 :param config: `hods.config.base.Config` instance 

44 """ 

45 self.settings = settings 

46 super().__init__(settings.hods_path) 

47 

48 @property 

49 def features(self): 

50 """List the features in this tree (children alias).""" 

51 return self.children 

52 

53 @features.setter 

54 def features(self, value): 

55 """Set the list of features in this tree (children alias).""" 

56 self.children[:] = value 

57 

58 def load_data(self, data): 

59 """Load the given configuration data.""" 

60 features = data.get(self.children_key, ()) 

61 self.children[:] = [] 

62 self.load_children(features) 

63 

64 def load_child_class(self, data): 

65 """Return `Feature` as static child class.""" 

66 return Feature 

67 

68 def as_dict(self): 

69 """Serialize the given Feature objects and return data.""" 

70 data = super().as_dict() 

71 del data['name'] 

72 data['version'] = self.settings.version 

73 return data 

74 

75 @property 

76 def installed_features(self): 

77 """Collect installed features.""" 

78 return [feature for feature in self.features if feature.installed] 

79 

80 def collect_features_to_sync(self): 

81 """Collect features to pull or push.""" 

82 if self.settings.is_server: 

83 yield from self.features 

84 else: 

85 yield from self.installed_features 

86 

87 def collect_sourcefiles(self): 

88 """Recursively collect source files to synchronize.""" 

89 for feature in self.installed_features: 

90 for source in feature.sources: 

91 if not source.is_ignored(): 

92 yield from source.collect_children(exclude_dirs=True, exclude_ignored=True)