easydoneit-cli

Easydoneit cli is a simple task manager for terminals.
git clone https://noulin.net/git/easydoneit-cli.git
Log | Files | Refs | README | LICENSE

edi_common.py (2894B)


      1 #!/usr/local/util/bin/python                                                                                                 
      2 #
      3 ## @package edi_common
      4 # Common data and functions for Easydoneit CLI.
      5 # python style<br>
      6 # basic functions that can be reused outside easydoneit<br>
      7 #
      8 # Copyright (C) 2014 Spartatek AB
      9 #
     10 # contact@spartatek.se
     11 # http://spartatek.se
     12 #
     13 # EASYDONEIT CLI is free software: you can redistribute it and/or modify
     14 # it under the terms of the GNU Genereric Public License as published by
     15 # the Free Software Foundation, either version 3 of the License, or
     16 # (at your option) any later version.
     17 #
     18 # EASYDONIT CLI is distributed in the hope that it will be usesul,
     19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     21 # GNU Genereral Public License for more details.
     22 #
     23 # You should have received a copy of the GNU General Public License
     24 # along with this program. If not, see
     25 # GNU licences http://www.gnu.org/licenses
     26 
     27 import os
     28 import logging
     29 
     30 #==========================================================================
     31 # Setup logger
     32 #==========================================================================
     33 if __name__ == '__main__':
     34   console = logging.StreamHandler() #pragma: no cover
     35   formatter = logging.Formatter('%(levelname)-8s (%(name)s) %(message)s') #pragma: no cover
     36   console.setFormatter(formatter) #pragma: no cover
     37   console.setLevel(logging.INFO) #pragma: no cover
     38   logging.getLogger('').addHandler(console) #pragma: no cover
     39 logger = logging.getLogger(os.path.basename(__file__))
     40 logger.setLevel(logging.INFO);
     41 #==========================================================================
     42 # Logger has been set up
     43 #==========================================================================
     44 
     45 
     46 
     47 #==========================================================================
     48 
     49 ## Find all files in tree path.
     50 # @return list of paths for files
     51 # @param[in] path to tree
     52 # @ingroup EDI_CORE
     53 def ffind(path, namefs=None, relative=True):
     54     """
     55     Finds files in the directory tree starting at 'path' (filtered by the
     56     functions in the optional 'namefs' sequence); if the 'relative'
     57     flag is not set, the result sequence will contain absolute paths.                                                        
     58 
     59     Returns a sequence of paths for files found.
     60     """
     61     if not os.access(path, os.R_OK):
     62         logger.error("cannot access path: '%s'" % path) #pragma: no cover
     63 
     64     fileList = []
     65     try:
     66         for dir, subdirs, files in os.walk(path):
     67             fileList.extend(['%s%s%s' % (dir, os.sep, f) for f in files])
     68         if not relative: fileList = map(os.path.abspath, fileList)
     69         if namefs: 
     70             for ff in namefs: fileList = filter(ff, fileList) #pragma: no cover
     71     except Exception, e: logger.error(str(e)) #pragma: no cover
     72     return(fileList)