Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI for simple services #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cranial/cli/service_template/cookiecutter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"project_name": "my_project",
"listner": ["zmq","file","stdin","postgresql","kafka","db"],
"serde": "json",
"module": "",
"function": ""
}
4 changes: 4 additions & 0 deletions cranial/cli/service_template/hooks/post_gen_project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

cp -rf . ..
rm -rf -- "$(pwd -P)"
11 changes: 11 additions & 0 deletions cranial/cli/service_template/hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
import sys

p = os.path.abspath('..')
sys.path.append(p)

try:
import {{ cookiecutter.module }}
except Exception as e:
print('Could not find module. Is module in the current directory?', 'Error: ', e)
sys.exit(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from cranial.listeners.{{cookiecutter.listner}} import Listener
import {{cookiecutter.module}}
import {{cookiecutter.serde}} as serde

listener = Listener()
while True:
data = listener.recv() # type: bytes
data = serde.loads(data) # type: dict
resp = {{cookiecutter.module}}.{{cookiecutter.function}}(data)
resp = serde.dumps(resp).encode('utf-8')
listener.resp(resp)
21 changes: 21 additions & 0 deletions cranial/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#! /usr/bin/python3
"""
Usage:
generate [<type>]

Possible types are:
service: generate a receiving listener with a custom module and function
which processes bytes from the specified listener
"""

from docopt import docopt
from cookiecutter.main import cookiecutter
import os

opts = docopt(__doc__)

if opts.get('<type>') == 'service':
# todo better way to get to the template
cookiecutter(os.getcwd() + '/cranial/cli/service_template')
else:
print('Only types that can be generated are: service')
16 changes: 11 additions & 5 deletions cranial/messaging/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from cranial.messaging import base
from cranial.common import logger
from cranial.connectors import FileConnector
# from cranial.connectors import file

log = logger.get()

Expand Down Expand Up @@ -53,12 +55,16 @@ def send(self, address=None, message='', endpoint=None, serde=json,
# make sure the path exists for actual local files.
if d != '' and '://' not in endpoint:
os.makedirs(d, exist_ok=True)
self.logfiles[endpoint] = open(endpoint,
'ab' if append else 'wb')
# todo pass to Instantiate File Connector with address and call get
# with endpoint.
# If given a path, break it up into address and endpoint
# self.logfiles[endpoint] = open(endpoint, 'ab' if append else 'wb')
self.logfiles[endpoint] = FileConnector(endpoint)

bytes_written = self.logfiles[endpoint].write(
message + '\n'.encode('utf-8'))
if bytes_written > 0:
# todo instead of write use put and pass in path
bytes_written = self.logfiles[endpoint].put(
message + '\n'.encode('utf-8'), append=append)
if bytes_written is True:
return message
else:
raise Exception("Couldn't write to destination.")
Expand Down