Source code for inetsix.tools

# __license__ = "GPL"
# __docformat__ = 'reStructuredText'

import pprint
import os
import jinja2
import colorful
import logging
import urllib2
from glob import glob


################################################################
#           Class to manage tooling
################################################################


[docs]class InetsixTools(object): """Class to expose some useful functions for day to day scripting. InetsixTools class provides a list of functions used in different script. Using such class to centralize tools help to maintain and enhance code instead of updating all scripts to be enhanced. """
[docs] @staticmethod def load_constant(key_name,default='UNSET', verbose=False): """Set up constant value from OS Environment. Help to define CONSTANT from OS Environment. If it is not defined, then, fallback to default value provided within parameters :Example: >>> from inetsix.tools import InetsixTools >>> CVP_USER = InetsixTools.load_constant(key_name='CVP_USER', default='username') >>> print CVP_USER myUsername :param key_name: VAR to lookup in os.environment :type key_name: str :param default: Default value to use if key_name is not defined. :type default: str :param verbose: Boolean to activate verbos mode (Optional, expected values: debug level) :type verbose: bool :returns: Value for variable :rtype: str """ if key_name in os.environ: if verbose: logging.debug( "%s is set to %s",key_name, os.environ.get(key_name)) return os.environ[key_name] else: if verbose: logging.debug(key_name+" is not set - using default ("+ str(default) +')') return default
[docs] @staticmethod def jprint( data=list() ): """PPrint wrapper to display list and dict. :param data: data to display python structure :type data: list or dict """ pp = pprint.PrettyPrinter(indent=4) pp.pprint(data)
[docs] @staticmethod def get_list_files( path=None, extension="j2", debug=False ): """List files in a specific directory Return a list of files found in a directory. A filter can be used to search specific file extensions. :param path: Path to search for file :type path: str :param extension: Extension to search files. Default is ``j2`` :type extension: str :param debug: Trigger to enable verbosity :type debug: bool :returns: A list of files found in ``path`` :rtype: list """ #result = [y for x in os.walk(path) for y in glob(os.path.join(x[0], '*.'+extension))] result = list() for root, dirs, files in os.walk(path): for t_file in files: if t_file.endswith("."+extension): if debug is True: logging.debug('found: %s', t_file) result.append(t_file) return result
[docs] @staticmethod def file_basename( file_path ): """Extract Filename from complete path. :param file_path: file path to a file :type file_path: str :returns: filename without path :rtype: str """ fname = os.path.basename(file_path) return os.path.splitext(fname)[0]
[docs] @staticmethod def list_unique_values( myList = list() ): """Remove duplicate entries in a list. :param myList: List where duplicates must be removed :type myList: list() :returns: List without duplicates :rtype: list() """ return set(myList)
[docs] @staticmethod def dict_to_list( dict_input=None, key=None): """Transform a dictionary to a list. :param dict_input: Dictionary used to create list :type dict_input: dict() :param key: If defined, only value from this key is extracted :type key: str :returns: A list of entries :rtype: list() """ output=list() for entry in dict_input: # print entry if key in entry: output.append(entry[key]) return output
[docs] @staticmethod def download_file( remote_file=None, verbose=False): """Function to download file from HTTP server. Download file from an HTTP or HTTPS server and save file locally :param remote_file: URL to download :type remote_file: str :param verbose: Manage verbosity level :returns: File name to access to local copy of the file :rtype: str """ url = remote_file file_name = url.split('/')[-1] u = urllib2.urlopen(url) f = open(file_name, 'wb') meta = u.info() file_size = int(meta.getheaders("Content-Length")[0]) if verbose is True: print " * Downloading: %s Bytes: %s" % (file_name, file_size) file_size_dl = 0 block_sz = 8192 while True: buffer = u.read(block_sz) if not buffer: break file_size_dl += len(buffer) f.write(buffer) if verbose is True: status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) status = status + chr(8)*(len(status)+1) print " > "+status, f.close() return file_name