Coverage for /home/pradyumna/Languages/python/packages/pyprojstencil/pyprojstencil/get_license.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python3
2# -*- coding:utf-8; mode:python; -*-
3#
4# Copyright 2021 Pradyumna Paranjape
5# This file is part of pyprojstencil.
6#
7# pyprojstencil is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Lesser General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# pyprojstencil is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with pyprojstencil. If not, see <https://www.gnu.org/licenses/>.
19#
20"""
21Define <LICENSE> <LICENSE_HEADER>
22"""
24from pathlib import Path
25from typing import Tuple
27from pyprojstencil import INFO_BASE
28from pyprojstencil.errors import LicenseNotKnownError
31def get_license(l_name: str = 'LGPLv3',
32 return_blank: bool = False) -> Tuple[Path, str]:
33 """
34 Fetch License text and license header from name
36 Args:
37 l_name: license name {GPLv3,LGPLv3,MIT}
38 return_blank: if license is not found, return Blank
40 Returns:
41 license path handle
42 license header text (without modifications from template)
44 Raises:
45 LicenseNotKnownError: Provided license is not found
47 """
48 for license_h in (INFO_BASE.templates / "licenses").glob("*"):
49 if not license_h.is_file():
50 continue
51 if license_h.name == l_name:
52 # found license text file
53 header_h = license_h.with_stem(license_h.stem + "_header")
54 if header_h.is_file():
55 return license_h, header_h.read_text()
56 return (license_h, (INFO_BASE.templates /
57 "licenses/custom_header").read_text())
58 elif license_h.name == l_name + "_header":
59 # found header file
60 text_h = license_h.with_stem(license_h.stem.replace("_header", ""))
61 if text_h.is_file():
62 return text_h, license_h.read_text()
63 return (INFO_BASE.templates / "licenses/custom",
64 license_h.read_text())
65 if return_blank:
66 return (INFO_BASE.templates / "licenses/custom",
67 (INFO_BASE.templates / "licenses/custom_header").read_text())
68 raise LicenseNotKnownError(l_name)