text
stringlengths
0
828
)
basename = os.path.basename(pathToYaml)
print ""Content of %(basename)s added to database"" % locals()
else:
from fundamentals.mysql import yaml_to_database
yaml2db = yaml_to_database(
log=log,
settings=settings,
dbConn=dbConn,
pathToInputDir=pathToYaml,
deleteFiles=deleteFlag
)
yaml2db.ingest()
print ""Content of %(pathToYaml)s directory added to database"" % locals()
return"
3835,"def ingest(self):
""""""
*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:
.. code-block:: python
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()
""""""
self.log.debug('starting the ``ingest`` method')
for d in os.listdir(self.pathToInputDir):
if os.path.isfile(os.path.join(self.pathToInputDir, d)) and ""yaml"" in d.lower():
self.add_yaml_file_content_to_database(
filepath=os.path.join(self.pathToInputDir, d),
deleteFile=self.deleteFiles
)
self.log.debug('completed the ``ingest`` method')
return None"
3836,"def add_yaml_file_content_to_database(
self,
filepath,
deleteFile=False
):
""""""*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:
.. code-block:: python
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
)
""""""
self.log.debug(
'completed the ````add_yaml_file_content_to_database`` method')
import codecs
try:
self.log.debug(""attempting to open the file %s"" % (filepath,))
readFile = codecs.open(filepath, encoding='utf-8', mode='r')
thisData = readFile.read()
readFile.close()
except IOError, e:
message = 'could not open the file %s' % (filepath,)
self.log.critical(message)
raise IOError(message)
readFile.close()
matchObject = re.finditer(
r'(^|\n)(?P<key>[^\:]*)\:\s(?P<value>.*?)(\n|$)',