-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
65 lines (56 loc) · 1.92 KB
/
config.py
File metadata and controls
65 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# config.py
#
# Copyright 2014 Sergio I. Urbina <checor@gmail.com>
#
import ConfigParser
import os
class ConfigReader:
"""Lee toda la info necesaria y la obtiene en un bonito string,
int o float dependiendo de que variable sea. Usa archivos .ini
filename: Nombre del arhivo en la carpera config, de la cual se
obtendra la config
dad: Categoría padre, [Ejemplo]
son: Atributo el cual se desea, Ej = Valor"""
def __init__(self):
self.configs = {}
self.dicc = {}
def get_config(self,filename):
conf = ConfigParser.ConfigParser()
filepath = os.path.join('config', filename)
if os.path.exists(filepath):
conf.read(filepath)
return conf
else:
print "Advertencia: Falta archivo", filename, "en la \
carpeta config"
return
def get_value(self,config, dad, son):
try:
return config.get(dad, son)
except:
print "Avertencia: Config no encontrada: ", son
return "<No value>"
def search(self, filename, dad, son):
key_string = filename + "_" + dad + "_" + son
if self.dicc.has_key(key_string):
return self.dicc[key_string]
else:
if not self.configs.has_key(filename):
self.configs[filename] = self.get_config(filename)
self.dicc[key_string] = self.get_value(self.configs[filename],
dad, son)
else:
self.dicc[key_string] = self.get_value(self.configs[filename],
dad, son)
return self.dicc[key_string]
#class CsvReader:
def main():
#print "Este programa no corre solo. Arranque el archivo main"
c = ConfigReader()
print c.search('info.ini','Empresa', 'Tel')
return 0
if __name__ == '__main__':
main()