Skip to content

Commit

Permalink
Merge pull request kiyoto-suzuki#8 from tomohiko-furumoto/support-edi…
Browse files Browse the repository at this point in the history
…tor-json

support editor json
  • Loading branch information
kiyoto-suzuki committed Apr 22, 2015
2 parents f6bdf89 + cca5ba0 commit a1e481a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
31 changes: 26 additions & 5 deletions script/build.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-

from collections import OrderedDict
import os
import sys
import re
Expand Down Expand Up @@ -49,6 +50,9 @@ def __init__(self, target=None, asset_version=None, top_dir=None, user_dir=None,
self.local_asset_search_path = self.user_dir+'/contents'
self.user_xlsx_dir = self.user_dir+'/master'

user_editor_json = self.user_dir+'/editor/editor_data.json'
self.editor_json = user_editor_json if os.path.exists(user_editor_json) else self.top_dir+'/editor/editor_data.json'

self.manifest_dir = self.top_dir+'/manifests'
self.xlsx_dir = self.top_dir+'/master'
self.schema_dir = self.top_dir+'/master_derivatives'
Expand Down Expand Up @@ -134,6 +138,22 @@ def build_json(self, src_xlsxes=None, dest_schema=None, dest_data=None):
check_call(cmdline)
return True

# merge editor's json data into the master json data
def merge_editor_json(self):
json_file = self.build_dir+'/'+self.JSON_DATA_FILE
with open(json_file, 'r') as f:
json_data = json.loads(f.read(), object_pairs_hook=OrderedDict)

with open(self.editor_json, 'r') as f:
editor_json_data = json.loads(f.read())

for key in editor_json_data:
json_data[key] = editor_json_data[key]

with open(json_file, 'w') as f:
j = json.dumps(json_data, ensure_ascii = False, indent = 4)
f.write(j.encode("utf-8"))

# create fbs from json
def build_fbs(self, src_json=None, dest_fbs=None, root_type=None, name_space=None):
src_json = src_json or self.build_dir+'/'+self.JSON_DATA_FILE
Expand Down Expand Up @@ -237,21 +257,22 @@ def deploy_dev(self):
# do all processes
def build_all(self, check_modified=True):
# check modified
xlsxes = self._get_xlsxes()
build_depends = self._get_xlsxes() + [self.editor_json]
modified = False
if check_modified:
for xlsx in xlsxes:
if self._check_modified(xlsx, self.data_dir+'/'+self.JSON_DATA_FILE):
for src in build_depends:
if self._check_modified(src, self.bin_dir+'/'+self.BIN_FILE):
modified = True
break
if not modified:
info("xlsxes are not modified")
info("xlsxes and editor data are not modified")

# main process
try:
self.setup_dir()
if not check_modified or modified:
self.build_json(xlsxes)
self.build_json()
self.merge_editor_json()
self.build_fbs()
self.build_bin()
self.build_manifest()
Expand Down
56 changes: 28 additions & 28 deletions script/master_data_xls2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,35 @@ def normalize(data, schema, target):
for table in data['table']:
if table['type'].find('ignore') >= 0:
continue
primary_key = table['primaryKey']
version_key = table['versionKey']

filtered = []
id_mapping = OrderedDict()
for d in data[table['name']]:
if version_key in d:
if d[version_key] != '' and d[version_key] != target:
continue
del d[version_key] # delete versionKey

# filter by primary key
if d[primary_key] is not None:
if d[primary_key] >= 0:
id_mapping[d[primary_key]] = d # override
elif d[primary_key] < 0:
id = abs(d[primary_key])
if id in id_mapping:
del id_mapping[id] # delete
else:
warning("no primary key record: %s: %s" % (table['name'], d))

# object -> list
filtered = []
for id in id_mapping:
filtered.append(id_mapping[id])

if table['type'].find('object') >= 0:
filtered = filtered[0] # by object, not object array
if table['type'].find('json') < 0:
primary_key = table['primaryKey']
version_key = table['versionKey']

id_mapping = OrderedDict()
for d in data[table['name']]:
if version_key in d:
if d[version_key] != '' and d[version_key] != target:
continue
del d[version_key] # delete versionKey

# filter by primary key
if d[primary_key] is not None:
if d[primary_key] >= 0:
id_mapping[d[primary_key]] = d # override
elif d[primary_key] < 0:
id = abs(d[primary_key])
if id in id_mapping:
del id_mapping[id] # delete
else:
warning("no primary key record: %s: %s" % (table['name'], d))

# object -> list
for id in id_mapping:
filtered.append(id_mapping[id])

if table['type'].find('object') >= 0:
filtered = filtered[0] # by object, not object array
normalized[table['name']] = filtered
return normalized

Expand Down
11 changes: 11 additions & 0 deletions watchman/watchman-editor-data.json.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
["trigger", "__ROOT_DIR__/editor", {
"name": "editor-data",
"expression": ["suffix", "json"],
"ignore_dirs": ["watchman", ".git"],
"command": ["__TOOL_DIR__/watchman/watchman-callback.sh", "__ROOT_DIR__", "5"],
"stdin": ["name", "exists", "new", "size"],
"stdout": ">>__TOOL_DIR__/watchman/watchman-callback.log",
"stderr": ">>__TOOL_DIR__/watchman/watchman-callback.log",
"chdir": "__TOOL_DIR__"
}]

0 comments on commit a1e481a

Please sign in to comment.