Skip to content

Instantly share code, notes, and snippets.

@stormsson
Last active December 16, 2015 10:18
Show Gist options
  • Save stormsson/5418583 to your computer and use it in GitHub Desktop.
Save stormsson/5418583 to your computer and use it in GitHub Desktop.
Python - Parse databases.yml
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