Detect copyrights and authors.
| 229 | |
| 230 | |
| 231 | class CopyrightDetector(object): |
| 232 | """ |
| 233 | Detect copyrights and authors. |
| 234 | """ |
| 235 | |
| 236 | def __init__(self): |
| 237 | """ |
| 238 | Initialize this detector with a lexer and a parser. |
| 239 | """ |
| 240 | self.lexer = lex.Lexer(matchers=PATTERNS) |
| 241 | self.parser = parse.Parser( |
| 242 | grammar=GRAMMAR, |
| 243 | loop=1, |
| 244 | trace=TRACE_DEEP, |
| 245 | validate=VALIDATE, |
| 246 | ) |
| 247 | |
| 248 | def detect(self, |
| 249 | numbered_lines, |
| 250 | include_copyrights=True, |
| 251 | include_holders=True, |
| 252 | include_authors=True, |
| 253 | include_copyright_years=True, |
| 254 | include_copyright_allrights=False, |
| 255 | ): |
| 256 | """ |
| 257 | Yield Detection objects detected in a ``numbered_lines`` sequence of |
| 258 | tuples of (line number, text). |
| 259 | |
| 260 | The flags ``include_copyrights``, ``include_holders`` and |
| 261 | ``include_authors`` drive which actual detections are done and returned. |
| 262 | |
| 263 | For copyrights only: |
| 264 | - If ``include_copyright_years`` is True, include years and year ranges. |
| 265 | - If ``include_copyright_allrights`` is True, include trailing |
| 266 | "all rights reserved"-style mentions |
| 267 | """ |
| 268 | |
| 269 | include_copyright_years = include_copyrights and include_copyright_years |
| 270 | include_copyright_allrights = include_copyrights and include_copyright_allrights |
| 271 | |
| 272 | if not numbered_lines: |
| 273 | return |
| 274 | |
| 275 | if TRACE or TRACE_TOK: |
| 276 | logger_debug(f'CopyrightDetector: numbered_lines: {numbered_lines}') |
| 277 | |
| 278 | tokens = list(get_tokens(numbered_lines)) |
| 279 | |
| 280 | if TRACE: |
| 281 | logger_debug(f'CopyrightDetector: initial tokens: {tokens}') |
| 282 | |
| 283 | if not tokens: |
| 284 | return |
| 285 | |
| 286 | # first, POS tag each token using token regexes |
| 287 | lexed_text = list(self.lexer.lex_tokens(tokens, trace=TRACE_TOK)) |
| 288 |
no outgoing calls
no test coverage detected