Browse Source

Merge "refactor: Allow site_by_params to take in list of fields"

Zuul 5 months ago
parent
commit
313c4ed86c
3 changed files with 27 additions and 33 deletions
  1. 1
    3
      pegleg/engine/lint.py
  2. 6
    12
      pegleg/engine/site.py
  3. 20
    18
      pegleg/engine/util/definition.py

+ 1
- 3
pegleg/engine/lint.py View File

@@ -183,9 +183,7 @@ def _verify_no_unexpected_files(*, sitenames=None):
183 183
     expected_directories = set()
184 184
     for site_name in sitenames:
185 185
         params = util.definition.load_as_params(site_name)
186
-        expected_directories.update(
187
-            util.files.directories_for(
188
-                site_name=params['site_name'], site_type=params['site_type']))
186
+        expected_directories.update(util.files.directories_for(**params))
189 187
     LOG.debug('expected_directories: %s', expected_directories)
190 188
     found_directories = util.files.existing_directories()
191 189
     LOG.debug('found_directories: %s', found_directories)

+ 6
- 12
pegleg/engine/site.py View File

@@ -117,20 +117,12 @@ def list_(output_stream):
117 117
 
118 118
     # Create a table to output site information for all sites for a given repo
119 119
     site_table = PrettyTable()
120
-    site_table.field_names = ['site_name', 'site_type']
120
+    field_names = ['site_name', 'site_type']
121
+    site_table.field_names = field_names
121 122
 
122 123
     for site_name in util.files.list_sites():
123
-        params = util.definition.load_as_params(site_name)
124
-        # TODO(felipemonteiro): This is a temporary hack around legacy manifest
125
-        # repositories containing the name of a directory that symbolizes a
126
-        # repository. Once all these manifest repositories migrate over to Git
127
-        # references instead, remove this hack.
128
-        # NOTE(felipemonteiro): The 'revision' information can instead be
129
-        # computed using :func:`process_site_repository` and storing into
130
-        # a configuration via a "set_site_revision" function, for example.
131
-        if 'revision' in params:
132
-            params.pop('revision')
133
-        site_table.add_row([params['site_name'], params['site_type']])
124
+        params = util.definition.load_as_params(site_name, *field_names)
125
+        site_table.add_row(list(map(lambda k: params[k], field_names)))
134 126
     # Write table to specified output_stream
135 127
     output_stream.write(site_table.get_string() + "\n")
136 128
 
@@ -141,6 +133,8 @@ def show(site_name, output_stream):
141 133
     # Create a table to output site information for specific site
142 134
     site_table = PrettyTable()
143 135
     site_table.field_names = ['revision', 'site_name', 'site_type', 'files']
136
+    # TODO(felipemonteiro): Drop support for 'revision' once manifest
137
+    # repositories have removed it altogether.
144 138
     if 'revision' in data.keys():
145 139
         for file in data['files']:
146 140
             site_table.add_row(

+ 20
- 18
pegleg/engine/util/definition.py View File

@@ -30,18 +30,25 @@ def load(site, primary_repo_base=None):
30 30
     return files.slurp(path(site, primary_repo_base))
31 31
 
32 32
 
33
-def load_as_params(site_name, primary_repo_base=None):
33
+def load_as_params(site_name, *fields, primary_repo_base=None):
34
+    """Load site definition for given ``site_name`` and return data as params.
35
+
36
+    :param str site_name: Name of the site.
37
+    :param iterable fields: List of parameter fields to return. Defaults to
38
+        ``('site_name', 'site_type')``.
39
+    :param str primary_repo_base: Path to primary repository.
40
+    :returns: key-value pairs of parameters, whose keys are a subset of those
41
+        specified by ``fields``.
42
+    :rtype: dict
43
+    """
44
+    if not fields:
45
+        # Default legacy fields.
46
+        fields = ('site_name', 'site_type')
47
+
34 48
     definition = load(site_name, primary_repo_base)
35
-    # TODO(felipemonteiro): Currently we are filtering out "revision" from
36
-    # the params that are returned by this function because it is no longer
37
-    # supported. This is a workaround. As soon as the site definition repos
38
-    # switch to real repository format, then we can drop that workaround.
39
-    # Ideally, we should:
40
-    # 1) validate the site-definition.yaml format using lint module
41
-    # 2) extract only the required params here
42 49
     params = definition.get('data', {})
43 50
     params['site_name'] = site_name
44
-    return params
51
+    return {k: v for k, v in params.items() if k in fields}
45 52
 
46 53
 
47 54
 def path(site_name, primary_repo_base=None):
@@ -63,17 +70,14 @@ def pluck(site_definition, key):
63 70
 
64 71
 def site_files(site_name):
65 72
     params = load_as_params(site_name)
66
-    for filename in files.search(
67
-            files.directories_for(
68
-                site_name=params['site_name'], site_type=params['site_type'])):
73
+    for filename in files.search(files.directories_for(**params)):
69 74
         yield filename
70 75
 
71 76
 
72 77
 def site_files_by_repo(site_name):
73 78
     """Yield tuples of repo_base, file_name."""
74 79
     params = load_as_params(site_name)
75
-    dir_map = files.directories_for_each_repo(
76
-        site_name=params['site_name'], site_type=params['site_type'])
80
+    dir_map = files.directories_for_each_repo(**params)
77 81
     for repo, dl in dir_map.items():
78 82
         for filename in files.search(dl):
79 83
             yield (repo, filename)
@@ -93,8 +97,7 @@ def documents_for_each_site():
93 97
 
94 98
     for sitename in sitenames:
95 99
         params = load_as_params(sitename)
96
-        paths = files.directories_for(
97
-            site_name=params['site_name'], site_type=params['site_type'])
100
+        paths = files.directories_for(**params)
98 101
         filenames = set(files.search(paths))
99 102
         for filename in filenames:
100 103
             with open(filename) as f:
@@ -116,8 +119,7 @@ def documents_for_site(sitename):
116 119
     documents = []
117 120
 
118 121
     params = load_as_params(sitename)
119
-    paths = files.directories_for(
120
-        site_name=params['site_name'], site_type=params['site_type'])
122
+    paths = files.directories_for(**params)
121 123
     filenames = set(files.search(paths))
122 124
     for filename in filenames:
123 125
         with open(filename) as f:

Loading…
Cancel
Save