Skip to content

Instantly share code, notes, and snippets.

@goanpeca
Created February 24, 2016 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goanpeca/0757298368ff4b02b5fc to your computer and use it in GitHub Desktop.
Save goanpeca/0757298368ff4b02b5fc to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 23 18:37:06 2016
@author: goanpeca
"""
# Standard library imports
import os
# Third party imports
from schematics.types import StringType, BooleanType
from schematics.types.compound import ListType, ModelType, Model
import yaml
class ProyectYaml(Model):
name = StringType(default='')
version = StringType(default='0.1.0')
description = StringType(default='')
default_channels = ListType(StringType, default=[])
class Environment(Model):
name = StringType(default='default')
class Dependency(Model):
conda = ListType(StringType, default=[], serialize_when_none=False)
pip = ListType(StringType, default=[], serialize_when_none=False)
dependencies = ModelType(Dependency, default=Dependency())
environments = ListType(ModelType(Environment), default=Environment())
class Command(Model):
name = StringType(default='default')
windows = StringType(serialize_when_none=False)
unix = StringType(serialize_when_none=False)
environment = StringType(default='default')
commands = ListType(ModelType(Command, default=Command()))
class AnacondaUI(Model):
icon = StringType(default='icon.png', serialize_when_none=False)
is_app = BooleanType(default=False, serialize_when_none=False)
anaconda_ui = ModelType(AnacondaUI, default=AnacondaUI(),
serialize_when_none=False)
DEFAULT_PROJECT_YAML = 'project.yaml'
def commented_dump(schematic_project, path):
# Convert to a dict
project = schematic_project.to_primitive()
# Define some comments per top-level key
comments = {'name': 'Some useful explanation',
'environments': 'Now something on environments',
'commands': 'Commands are awesome',
'anaconda_ui': 'Ignore these keys :-p!'}
# The order of keys is defined by the order in the model definition
ordered_keys = [item[0] for item in schematic_project.items()]
# Some comments template...
comment_template = '# {0}'
# Now we can write to a yaml file per top-level key and add intermediate
# comments as needed
all_data = []
for key in ordered_keys:
dic = {}
dic[key] = project[key]
comment = comments.get(key, None)
if dic[key]:
if comment:
all_data.append(comment_template.format(comment))
data = yaml.safe_dump(dic,
default_flow_style=False,
default_style='',
)
all_data.append(data)
path = os.sep.join([path, DEFAULT_PROJECT_YAML])
with open(path, 'w') as f:
data = '\n'.join(all_data)
f.write(data)
if __name__ == '__main__':
project = {
'name': 'project-name',
'description': 'Something',
'version': '0.1.0',
'default_channels': ['http://repo.continuum.io_pkgs/free',
'http://repo.continuum.io_pkgs/pro'],
'environments': [
{'name': 'default',
'dependencies':
{'conda':
['jupyter', 'redis']
}
},
{'name': 'build',
'dependencies':
{'conda':
['jupyter', 'redis']
}
},
],
'commands': [
{'name': 'default',
'windows': 'jupyter notebook',
'unix': 'jupyter notebook',
'environment': 'default',
},
],
'anaconda_ui': {'is_app': False,
'icon': None}
}
# Load with schematics model
schematic_project = ProyectYaml(project)
schematic_project.validate()
path = os.path.expanduser('~')
commented_dump(schematic_project, path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment