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

[WIP] Modules baby #9

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
6 changes: 6 additions & 0 deletions .env.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

source MONGO_DB_USER=''
source MONGO_DB_PASS=''
source MONGO_DB_URL='mongodb://localhost:27017'
source ISSUE_CREATE_TOKEN=$RANDOM
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
*.pyc
bill/__pycache__/
.serverless
.idea/*
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.8.3/envs/billtracker
14 changes: 14 additions & 0 deletions _setup/00_pyenv_venv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -e

if ! (which pyenv >/dev/null) ; then
echo "!! You must use pyenv to use this setup script. Please install pyenv, pyenv-virtualenv. !!" >&2
exit 1
fi

PYVER=$(cat ./.python-version | cut -d '/' -f 1)

pyenv install -s $PYVER
pyenv virtualenv $PYVER billtracker || echo "Could not create virtualenv -- is it already set up? If so: good."

echo "Setup of venv complete. You should be able to activate with: 'pyenv shell'"
Empty file added billtracker/.python-version
Empty file.
Empty file added billtracker/__init__.py
Empty file.
1 change: 1 addition & 0 deletions billtracker/bill
1 change: 1 addition & 0 deletions billtracker/config.py
1 change: 1 addition & 0 deletions billtracker/issue
1 change: 1 addition & 0 deletions billtracker/result
1 change: 1 addition & 0 deletions billtracker/shitchain
1 change: 1 addition & 0 deletions billtracker/user
32 changes: 32 additions & 0 deletions mode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
import os
import logging


config = AttrDict()


def set_config(env_key, default, from_string_f=lambda x: x, private=False):
nonlocal config
if env_key not in os.environ or not os.environ[env_key]:
config[env_key] = default if not callable(default) else default()
else:
config[env_key] = from_string_f(os.getenv(env_key))
if not private:
logging.info("Set config k,v: `%s`,`%s`" % (env_key, config[env_key]))
else:
logging.info("Set config k,v: `%s` (private)" % (env_key,))
if config[env_key] is None:
raise Exception(f'set_config error: var: {env_key}, default: {default}, value: {config[env_key]}')


set_config('MONGODB_URI', 'mongodb://localhost:27017/billtracker-local-dev', private=True)

set_config('MONGO_DB_USER', None)
set_config('MONGO_DB_PASS', None, private=True)
set_config('MONGO_DB_URL', None)
set_config('MONGO_DB_NAME', None)



# These are _dict keys_, not values; values are set in `ms` below
USER = 'user'
PWD = 'pwd'
MONGODB = 'mongodb'
Expand All @@ -11,6 +40,7 @@
BALLOTSPECSCOLLECTION = "ballotspecscollection"
URL = 'url'

# to be used as values, sometimes
user = os.environ['MONGO_DB_USER']
pwd = os.environ['MONGO_DB_PASS']
url = os.environ['MONGO_DB_URL']
Expand Down Expand Up @@ -57,4 +87,6 @@
}


ms.update(config=config)

mongosettings = ms
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pymongo
dnspython
ausbills
python-dotenv==0.13.0
pytest==5.4.3
web3==5.11.0
attrdict==2.0.1
1 change: 1 addition & 0 deletions scripts/update_ballotspecs_db.py
1 change: 1 addition & 0 deletions scripts/update_bills_db.py
1 change: 1 addition & 0 deletions scripts/update_issues_db.py
1 change: 1 addition & 0 deletions scripts/update_results_db.py
48 changes: 31 additions & 17 deletions update_ballotspecs_db.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
import logging
import os
from binascii import hexlify

import pymongo
from mode import *
import hashlib
import requests


# Data for each document
ID = "id"
SHORT_TITLE = "short_title"
Expand All @@ -22,11 +24,19 @@
db = client[mongosettings[MONGODB]]
ballotspecs_collection = db[mongosettings[BALLOTSPECSCOLLECTION]]

log = logging.getLogger(__name__)

def hash_ballotspec(ballotspec_string):
h = hashlib.sha256()
h.update(str(ballotspec_string).encode('utf-8'))
return(h.hexdigest())
return (h.hexdigest())


def push_to_chain(method, params):
log.info(f"Pushing to BC API: {json.dumps(params)}")
r = requests.post("https://api.blockchain.suzuka.flux.party/api", data={"method": method, "params": params})
log.info(f"push_to_chain Response: {r}\n\n-- Response content {r.content}\n\n-- As text: {r.text}")
return r


def update_ballotspecs(id, short_title, question, description, start_date, chamber, sponsor):
Expand All @@ -47,29 +57,33 @@ def update_ballotspecs(id, short_title, question, description, start_date, chamb
"ballotTitle": id,
"longDesc": issue_string,
"shortDesc": short_title,
"ballotVersion" : 2,
"optionsVersion" : 1,
"ballotVersion": 2,
"optionsVersion": 1,
}
bs_h = hash_ballotspec(json.dumps(ballotspec_dict))
## Post to the blochain
ballot_spec_sz = json.dumps(ballotspec_dict)
bs_h = hash_ballotspec(ballot_spec_sz)

to_api = {
"method" : "ballot_publish",
"params": {
"specHash": bs_h
}
}

print(to_api)
r = requests.post("https://api.blockchain.suzuka.flux.party/api", data=to_api)
print(r.text)
def render_spec_hash(_s):
if isinstance(_s, str) or type(_s) is str:
if _s[:2] != "0x" or len(_s) != 66:
return "0x" + ("00" * (32 - len(_s)) + hexlify(_s).decode('ascii'))
return _s

try:
# Post to API => posts the blockchain
push_to_chain("ballot_publish", {
"specHash": render_spec_hash(id),
"ballotSpec": ballot_spec_sz,
"realSpecHash": bs_h
})
except Exception as e:
import traceback
log.error(f"Error pushing to chain: {e}\n\n{traceback.format_tb(e.__traceback__)}\n\nCONTINUING")

try:
ballotspecs_collection.insert_one(
{'_id': input_dict["id"],
'data': input_dict,
BALLOTSPEC_HASH: bs_h})

except Exception as e:
print(e)