コード例 #1
0
def sort_requirements(filename: PathLike, allow_git: bool = False) -> int:
    """
	Sort the requirements in the given file alphabetically.

	:param filename: The file to sort the requirements in.
	:param allow_git: Whether to allow lines that start with ``git+``, which are allowed by pip but not :pep:`508`.
	"""

    ret = PASS
    filename = PathPlus(filename)
    comments: List[str]
    requirements: Set[ComparableRequirement]
    git_lines: List[str] = []

    requirements, comments, invalid_lines = read_requirements(
        req_file=filename,
        include_invalid=True,
        normalize_func=normalize_keep_dot,
    )

    for line in invalid_lines:
        if line.startswith("git+") and allow_git:
            git_lines.append(line)
        else:
            ret |= FAIL

    # find and remove pkg-resources==0.0.0
    # which is automatically added by broken pip package under Debian
    if ComparableRequirement("pkg-resources==0.0.0") in requirements:
        requirements.remove(ComparableRequirement("pkg-resources==0.0.0"))
        ret |= FAIL

    sorted_requirements = sorted(requirements)

    buf = StringList(
        [*comments, *git_lines, *[str(req) for req in sorted_requirements]])
    buf.blankline(ensure_single=True)

    if (requirements != sorted_requirements
            and buf != filename.read_lines()) or ret:
        print('\n'.join(buf))
        # print(coloured_diff(
        # 		filename.read_lines(),
        # 		buf,
        # 		str(filename),
        # 		str(filename),
        # 		"(original)",
        # 		"(sorted)",
        # 		lineterm='',
        # 		))
        ret |= FAIL
        filename.write_lines(buf)

    return ret
コード例 #2
0
ファイル: reference_data.py プロジェクト: domdfcoding/pynist
    def from_jcamp(cls,
                   file_name: PathLike,
                   ignore_warnings: bool = True) -> "ReferenceData":
        """
		Create a ReferenceData object from a JCAMP-DX file.

		:param file_name: Path of the file to read.
		:param ignore_warnings: Whether warnings about invalid tags should be shown.

		:authors: Qiao Wang, Andrew Isaac, Vladimir Likic, David Kainer, Dominic Davis-Foster
		"""

        with warnings.catch_warnings():

            if ignore_warnings:
                warnings.simplefilter("ignore", JcampTagWarning)

            file_name = PathPlus(file_name)

            # Commented this line because it also gets printed when the MassSpectrum is created
            # print(f" -> Reading JCAMP file '{file_name}'")
            lines_list = file_name.read_lines()
            last_tag = None

            header_info: Dict[str, Any] = {
            }  # Dictionary containing header information

            for line in lines_list:

                if len(line.strip()):
                    if line.startswith("##"):
                        # key word or information
                        fields = line.split('=', 1)
                        current_tag = fields[0] = fields[0].lstrip(
                            "##").upper()
                        last_tag = fields[0]
                        fields[1] = fields[1].strip()

                        if current_tag.upper().startswith("END"):
                            break

                        elif current_tag in xydata_tags:
                            continue

                        elif current_tag in header_info_fields:
                            if fields[1].isdigit():
                                header_info[current_tag] = int(fields[1])
                            elif is_float(fields[1]):
                                header_info[current_tag] = float(fields[1])
                            else:
                                header_info[current_tag] = fields[1]
                        else:
                            warnings.warn(current_tag, JcampTagWarning)

                    else:
                        if last_tag in header_info:
                            header_info[last_tag] += f"{line}"

            return cls(
                name=header_info["TITLE"],
                cas=header_info["CAS REGISTRY NO"],
                nist_no=header_info["$NIST MASS SPEC NO"],
                contributor=header_info["ORIGIN"],
                formula=header_info["MOLFORM"],
                mw=header_info["MW"],
                mass_spec=MassSpectrum.from_jcamp(file_name),
            )