-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
83 lines (54 loc) · 1.84 KB
/
sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
import yaml
import os
from models import Schema
def __create_yaml(path, schema):
""" Cria o yaml de um objeto Schema na pasta 'path' """
stream = file(path+"/"+schema.name+".yaml", 'w')
yaml.safe_dump(schema.to_dic(),
stream=stream,
encoding=('utf-8'),
allow_unicode=True,
default_flow_style=False)
def schema_yaml(path, schema):
""" Cria o yaml para um schema de nome 'schema' na pasta 'path' """
s = Schema.with_name(schema)
if not s:
print "Schema com nome '%s' não encontrado." % (schema,)
else:
__create_yaml(path, s)
def all_yamls(path):
""" Cria na pasta 'path' os yamls
para todos os schemas do banco de dados
"""
schemas = Schema.all()
if not schemas:
print "Nenhum schema encontrado."
for s in schemas:
__create_yaml(path, s)
def __sync(fname):
stream = file(fname, 'r')
print "syncing "+fname
dic = yaml.safe_load(stream=stream)
s = Schema.from_dic(dic)
s.sync_description()
def sync_schema(path, schema):
""" Sincroniza as descrições de um schema no banco de dados
de acordo com as descrições num arquivo 'schema'.yaml no
path especificado.
"""
fname = "%s/%s.yaml" % (path, schema)
if not os.path.isfile(fname):
print ("Arquivo '{0}' para o schema '{1}' não encontrado."
.format(fname, schema))
return
__sync(fname)
def sync_all(path):
""" Sincroniza as descrições de todos schemas cujos yamls
com as descrições estiverem em 'path'
"""
for root, dirs, files in os.walk(path):
for f in files:
fullpath = os.path.join(root, f)
if os.path.splitext(fullpath)[1] == '.yaml':
__sync(fullpath)