Skip to content

Commit

Permalink
feat(*): add support for Datastore. fix #402 @8.5h
Browse files Browse the repository at this point in the history
  • Loading branch information
hayatedayon committed Aug 4, 2018
1 parent 063afdd commit f6a7866
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 18 deletions.
44 changes: 39 additions & 5 deletions .circleci/config.yml
Expand Up @@ -19,17 +19,51 @@ jobs:
- run: sudo chown -R circleci:circleci /usr/local/lib/python2.7/site-packages
- restore_cache:
keys:
- v1-dependencies-{{ checksum "requirements.txt" }}
- v2-dependencies-{{ .Branch }}-{{ checksum "requirements.txt" }}-{{ checksum ".circleci/config.yml" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- v2-dependencies-
- run:
name: install cache deb file
command: |
if [[ $(find /home/circleci/apt/archives -name "*.deb") == *".deb"* ]];then
sudo dpkg -i /home/circleci/apt/archives/*.deb
fi
- run:
name: install lsb-release and set CLOUD_SDK_REPO
command: |
sudo apt-get -d install lsb-release
if [[ $(find /var/cache/apt/archives -name "*.deb") == *".deb"* ]];then
sudo dpkg -i /var/cache/apt/archives/*.deb
fi
echo 'export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)"' >> $BASH_ENV
- run:
name: install gcloud command and python sdk
command: |
echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update
sudo apt-get -d install google-cloud-sdk google-cloud-sdk-app-engine-python google-cloud-sdk-app-engine-python-extras
if [[ $(find /var/cache/apt/archives -name "*.deb") == *".deb"* ]];then
sudo dpkg -i /var/cache/apt/archives/*.deb
fi
echo 'export PATH=$PATH:/usr/lib/google-cloud-sdk/platform/google_appengine/bin/' >> $BASH_ENV
echo 'export PYTHONPATH=$PYTHONPATH:/usr/lib/google-cloud-sdk/platform/google_appengine/' >> $BASH_ENV
- run:
name: vendor our dependencies
command: pip install -r requirements.txt
- run:
name: copy deb file to cache directory
command: |
if [[ $(find /var/cache/apt/archives -name "*.deb") == *".deb"* ]];then
mkdir -p /home/circleci/apt/archives/
cp -r /var/cache/apt/archives/*.deb /home/circleci/apt/archives/
fi
- save_cache:
key: v1-dependencies-{{ checksum "requirements.txt" }}
key: v2-dependencies-{{ .Branch }}-{{ checksum "requirements.txt" }}-{{ checksum ".circleci/config.yml" }}
paths:
- "/usr/local/bin"
- "/usr/local/lib/python2.7/site-packages"
- "/home/circleci/apt/archives/"
- run:
name: Check PEP 8 for flake8
command: flake8 ./ tests --output-file flake8-test-reports
Expand Down Expand Up @@ -59,9 +93,9 @@ jobs:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "requirements.txt" }}
- v2-dependencies-{{ .Branch }}-{{ checksum "requirements.txt" }}-{{ checksum ".circleci/config.yml" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- v2-dependencies-
- run:
name: Retrieve our secrets from the CircleCI environment
command: echo $CLIENT_SECRET | base64 --decode > ${HOME}/client-secret.json
Expand Down
43 changes: 42 additions & 1 deletion infra.py
Expand Up @@ -18,12 +18,53 @@
from __future__ import division
from __future__ import print_function

from google.appengine.ext import ndb

import json

with open('data/dict.json') as json_file:
json_dic = json.load(json_file)


class User(ndb.Model):
words = ndb.TextProperty()
last_word = ndb.TextProperty()
count = ndb.IntegerProperty()
date = ndb.DateTimeProperty(auto_now_add=True)


def reset_datastore(user_id):
try:
user = User.get_by_id(user_id)
user.key.delete()
except Exception:
pass


def check_word_datastore(user_id, check_word):
try:
user = User.get_by_id(user_id)
words = user.words.split(u',')
if check_word in words:
return False
return True
except Exception:
return True


def save_word_datastore(user_id, save_word):
try:
user = User.get_by_id(user_id)
user.words += u',' + save_word
user.count += 1
except Exception:
user = User(id=user_id)
user.words = save_word
user.count = 1
user.last_word = save_word
user.put()


def search_reading_from_dic(search_word):
"""json形式の辞書ファイルを全探索し、引数の文字列の読みをカタカナで返す
Expand All @@ -35,7 +76,7 @@ def search_reading_from_dic(search_word):
for word in dict_record[u'org']:
if word == search_word:
return dict_record[u'key']
return ''
return u''


def search_word_record_from_dic(search_first):
Expand Down
31 changes: 19 additions & 12 deletions main.py
Expand Up @@ -45,8 +45,9 @@ def post(self):
# responseId = obj['responseId']
# session = obj['session']
queryResult = obj[u'queryResult']
# originalDetectIntentRequest = obj['originalDetectIntentRequest']
originalDetectIntentRequest = obj['originalDetectIntentRequest']
intentDisplayName = queryResult[u'intent'][u'displayName']
userId = originalDetectIntentRequest[u'payload'][u'user'][u'userId']

self.response.headers['Content-Type'] = 'application/json'
if intentDisplayName == GOOGLE_ASSISTANT_WELCOME_INTENT:
Expand All @@ -73,17 +74,23 @@ def post(self):
logging.info(queryText)
reading = infra.search_reading_from_dic(queryText)
if reading:
logging.info(reading)
# FIXME: 暫定実装
word_record = infra.search_word_record_from_dic(
reading[-1])
logging.info(word_record)
word = word_record[u'org'][0]
fulfillmentText = word + u'、の、' + word_record[u'end']
logging.info(word)
obj = {
u'fulfillmentText': fulfillmentText,
}
if infra.check_word_datastore(userId, reading):
infra.save_word_datastore(userId, reading)
logging.info(reading)
# FIXME: 暫定実装
word_record = infra.search_word_record_from_dic(
reading[-1])
logging.info(word_record)
word = word_record[u'org'][0]
fulfillmentText = word + u'、の、' + word_record[u'end']
logging.info(word)
obj = {
u'fulfillmentText': fulfillmentText,
}
else:
obj = {
u'fulfillmentText': u'それは使用済みの言葉です',
}
else:
obj = {
u'fulfillmentText': u'それは知らない言葉です',
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -1,4 +1,5 @@
flake8
pylint
pyyaml
webtest
webapp2
61 changes: 61 additions & 0 deletions tests/test_main.py
Expand Up @@ -18,6 +18,8 @@
from __future__ import division
from __future__ import print_function

from google.appengine.ext import testbed

import json
import unittest
import webtest
Expand All @@ -26,6 +28,15 @@


class TestWebApp(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()

def tearDown(self):
self.testbed.deactivate()

def test_get(self):
app = webtest.TestApp(main.app)

Expand All @@ -47,6 +58,16 @@ def test_post_google_assistant_welcome_intent(self):
},
u'queryText': u'test code',
u'languageCode': u'ja',
},
u'originalDetectIntentRequest':
{
u'payload':
{
u'user':
{
u'userId': u'TestId'
}
}
}
},
)
Expand All @@ -69,6 +90,16 @@ def test_post_ask_continue_intent(self):
},
u'queryText': u'test code',
u'languageCode': u'ja',
},
u'originalDetectIntentRequest':
{
u'payload':
{
u'user':
{
u'userId': u'TestId'
}
}
}
},
)
Expand All @@ -91,6 +122,16 @@ def test_post_ask_word_intent_1(self):
},
u'queryText': u'ASK_WORD_EVENT',
u'languageCode': u'ja',
},
u'originalDetectIntentRequest':
{
u'payload':
{
u'user':
{
u'userId': u'TestId'
}
}
}
},
)
Expand All @@ -112,6 +153,16 @@ def test_post_ask_word_intent_2(self):
},
u'queryText': u'りんご',
u'languageCode': u'ja',
},
u'originalDetectIntentRequest':
{
u'payload':
{
u'user':
{
u'userId': u'TestId'
}
}
}
},
)
Expand All @@ -135,6 +186,16 @@ def test_post_ask_word_intent_3(self):
},
u'queryText': u'ほげほげ',
u'languageCode': u'ja',
},
u'originalDetectIntentRequest':
{
u'payload':
{
u'user':
{
u'userId': u'TestId'
}
}
}
},
)
Expand Down

0 comments on commit f6a7866

Please sign in to comment.