Package deefuzzer :: Package tools :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module deefuzzer.tools.utils

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright (c) 2007-2009 Guillaume Pellerin <yomguy@parisson.com> 
  5  # All rights reserved. 
  6  # 
  7  # This software is licensed as described in the file COPYING, which 
  8  # you should have received as part of this distribution. The terms 
  9  # are also available at http://svn.parisson.org/deefuzz/wiki/DefuzzLicense. 
 10  # 
 11  # Author: Guillaume Pellerin <yomguy@parisson.com> 
 12   
 13  import os 
 14  import re 
 15  import string 
 16  import mimetypes 
 17  from itertools import chain 
 18  from deefuzzer.tools import * 
 19   
 20  mimetypes.add_type('application/x-yaml', '.yaml') 
 21   
 22   
23 -def clean_word(word):
24 """ Return the word without excessive blank spaces, underscores and 25 characters causing problem to exporters""" 26 word = re.sub("^[^\w]+", "", word) # trim the beginning 27 word = re.sub("[^\w]+$", "", word) # trim the end 28 word = re.sub("_+", "_", word) # squeeze continuous _ to one _ 29 word = re.sub("^[^\w]+", "", word) # trim the beginning _ 30 # word = string.replace(word,' ','_') 31 # word = string.capitalize(word) 32 dict = '&[];"*:,' 33 for letter in dict: 34 word = string.replace(word, letter, '_') 35 return word
36 37
38 -def get_file_info(media):
39 file_name = media.split(os.sep)[-1] 40 file_title = file_name.split('.')[:-1] 41 file_title = '.'.join(file_title) 42 file_ext = file_name.split('.')[-1] 43 return file_name, file_title, file_ext
44 45
46 -def is_absolute_path(path):
47 return os.sep == path[0]
48 49
50 -def merge_defaults(setting, default):
51 combined = {} 52 for key in set(chain(setting, default)): 53 if key in setting: 54 if key in default: 55 if isinstance(setting[key], dict) and isinstance(default[key], dict): 56 combined[key] = merge_defaults(setting[key], default[key]) 57 else: 58 combined[key] = setting[key] 59 else: 60 combined[key] = setting[key] 61 else: 62 combined[key] = default[key] 63 return combined
64 65
66 -def replace_all(option, repl):
67 if isinstance(option, list): 68 r = [] 69 for i in option: 70 r.append(replace_all(i, repl)) 71 return r 72 elif isinstance(option, dict): 73 r = {} 74 for key in option.keys(): 75 r[key] = replace_all(option[key], repl) 76 return r 77 elif isinstance(option, str): 78 r = option 79 for key in repl.keys(): 80 r = r.replace('[' + key + ']', repl[key]) 81 return r 82 return option
83 84
85 -def get_conf_dict(file):
86 mime_type = mimetypes.guess_type(file)[0] 87 88 # Do the type check first, so we don't load huge files that won't be used 89 if 'xml' in mime_type: 90 confile = open(file, 'r') 91 data = confile.read() 92 confile.close() 93 return xmltodict(data, 'utf-8') 94 elif 'yaml' in mime_type: 95 import yaml 96 97 def custom_str_constructor(loader, node): 98 return loader.construct_scalar(node).encode('utf-8')
99 100 yaml.add_constructor(u'tag:yaml.org,2002:str', custom_str_constructor) 101 confile = open(file, 'r') 102 data = confile.read() 103 confile.close() 104 return yaml.load(data) 105 elif 'json' in mime_type: 106 import json 107 108 confile = open(file, 'r') 109 data = confile.read() 110 confile.close() 111 return json.loads(data) 112 113 return False 114 115
116 -def folder_contains_music(folder):
117 files = os.listdir(folder) 118 for file in files: 119 filepath = os.path.join(folder, file) 120 if os.path.isfile(filepath): 121 mime_type = mimetypes.guess_type(filepath)[0] 122 if 'audio/mpeg' in mime_type or 'audio/ogg' in mime_type: 123 return True 124 return False
125