Last active
December 16, 2015 10:18
-
-
Save stormsson/5418583 to your computer and use it in GitHub Desktop.
Python - Parse databases.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import yaml | |
import re | |
import os | |
# definire il path rispetto allo script | |
dbPath = os.path.dirname(os.path.realpath(__file__)) + '/../config/databases.yml' | |
# ritorna dizionario del tipo | |
# {'username': 'root', 'password': 'root'} | |
def readDatabasesyml(path): | |
try: | |
stream = open(path,'r') | |
y = yaml.load(stream); | |
except IOError: | |
print "file ",path," non apribile" | |
exit() | |
result= {} | |
result['username'] = y['all']['propel']['param']['username'] | |
result['password'] = y['all']['propel']['param']['password'] | |
dbmatch = re.search("dbname=(.*?)(\;)?$",y['all']['propel']['param']['dsn']) | |
if(dbmatch): | |
result['database'] = dbmatch.group(1) | |
else: | |
print "match per il database non trovato" | |
exit() | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment