Package osgeo :: Package utils :: Module gdalident |
[hide private]
[frames] | no frames]

# Source Code for Module osgeo.utils.gdalident

      1  #!/usr/bin/env python3
      2  # ******************************************************************************
      3  #  $Id$
      4  #
      5  #  Project:  GDAL
      6  #  Purpose:  Application to identify files by format.
      7  #  Author:   Frank Warmerdam, warmerdam@pobox.com
      8  #
      9  # ******************************************************************************
     10  #  Copyright (c) 2007, Frank Warmerdam <warmerdam@pobox.com>
     11  #
     12  #  Permission is hereby granted, free of charge, to any person obtaining a
     13  #  copy of this software and associated documentation files (the "Software"),
     14  #  to deal in the Software without restriction, including without limitation
     15  #  the rights to use, copy, modify, merge, publish, distribute, sublicense,
     16  #  and/or sell copies of the Software, and to permit persons to whom the
     17  #  Software is furnished to do so, subject to the following conditions:
     18  #
     19  #  The above copyright notice and this permission notice shall be included
     20  #  in all copies or substantial portions of the Software.
     21  #
     22  #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     23  #  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24  #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     25  #  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26  #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     27  #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     28  #  DEALINGS IN THE SOFTWARE.
     29  # ******************************************************************************
     30  #
     31
     32  import os
     33  import stat
     34  import sys
     35
     36  from osgeo import gdal
     37
     38
     39  # =============================================================================
     40  #       Usage()
     41  # =============================================================================
     42 -def Usage():
     43      print('Usage: gdalident.py [-r] file(s)')
     44      sys.exit(1)
     45
     46  # =============================================================================
     47  #       ProcessTarget()
     48  # =============================================================================
     49
     50
     51 -def ProcessTarget(target, recursive, report_failure, filelist=None):
     52
     53      if filelist is not None:
     54          driver = gdal.IdentifyDriver(target, filelist)
     55      else:
     56          driver = gdal.IdentifyDriver(target)
     57
     58      if driver is not None:
     59          print('%s: %s' % (target, driver.ShortName))
     60      elif report_failure:
     61          print('%s: unrecognized' % target)
     62
     63      if recursive and driver is None:
     64          try:
     65              mode = os.stat(target)[stat.ST_MODE]
     66          except OSError:
     67              mode = 0
     68
     69          if stat.S_ISDIR(mode):
     70              subfilelist = os.listdir(target)
     71              for item in subfilelist:
     72                  subtarget = os.path.join(target, item)
     73                  ProcessTarget(subtarget, 1, report_failure, subfilelist)
     74
     75
     76 -def main(argv):
     77      recursive = 0
     78      report_failure = 0
     79      files = []
     80
     81      gdal.AllRegister()
     82      argv = gdal.GeneralCmdLineProcessor(argv)
     83      if argv is None:
     84          sys.exit(0)
     85
     86      # Parse command line arguments.
     87      i = 1
     88      while i < len(argv):
     89          arg = argv[i]
     90
     91          if arg == '-r':
     92              recursive = 1
     93
     94          elif arg == '-f':
     95              report_failure = 1
     96
     97          else:
     98              files.append(arg)
     99
    100          i = i + 1
    101
    102      if not files:
    103          Usage()
    104
    105      for f in files:
    106          ProcessTarget(f, recursive, report_failure)
    107
    108
    109  if __name__ == '__main__':
    110      sys.exit(main(sys.argv))
    111