Skip to content

Commit

Permalink
refactor: Change setup and fix formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Dec 27, 2020
1 parent 792982a commit 33c52f1
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 119 deletions.
15 changes: 0 additions & 15 deletions Makefile

This file was deleted.

File renamed without changes.
10 changes: 5 additions & 5 deletions README.md → docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This process has largely been manual and is time consuming. Hence, this
python script which will read results from a Google Sheet and generate
the appropriate Google Doc.

![Example Doc Section](.github/example-doc-section.png)
![Example Doc Section](example-doc-section.png)

## Getting Started

Expand All @@ -29,11 +29,11 @@ Create a virtualenv and install the required dependencies from the
requirements.txt file.

```
virtualenv --clear .env
python -m venv .env
. .env/bin/activate
pip install -r requirements.txt
pip install -e .
yarn install
python -m surveydoc
surveydoc
```

### config.json
Expand Down Expand Up @@ -72,7 +72,7 @@ answers, and format the rest as a bulleted list.
The DivergentBarChart option will generate [a divergent bar chart](https://peltiertech.com/diverging-stacked-bar-charts/),
as shown below.

![Example Divergent Bar Chart](.github/example-divergent-bar-chart.png)
![Example Divergent Bar Chart](example-divergent-bar-chart.png)

#### Google Sheet Data Format

Expand Down
File renamed without changes
File renamed without changes
101 changes: 54 additions & 47 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,54 @@
attrs==19.3.0
boto3==1.12.26
botocore==1.15.26
cachetools==4.0.0
certifi==2019.11.28
chardet==3.0.4
click==7.1.1
decorator==4.4.2
docutils==0.15.2
google-api-core==1.16.0
google-api-python-client==1.8.0
google-auth==1.11.3
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.4.1
googleapis-common-protos==1.51.0
httplib2==0.18.0
idna==2.9
importlib-metadata==1.5.0
ipython-genutils==0.2.0
jmespath==0.9.5
jsonschema==3.2.0
jupyter-core==4.6.3
MarkupSafe==1.1.1
more-itertools==8.2.0
nbformat==5.0.4
numpy==1.18.2
oauthlib==3.1.0
pandas==1.0.3
plotly==4.5.4
protobuf==3.11.3
psutil==5.7.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycodestyle==2.5.0
pyrsistent==0.15.7
python-dateutil==2.8.1
pytz==2019.3
requests==2.23.0
requests-oauthlib==1.3.0
retrying==1.3.3
rsa==4.0
s3transfer==0.3.3
six==1.14.0
traitlets==4.3.3
uritemplate==3.0.1
urllib3==1.25.8
zipp==3.1.0
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile
#
appdirs==1.4.4 # via virtualenv
boto3==1.12.26 # via surveydoc (setup.py)
botocore==1.15.26 # via boto3, s3transfer
cachetools==4.0.0 # via google-auth
certifi==2019.11.28 # via requests
chardet==3.0.4 # via requests
click==7.1.1 # via surveydoc (setup.py)
distlib==0.3.1 # via virtualenv
docutils==0.15.2 # via botocore
filelock==3.0.12 # via tox, virtualenv
google-api-core==1.16.0 # via google-api-python-client
google-api-python-client==1.8.0 # via surveydoc (setup.py)
google-auth-httplib2==0.0.3 # via google-api-python-client, surveydoc (setup.py)
google-auth-oauthlib==0.4.1 # via surveydoc (setup.py)
google-auth==1.11.3 # via google-api-core, google-api-python-client, google-auth-httplib2, google-auth-oauthlib
googleapis-common-protos==1.51.0 # via google-api-core
httplib2==0.18.0 # via google-api-python-client, google-auth-httplib2
idna==2.9 # via requests
jmespath==0.9.5 # via boto3, botocore
numpy==1.18.2 # via pandas
oauthlib==3.1.0 # via requests-oauthlib
packaging==20.8 # via tox
pandas==1.0.3 # via surveydoc (setup.py)
plotly==4.5.4 # via surveydoc (setup.py)
pluggy==0.13.1 # via tox
progress==1.5 # via surveydoc (setup.py)
protobuf==3.11.3 # via google-api-core, googleapis-common-protos
psutil==5.7.0 # via surveydoc (setup.py)
py==1.10.0 # via tox
pyasn1-modules==0.2.8 # via google-auth
pyasn1==0.4.8 # via pyasn1-modules, rsa
pyparsing==2.4.7 # via packaging
python-dateutil==2.8.1 # via botocore, pandas
pytz==2019.3 # via google-api-core, pandas
requests-oauthlib==1.3.0 # via google-auth-oauthlib
requests==2.23.0 # via google-api-core, requests-oauthlib
retrying==1.3.3 # via plotly
rsa==4.0 # via google-auth
s3transfer==0.3.3 # via boto3
six==1.14.0 # via google-api-core, google-api-python-client, google-auth, plotly, protobuf, python-dateutil, retrying, tox, virtualenv
toml==0.10.2 # via tox
tox==3.20.1 # via surveydoc (setup.py)
uritemplate==3.0.1 # via google-api-python-client
urllib3==1.25.8 # via botocore, requests
virtualenv==20.2.2 # via tox

# The following packages are considered to be unsafe in a requirements file:
# setuptools
2 changes: 0 additions & 2 deletions setup.cfg

This file was deleted.

29 changes: 22 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
from setuptools import setup
from setuptools import find_packages, setup

__version__ = '0.1.0'

setup(
name = 'surveydoc',
version = '0.1.0',
packages = ['surveydoc'],
entry_points = {
name='surveydoc',
version=__version__,
packages=find_packages(),
python_requires=">=3.6",
entry_points={
'console_scripts': [
'surveydoc = surveydoc.__main__:main'
'surveydoc=surveydoc.__main__:main'
]
}
},
install_requires=[
'boto3',
'click',
'google-api-python-client',
'google-auth-httplib2',
'google-auth-oauthlib',
'pandas',
'plotly',
'psutil',
'progress',
'tox',
],
)
21 changes: 14 additions & 7 deletions surveydoc/__main__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import click
import json
from datetime import datetime
from .google import authenticate, SurveyResultsRepository, DocWriter, DriveManager
from .formatters import DivergentBarChart, RecentResponses
from .aws import S3

import click
from progress.bar import Bar

from .aws import S3
from .formatters import DivergentBarChart, RecentResponses
from .google import authenticate, SurveyResultsRepository, DocWriter, DriveManager


@click.command()
@click.option('--credentials_path', default='credentials.json', type=click.Path(), help='The path to your Google credentials.json file')
@click.option('--credentials_path', default='credentials.json', type=click.Path(),
help='The path to your Google credentials.json file')
@click.option('--config_path', default='config.json', type=click.File(), help='The path to your configuration file.')
def main(credentials_path, config_path):
credentials = authenticate(credentials_path)
Expand Down Expand Up @@ -40,17 +43,21 @@ def main(credentials_path, config_path):
doc_writer.insert_page_break()

if style == 'DivergentBarChart':
image_path = divergent_bar_chart.generate(subject_config['name'], survey_results['answers']['Timestamp'], survey_results['answers'][question], question)
image_path = divergent_bar_chart.generate(subject_config['name'],
survey_results['answers']['Timestamp'],
survey_results['answers'][question])
s3_path = s3.write_to_s3(image_path)
doc_writer.divergent_bar_chart(question, s3_path)
elif style == "TextSummary":
answers = recent_responses.filter(survey_results['answers']['Timestamp'], survey_results['answers'][question])
answers = recent_responses.filter(survey_results['answers']['Timestamp'],
survey_results['answers'][question])
answers = answers.sample(frac=1)

doc_writer.text_summary(question, answers)

document_id = doc_writer.generate_doc("{} {}".format(datetime.now().strftime("%Y-%m"), subject_config['name']))
drive_manager.move_doc_to_folder(document_id, subject_config['drive-folder'])


if __name__ == '__main__':
main()
5 changes: 3 additions & 2 deletions surveydoc/aws.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import boto3
import uuid

import boto3


class S3():
class S3:
def __init__(self, bucket, path):
self.s3 = boto3.resource('s3')
self.bucket = bucket
Expand Down
16 changes: 7 additions & 9 deletions surveydoc/formatters.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import os
import tempfile
import uuid
import pandas as pd
from datetime import datetime

import pandas as pd
import plotly.graph_objs as go
import plotly.io as pio
from string import punctuation
import os

pio.orca.config.executable = './node_modules/orca/bin/orca.js'


class DivergentBarChart():
def generate(self, subject, timestamps, answers, question):
class DivergentBarChart:
def generate(self, subject, timestamps, answers):
answers = answers.apply(lambda x: int(x) if x != '' and x is not None else 0)
answers = answers[answers > 0]
frequencies = pd.crosstab(timestamps, answers, normalize='index')
Expand Down Expand Up @@ -43,8 +43,6 @@ def generate(self, subject, timestamps, answers, question):
if not os.path.exists(subject):
os.mkdir(subject)

translation_table = str.maketrans("", "", punctuation)

image_path = "{}.png".format(os.path.join(tempfile.mkdtemp(), str(uuid.uuid4())))
pio.write_image(figure, image_path, format='png', scale=1, height=30 * len(frequencies))

Expand All @@ -57,12 +55,12 @@ def _add_bar(self, figure, values, months, fill_color):
orientation='h',
marker=dict(
color=fill_color,
line=None
line=dict(width=0)
)
)


class RecentResponses():
class RecentResponses:
def filter(self, timestamps, answers):
indexes = timestamps.loc[timestamps == timestamps.iloc[-1]].index
answers = answers[indexes]
Expand Down
Loading

0 comments on commit 33c52f1

Please sign in to comment.