Debian Codegraph @

mrcfile/command_line.py

raw
1 # Copyright (c) 2018, Science and Technology Facilities Council
2 # This software is distributed under a BSD licence. See LICENSE.txt.
3
4 """
5 command_line
6 ------------
7
8 Module for functions used as command line entry points.
9
10 The names of the corresponding command line scripts can be found in the
11 ``entry_points`` section of ``setup.py``.
12
13 """
14
15 # Import Python 3 features for future-proofing
16 from __future__ import (absolute_import, division, print_function,
17 unicode_literals)
18
19 import argparse
20
21 import mrcfile
22
23
24 def print_headers(names=None, print_file=None):
25 """
26 Print the MRC header contents from a list of files.
27
28 This function opens files in permissive mode to allow headers of invalid
29 files to be examined.
30
31 Args:
32 names: A list of file names. If not given or :data:`None`, the names
33 are taken from the command line arguments.
34 print_file: The output text stream to use for printing the headers.
35 This is passed directly to the ``print_file`` argument of
36 :meth:`~mrcfile.mrcobject.MrcObject.print_header`. The default is
37 :data:`None`, which means output will be printed to
38 :data:`sys.stdout`.
39 """
40 if names is None:
41 parser = argparse.ArgumentParser(
42 description="Print the MRC header contents from a list of files."
43 )
44 parser.add_argument("filename", nargs='*', help="Input MRC file")
45 args = parser.parse_args()
46 names = args.filename
47 for name in names:
48 with mrcfile.open(name, permissive=True, header_only=True) as mrc:
49 print("MRC header for {}:".format(name), file=print_file)
50 mrc.print_header(print_file=print_file)