fundamentals.mysql.yaml_to_database

Take key-values from a yaml file including a tablename(s) and add them to a mysql database table

Usage: yaml2db [-d] -s yaml2db [-d] –host= –user= –passwd= –dbName=

Options:

pathToYaml            path to a single yaml file or directory of yaml files
pathToSettingsFile    path to a settings file with logging and database information (yaml file)
--host=<host>         the database host
--user=<user>         database user
--passwd=<passwd>     database user password
--dbName=<dbName>     name of the database to add the table content to

-d, --delete          delete yaml open(s) once added to datbase
-h, --help            show this help message
-v, --version         show version
-s, --settings        the settings file

Module Contents

Classes

yaml_to_database

Take key-values from yaml files including a tablename(s) and add them to a mysql database table

Functions

main

The main function used when yaml_to_database.py when installed as a cl tool

API

fundamentals.mysql.yaml_to_database.main(arguments=None)[source][source]

The main function used when yaml_to_database.py when installed as a cl tool

class fundamentals.mysql.yaml_to_database.yaml_to_database(log, dbConn, pathToInputDir=False, settings=False, deleteFiles=False)[source][source]

Bases: object

Take key-values from yaml files including a tablename(s) and add them to a mysql database table

Key Arguments

  • log – logger

  • settings – the settings dictionary

  • pathToInputDir – path to the directory containing the yaml files that will be added to the database table(s). Default False

  • dbConn – connection to database to add the content to

  • deleteFiles - - delete the yamls files once their content has been added to the database. Default * False*

Usage

To setup your logger, settings and database connections, please use the fundamentals package (see tutorial here https://fundamentals.readthedocs.io/en/master/initialisation.html).

To initiate a yaml2db object, use the following:

from fundamentals.mysql import yaml_to_database
yaml2db = yaml_to_database(
    log=log,
    settings=settings,
    dbConn=dbConn,
    pathToInputDir="/path/to/yaml/directory",
    deleteFiles=False
)

And here’s an example of the content in a yaml file that this yaml2db object can parse:

title: Why you should do most of your text editing in : Sublime Text | Sublime Text Tips
url: http://sublimetexttips.com/why-you-should-do-most-of-your-text-editing-in-sublime-text/?utm_source=drip&utm_medium=email&utm_campaign=editor-proliferation
kind: webpage
subtype: article
table: web_articles,podcasts

Initialization

add_yaml_file_content_to_database(filepath, deleteFile=False)[source][source]

given a file to a yaml file, add yaml file content to database

Key Arguments

  • filepath – the path to the yaml file

  • deleteFile – delete the yaml file when its content has been added to the database. Default False

Return

  • None

Usage

To parse and import the contents of a single yaml file into the database, use the following:

from fundamentals.mysql import yaml_to_database
# PARSE YAML FILE CONTENTS AND ADD TO DATABASE
yaml2db = yaml_to_database(
    log=log,
    settings=settings,
    dbConn=dbConn
)
yaml2db.add_yaml_file_content_to_database(
    filepath=${1:"/path/to/file.yaml"},
    deleteFile=True
)
ingest()[source][source]

ingest the contents of the directory of yaml files into a database

Return

  • None

Usage

To import an entire directory of yaml files into a database, use the following:

from fundamentals.mysql import yaml_to_database
yaml2db = yaml_to_database(
    log=log,
    settings=settings,
    dbConn=dbConn,
    pathToInputDir="/path/to/yaml/directory",
    deleteFiles=False
)
yaml2db.ingest()