-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from kieras/develop
0.0.3 version
- Loading branch information
Showing
12 changed files
with
206 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
# -*- coding: utf-8 -*- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from cli import main | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
"""The export command.""" | ||
import json | ||
import click | ||
|
||
from .utils import get_datastore_api, get_kinds_list, show_progressbar_item, partition_replace, save | ||
|
||
|
||
@click.command() | ||
@click.option('--project', '-p', | ||
help='GCP project.', | ||
required=True) | ||
@click.option('--namespace', '-n', | ||
help='Datastore namespace.', | ||
required=True) | ||
@click.option('--data-dir', | ||
help='Directory to be used to store exported files.', | ||
required=True, | ||
default='./data', | ||
show_default=True) | ||
@click.option('--project-placeholder', '-pp', | ||
help='Placeholder value to replace the project value in exported files.', | ||
required=True, | ||
default='___PROJECT___', | ||
show_default=True) | ||
@click.option('--namespace-placeholder', '-np', | ||
help='Placeholder value to replace the namespace value in exported files.', | ||
required=True, | ||
default='___NAMESPACE___', | ||
show_default=True) | ||
@click.option('--kinds', '-k', | ||
help='Comma separated list of Datastore Kinds to export.', | ||
required=True) | ||
def export(project, namespace, data_dir, project_placeholder, namespace_placeholder, kinds): | ||
"""Export data from database.""" | ||
kinds_list = get_kinds_list(kinds) | ||
click.echo("Executing export. Project={}, Namespace={}, Kinds={}.".format(project, namespace, kinds_list)) | ||
with click.progressbar(kinds_list, label='Exporting', show_eta=True, | ||
item_show_func=show_progressbar_item) as bar: | ||
for kind in bar: | ||
execute_export(project, namespace, data_dir, project_placeholder, namespace_placeholder, kind) | ||
|
||
|
||
def execute_export(project, namespace, data_dir, project_placeholder, namespace_placeholder, kind): | ||
datastore = get_datastore_api() | ||
|
||
request_body = { | ||
'partitionId': { | ||
'projectId': project, | ||
'namespaceId': namespace | ||
}, | ||
'query': { | ||
'kind': [ | ||
{ | ||
'name': kind | ||
} | ||
] | ||
} | ||
} | ||
|
||
response = datastore.projects() \ | ||
.runQuery(projectId=project, body=request_body) \ | ||
.execute() | ||
|
||
entities = extract_entities(response) | ||
entities_json = json.dumps(entities) | ||
entities_replaced_json = partition_replace(entities_json, project, project_placeholder, namespace, | ||
namespace_placeholder) | ||
entities_replaced = json.loads(entities_replaced_json) | ||
|
||
save(entities_replaced, kind, data_dir) | ||
|
||
|
||
def extract_entities(response): | ||
entities = [] | ||
for entityResult in response.get('batch').get('entityResults'): | ||
entity = entityResult.get('entity') | ||
entities.append(entity) | ||
return entities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -*- coding: utf-8 -*- | ||
"""The import command.""" | ||
import json | ||
import click | ||
|
||
from .utils import get_datastore_api, get_kinds_list, show_progressbar_item, partition_replace, load | ||
|
||
|
||
@click.command('import') | ||
@click.option('--project', '-p', | ||
help='GCP project.', | ||
required=True) | ||
@click.option('--namespace', '-n', | ||
help='Datastore namespace.', | ||
required=True) | ||
@click.option('--data-dir', | ||
help='Directory to be used to store exported files.', | ||
required=True, | ||
default='./data', | ||
show_default=True) | ||
@click.option('--project-placeholder', '-pp', | ||
help='Placeholder value to replace the project value in previously exported files.', | ||
required=True, | ||
default='___PROJECT___', | ||
show_default=True) | ||
@click.option('--namespace-placeholder', '-np', | ||
help='Placeholder value to replace the namespace value in previously exported files.', | ||
required=True, | ||
default='___NAMESPACE___', | ||
show_default=True) | ||
@click.option('--kinds', '-k', | ||
help='Comma separated list of Datastore Kinds to import.', | ||
required=True) | ||
def import_cmd(project, namespace, data_dir, project_placeholder, namespace_placeholder, kinds): | ||
"""Import data to database using previously exported data as input.""" | ||
kinds_list = get_kinds_list(kinds) | ||
click.echo("Executing import. Project={}, Namespace={}, Kinds={}.".format(project, namespace, kinds_list)) | ||
with click.progressbar(kinds_list, label='Importing', show_eta=True, | ||
item_show_func=show_progressbar_item) as bar: | ||
for kind in bar: | ||
execute_import(project, namespace, data_dir, project_placeholder, namespace_placeholder, kind) | ||
|
||
|
||
def execute_import(project, namespace, data_dir, project_placeholder, namespace_placeholder, kind): | ||
datastore = get_datastore_api() | ||
|
||
entities = load(kind, data_dir) | ||
entities_json = json.dumps(entities) | ||
entities_replaced_json = partition_replace(entities_json, project_placeholder, project, | ||
namespace_placeholder, namespace) | ||
entities_replaced = json.loads(entities_replaced_json) | ||
|
||
inserts = [] | ||
for entity in entities_replaced: | ||
insert = { | ||
'insert': entity | ||
} | ||
inserts.append(insert) | ||
|
||
request_body = { | ||
"mutations": inserts, | ||
"mode": "NON_TRANSACTIONAL" | ||
} | ||
|
||
datastore.projects() \ | ||
.commit(projectId=project, body=request_body) \ | ||
.execute() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Utilities.""" | ||
import io | ||
import os | ||
import json | ||
import googleapiclient.discovery | ||
|
||
|
||
def get_datastore_api(): | ||
return googleapiclient.discovery.build('datastore', 'v1') | ||
|
||
|
||
def get_kinds_list(kinds): | ||
return kinds.split(',') | ||
|
||
|
||
def show_progressbar_item(item): | ||
if item is None: | ||
return 'Done!' | ||
return "Kind: {}".format(item) | ||
|
||
|
||
def partition_replace(entities_json, from_project, to_project, from_namespace, to_namespace): | ||
result = entities_json.replace('"projectId": "{}"'.format(from_project), | ||
'"projectId": "{}"'.format(to_project)) | ||
result = result.replace('"namespaceId": "{}"'.format(from_namespace), | ||
'"namespaceId": "{}"'.format(to_namespace)) | ||
return result | ||
|
||
|
||
def save(entities, kind, data_dir): | ||
if not os.path.exists(data_dir): | ||
os.makedirs(data_dir) | ||
|
||
with io.open('{}/{}.json'.format(data_dir, kind), 'w', encoding='utf-8') as export_file: | ||
export_file.write( | ||
json.dumps(entities, ensure_ascii=False, sort_keys=True, indent=2, separators=(',', ': '))) | ||
|
||
|
||
def load(kind, data_dir): | ||
with io.open('{}/{}.json'.format(data_dir, kind), 'r', encoding='utf-8') as export_file: | ||
entities = json.load(export_file) | ||
return entities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ google-auth-httplib2==0.0.3 | |
click==6.7 | ||
tox==2.9.1 | ||
pytest==3.3.2 | ||
pytest-cov==2.5.1 | ||
pytest-cov==2.5.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters