labpack/records/settings.py
def save_settings(file_path, record_details, overwrite=False, secret_key=''):

    ''' a method to save dictionary typed data to a local file

    :param file_path: string with path to settings file
    :param record_details: list or dictionary with record details
    :param overwrite: [optional] boolean to overwrite existing file data
    :param secret_key: [optional] string with key to decrypt drep file
    :return: string with file path
    '''

# validate inputs
    title = 'save_settings'
    try:
        _path_arg = '%s(file_path=%s)' % (title, str(file_path))
    except:
        raise ValueError('%s(file_path=...) must be a string.' % title)
    _details_arg = '%s(record_details={...})' % title
    if not isinstance(record_details, dict) and not isinstance(record_details, list):
        raise ValueError('%s must be a list or dictionary.' % _details_arg)
    if secret_key:
        try:
            _secret_arg = '%s(secret_key=%s)' % (title, str(secret_key))
        except:
            raise ValueError('%s(secret_key=...) must be a string.' % title)

# parse extension type
    ext_map = {}
    file_extensions = {
        "json": ".+\\.json$",
        "json.gz": ".+\\.json\\.gz$",
        "yaml": ".+\\.ya?ml$",
        "yaml.gz": ".+\\.ya?ml\\.gz$",
        "drep": ".+\\.drep$"
    }
    import re
    for key, value in file_extensions.items():
        file_pattern = re.compile(value)
        if file_pattern.findall(file_path):
            ext_map[key] = True
        else:
            ext_map[key] = False

# construct file data
    file_time = 0
    file_data = ''.encode('utf-8')
    if ext_map['json']:
        import json
        file_data = json.dumps(record_details, indent=2).encode('utf-8')
    elif ext_map['yaml']:
        import yaml
        file_data = yaml.dump(record_details).encode('utf-8')
    elif ext_map['json.gz']:
        import json
        import gzip
        file_bytes = json.dumps(record_details).encode('utf-8')
        file_data = gzip.compress(file_bytes)
    elif ext_map['yaml.gz']:
        import yaml
        import gzip
        file_bytes = yaml.dump(record_details).encode('utf-8')
        file_data = gzip.compress(file_bytes)
    elif ext_map['drep']:
        from labpack.compilers import drep
        file_data = drep.dump(record_details, secret_key)
        file_time = 1
    else:
        raise ValueError('%s must be one of %s file types.' % (_path_arg, list(ext_map.keys())))

# check overwrite exception
    import os
    if not overwrite:
        if os.path.exists(file_path):
            raise Exception('%s already exists. To overwrite %s, set overwrite=True' % (_path_arg, _path_arg))

# create directories in path to file
    dir_path = os.path.split(file_path)
    if dir_path[0]:
        if not os.path.exists(dir_path[0]):
            os.makedirs(dir_path[0])

# write data to file
    with open(file_path, 'wb') as f:
        f.write(file_data)
        f.close()

# eliminate update and access time metadata (for drep files)
    if file_time:
        os.utime(file_path, times=(file_time, file_time))

# TODO add windows creation time wiping
# http://stackoverflow.com/questions/4996405/how-do-i-change-the-file-creation-date-of-a-windows-file-from-python

    return file_path
 a method to save dictionary typed data to a local file

    :param file_path: string with path to settings file
    :param record_details: list or dictionary with record details
    :param overwrite: [optional] boolean to overwrite existing file data
    :param secret_key: [optional] string with key to decrypt drep file
    :return: string with file path
    
a method to save dictionary typed data to a local file
a method to save dictionary typed data to a local file
def save_settings(file_path, record_details, overwrite=False, secret_key=''):
    title = 'save_settings'
    try:
        _path_arg = '%s(file_path=%s)' % (title, str(file_path))
    except:
        raise ValueError('%s(file_path=...) must be a string.' % title)
    _details_arg = '%s(record_details={...})' % title
    if not isinstance(record_details, dict) and not isinstance(record_details, list):
        raise ValueError('%s must be a list or dictionary.' % _details_arg)
    if secret_key:
        try:
            _secret_arg = '%s(secret_key=%s)' % (title, str(secret_key))
        except:
            raise ValueError('%s(secret_key=...) must be a string.' % title)
    ext_map = {}
    file_extensions = {
        "json": ".+\\.json$",
        "json.gz": ".+\\.json\\.gz$",
        "yaml": ".+\\.ya?ml$",
        "yaml.gz": ".+\\.ya?ml\\.gz$",
        "drep": ".+\\.drep$"
    }
    import re
    for key, value in file_extensions.items():
        file_pattern = re.compile(value)
        if file_pattern.findall(file_path):
            ext_map[key] = True
        else:
            ext_map[key] = False
    file_time = 0
    file_data = ''.encode('utf-8')
    if ext_map['json']:
        import json
        file_data = json.dumps(record_details, indent=2).encode('utf-8')
    elif ext_map['yaml']:
        import yaml
        file_data = yaml.dump(record_details).encode('utf-8')
    elif ext_map['json.gz']:
        import json
        import gzip
        file_bytes = json.dumps(record_details).encode('utf-8')
        file_data = gzip.compress(file_bytes)
    elif ext_map['yaml.gz']:
        import yaml
        import gzip
        file_bytes = yaml.dump(record_details).encode('utf-8')
        file_data = gzip.compress(file_bytes)
    elif ext_map['drep']:
        from labpack.compilers import drep
        file_data = drep.dump(record_details, secret_key)
        file_time = 1
    else:
        raise ValueError('%s must be one of %s file types.' % (_path_arg, list(ext_map.keys())))
    import os
    if not overwrite:
        if os.path.exists(file_path):
            raise Exception('%s already exists. To overwrite %s, set overwrite=True' % (_path_arg, _path_arg))
    dir_path = os.path.split(file_path)
    if dir_path[0]:
        if not os.path.exists(dir_path[0]):
            os.makedirs(dir_path[0])
    with open(file_path, 'wb') as f:
        f.write(file_data)
        f.close()
    if file_time:
        os.utime(file_path, times=(file_time, file_time))
    return file_path
def save_settings ( file_path , record_details , overwrite = False , secret_key = '' ) : title = 'save_settings' try : _path_arg = '%s(file_path=%s)' % ( title , str ( file_path ) ) except : raise ValueError ( '%s(file_path=...) must be a string.' % title ) _details_arg = '%s(record_details={...})' % title if not isinstance ( record_details , dict ) and not isinstance ( record_details , list ) : raise ValueError ( '%s must be a list or dictionary.' % _details_arg ) if secret_key : try : _secret_arg = '%s(secret_key=%s)' % ( title , str ( secret_key ) ) except : raise ValueError ( '%s(secret_key=...) must be a string.' % title ) ext_map = { } file_extensions = { "json" : ".+\\.json$" , "json.gz" : ".+\\.json\\.gz$" , "yaml" : ".+\\.ya?ml$" , "yaml.gz" : ".+\\.ya?ml\\.gz$" , "drep" : ".+\\.drep$" } import re for key , value in file_extensions . items ( ) : file_pattern = re . compile ( value ) if file_pattern . findall ( file_path ) : ext_map [ key ] = True else : ext_map [ key ] = False file_time = 0 file_data = '' . encode ( 'utf-8' ) if ext_map [ 'json' ] : import json file_data = json . dumps ( record_details , indent = 2 ) . encode ( 'utf-8' ) elif ext_map [ 'yaml' ] : import yaml file_data = yaml . dump ( record_details ) . encode ( 'utf-8' ) elif ext_map [ 'json.gz' ] : import json import gzip file_bytes = json . dumps ( record_details ) . encode ( 'utf-8' ) file_data = gzip . compress ( file_bytes ) elif ext_map [ 'yaml.gz' ] : import yaml import gzip file_bytes = yaml . dump ( record_details ) . encode ( 'utf-8' ) file_data = gzip . compress ( file_bytes ) elif ext_map [ 'drep' ] : from labpack . compilers import drep file_data = drep . dump ( record_details , secret_key ) file_time = 1 else : raise ValueError ( '%s must be one of %s file types.' % ( _path_arg , list ( ext_map . keys ( ) ) ) ) import os if not overwrite : if os . path . exists ( file_path ) : raise Exception ( '%s already exists. To overwrite %s, set overwrite=True' % ( _path_arg , _path_arg ) ) dir_path = os . path . split ( file_path ) if dir_path [ 0 ] : if not os . path . exists ( dir_path [ 0 ] ) : os . makedirs ( dir_path [ 0 ] ) with open ( file_path , 'wb' ) as f : f . write ( file_data ) f . close ( ) if file_time : os . utime ( file_path , times = ( file_time , file_time ) ) return file_path
a method to save dictionary typed data to a local file
a method to save dictionary typed data to a local file
''' a method to save dictionary typed data to a local file

    :param file_path: string with path to settings file
    :param record_details: list or dictionary with record details
    :param overwrite: [optional] boolean to overwrite existing file data
    :param secret_key: [optional] string with key to decrypt drep file
    :return: string with file path
    ''' # validate inputs
 # parse extension type
 # construct file data
 # check overwrite exception
 # create directories in path to file
 # write data to file
 # eliminate update and access time metadata (for drep files)
 # TODO add windows creation time wiping
 # http://stackoverflow.com/questions/4996405/how-do-i-change-the-file-creation-date-of-a-windows-file-from-python

string with file path
string with file path
file_path
string with path to settings file
string with path to settings file
record_details
list or dictionary with record details
list or dictionary with record details
overwrite
[optional] boolean to overwrite existing file data
[ optional ] boolean to overwrite existing file data
secret_key
[optional] string with key to decrypt drep file
[ optional ] string with key to decrypt drep file