Assign one or more "facet" to each file (and NOT to directories). Facets are a way to qualify that some part of the scanned code may be core code vs. test vs. data, etc.
| 99 | |
| 100 | @pre_scan_impl |
| 101 | class AddFacet(PreScanPlugin): |
| 102 | """ |
| 103 | Assign one or more "facet" to each file (and NOT to directories). Facets are |
| 104 | a way to qualify that some part of the scanned code may be core code vs. |
| 105 | test vs. data, etc. |
| 106 | """ |
| 107 | |
| 108 | resource_attributes = dict(facets=attr.ib(default=attr.Factory(list), repr=False)) |
| 109 | |
| 110 | run_order = 20 |
| 111 | sort_order = 20 |
| 112 | |
| 113 | options = [ |
| 114 | PluggableCommandLineOption(('--facet',), |
| 115 | multiple=True, |
| 116 | default=None, |
| 117 | metavar='<facet>=<pattern>', |
| 118 | callback=validate_facets, |
| 119 | help='Add the <facet> to files with a path matching <pattern>.', |
| 120 | help_group=PRE_SCAN_GROUP, |
| 121 | sort_order=80, |
| 122 | ) |
| 123 | ] |
| 124 | |
| 125 | def is_enabled(self, facet, **kwargs): |
| 126 | if TRACE: |
| 127 | logger_debug('is_enabled: facet:', facet) |
| 128 | |
| 129 | return bool(facet) |
| 130 | |
| 131 | def process_codebase(self, codebase, facet=(), **kwargs): |
| 132 | """ |
| 133 | Add facets to file resources using the `facet` definition of facets. |
| 134 | Each entry in the `facet` sequence is a string as in <facet>:<pattern> |
| 135 | """ |
| 136 | |
| 137 | if not facet: |
| 138 | return |
| 139 | |
| 140 | facet_definitions, _invalid_facet_definitions = build_facets(facet) |
| 141 | |
| 142 | if TRACE: |
| 143 | logger_debug('facet_definitions:', facet_definitions) |
| 144 | |
| 145 | # Walk the codebase and set the facets for each file (and only files) |
| 146 | for resource in codebase.walk(topdown=True): |
| 147 | if not resource.is_file: |
| 148 | continue |
| 149 | facets = compute_path_facets(resource.path, facet_definitions) |
| 150 | if facets: |
| 151 | resource.facets = facets |
| 152 | else: |
| 153 | resource.facets = [FACET_CORE] |
| 154 | resource.save(codebase) |
| 155 | |
| 156 | |
| 157 | def compute_path_facets(path, facet_definitions): |
nothing calls this directly
no test coverage detected