Skip to content

Commit 69943a3

Browse files
authored
Merge pull request #8 from HBClab/octothorpe
[REF] Refactor Code
2 parents d01aa99 + 0311212 commit 69943a3

File tree

12 files changed

+344
-24
lines changed

12 files changed

+344
-24
lines changed

.gitignore

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,141 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
local_settings.py
60+
db.sqlite3
61+
db.sqlite3-journal
62+
63+
# Flask stuff:
64+
instance/
65+
.webassets-cache
66+
67+
# Scrapy stuff:
68+
.scrapy
69+
70+
# Sphinx documentation
71+
docs/_build/
72+
73+
# PyBuilder
74+
.pybuilder/
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
# For a library or package, you might want to ignore these files since the code is
86+
# intended to run in multiple environments; otherwise, check them in:
87+
# .python-version
88+
89+
# pipenv
90+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
91+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
92+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
93+
# install all needed dependencies.
94+
#Pipfile.lock
95+
96+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
97+
__pypackages__/
98+
99+
# Celery stuff
100+
celerybeat-schedule
101+
celerybeat.pid
102+
103+
# SageMath parsed files
104+
*.sage.py
105+
106+
# Environments
107+
.env
108+
.venv
109+
env/
110+
venv/
111+
ENV/
112+
env.bak/
113+
venv.bak/
114+
115+
# Spyder project settings
116+
.spyderproject
117+
.spyproject
118+
119+
# Rope project settings
120+
.ropeproject
121+
122+
# mkdocs documentation
123+
/site
124+
125+
# mypy
126+
.mypy_cache/
127+
.dmypy.json
128+
dmypy.json
129+
130+
# Pyre type checker
131+
.pyre/
132+
133+
# pytype static type analyzer
134+
.pytype/
135+
136+
# Cython debug symbols
137+
cython_debug/
138+
139+
# user defined
1140
*.html
2-
*.log
141+

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
anguage: python
2+
3+
services:
4+
- docker
5+
6+
before_install:
7+
- docker build -t hbclab/log-html .
8+
9+
script:
10+
- docker run --entrypoint /bin/bash hbclab/log-html -c ". activate log && pytest /home/coder/projects/log-html"
11+
12+
deploy:
13+
provider: script
14+
script: bash docker_push.sh
15+
on:
16+
all_branches: true

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ COPY --chown=coder . /home/coder/projects/
2424
RUN conda env create -f environment.yml
2525
RUN bash -c 'conda init && . /home/coder/.bashrc'
2626

27-
ENTRYPOINT ["/opt/miniconda-latest/envs/log/bin/python", "/home/coder/projects/logToHtml.py"]
27+
ENTRYPOINT ["/opt/miniconda-latest/envs/log/bin/python", "/home/coder/projects/log-html/logToHtml.py"]

docker_push.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
echo "$DOCKER_PASS" | docker login --username "$DOCKER_USERNAME" --password-stdin
3+
# if tag is defined
4+
if [[ ! -z "$TRAVIS_TAG" ]]; then
5+
docker tag hbclab/log-html:latest hbclab/log-html:$TRAVIS_TAG
6+
docker push hbclab/log-html:$TRAVIS_TAG
7+
# if the build is on the master branch
8+
elif [[ "$TRAVIS_BRANCH" == "master" ]]; then
9+
docker tag hbclab/log-html:latest hbclab/log-html:unstable
10+
docker push hbclab/log-html:unstable
11+
else
12+
echo "This build is not on the master branch or a tagged release"
13+
fi

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
- wheel=0.34.2
2222
- xz=5.2.5
2323
- zlib=1.2.11
24+
- pytest
2425
- pip:
2526
- jinja2==2.11.2
2627
- markupsafe==1.1.1

log-html/__init__.py

Whitespace-only changes.

logToHtml.py renamed to log-html/logToHtml.py

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import os
2+
import re
23
import jinja2
34
from argparse import ArgumentParser, RawTextHelpFormatter
45
from glob import glob
56

7+
DEFAULT_TEMPLATE = os.path.join(os.path.dirname(__file__), "templates", "temp.tpl")
8+
9+
10+
class Accel():
11+
def __init__(self, filename, status, path, date, time):
12+
self.filename = filename
13+
self.status = status
14+
if status == 'ERROR':
15+
self.css = 'redtext'
16+
else:
17+
self.css = 'greentext'
18+
self.path = path
19+
self.date = date
20+
self.time = time
21+
622

723
def build_parser():
824
parser = ArgumentParser(
@@ -11,25 +27,45 @@ def build_parser():
1127
)
1228
parser.add_argument('log_dir', help='(abs path) directory where log '
1329
'files are mounted')
14-
parser.add_argument('output_file', help='location of the html output')
30+
parser.add_argument('output_file', help='name of the HTML file')
1531
parser.add_argument('--template-file', help='location of the html template',
16-
required=False, default='templates/temp.tpl')
32+
required=False, default=DEFAULT_TEMPLATE)
1733

1834
return parser
1935

36+
def parse_log_file(fil):
37+
38+
# read all contents of log file
39+
contents = open(fil, 'r').read()
40+
41+
# assume log entries are separated by at least 5 #'s
42+
try:
43+
entry_separator = re.search(r"[#]{5,}", contents).group()
44+
except AttributeError:
45+
raise ValueError("Cannot find at least 5 #'s separating log entries")
46+
47+
# assume the last entry is the most recent
48+
last_entry = contents.split(entry_separator)[-1]
49+
50+
# find the date
51+
try:
52+
date = re.search("[0-9]{4}-[0-9]{2}-[0-9]{2}", last_entry).group()
53+
except AttributeError:
54+
raise ValueError("Cannot find date formatted YYYY-MM-DD")
55+
56+
# find the time
57+
try:
58+
time = re.search("[0-9]{2}:[0-9]{2}:[0-9]{2}", last_entry).group()
59+
except AttributeError:
60+
raise ValueError("Cannot find time formatted HH:MM:SS")
61+
62+
if 'ERROR' in last_entry:
63+
return (date, time, "ERROR")
64+
else:
65+
return (date, time, "SUCCESS")
66+
2067

2168
def main():
22-
class Accel():
23-
def __init__(self, filename, status, path, date, time):
24-
self.filename = filename
25-
self.status = status
26-
if status == 'ERROR':
27-
self.css = 'redtext'
28-
else:
29-
self.css = 'greentext'
30-
self.path = path
31-
self.date = date
32-
self.time = time
3369

3470
opts = build_parser().parse_args()
3571
# converting relative paths to absolute
@@ -45,15 +81,11 @@ def __init__(self, filename, status, path, date, time):
4581
accel_files = []
4682

4783
for fil in filenames:
48-
contents = open(fil, 'r').read()
49-
date = contents[0:10]
50-
time = contents[11:19]
51-
if 'ERROR' in contents:
52-
accel_files.append(Accel(os.path.basename(fil), 'ERROR', 'file:' +
53-
fil, date, time))
54-
else:
55-
accel_files.append(Accel(os.path.basename(fil), 'SUCCESS', 'file:' +
56-
fil, date, time))
84+
date, time, status = parse_log_file(fil)
85+
86+
accel_files.append(Accel(os.path.basename(fil), status, 'file:' +
87+
fil, date, time))
88+
5789

5890
env = jinja2.Environment(
5991
loader=jinja2.FileSystemLoader(searchpath=os.path.dirname(template_file))
File renamed without changes.

log-html/tests/__init__.py

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
###############################
3+
4+
2020-05-14 07:31:12,110 - accel_code.run - ERROR - could not get subject id from redcap for 827 (2018-10-02 00:00:00)
5+
Traceback (most recent call last):
6+
File "/opt/miniconda-latest/envs/accel/lib/python3.7/site-packages/redcap/project.py", line 57, in configure
7+
self.metadata = self.__md()
8+
File "/opt/miniconda-latest/envs/accel/lib/python3.7/site-packages/redcap/project.py", line 93, in __md
9+
return self._call_api(p_l, 'metadata')[0]
10+
File "/opt/miniconda-latest/envs/accel/lib/python3.7/site-packages/redcap/project.py", line 155, in _call_api
11+
return rcr.execute(**request_kwargs)
12+
File "/opt/miniconda-latest/envs/accel/lib/python3.7/site-packages/redcap/request.py", line 125, in execute
13+
self.raise_for_status(r)
14+
File "/opt/miniconda-latest/envs/accel/lib/python3.7/site-packages/redcap/request.py", line 166, in raise_for_status
15+
r.raise_for_status()
16+
File "/opt/miniconda-latest/envs/accel/lib/python3.7/site-packages/requests/models.py", line 941, in raise_for_status
17+
raise HTTPError(http_error_msg, response=self)
18+
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://redcap.icts.uiowa.edu/redcap/api/
19+
20+
During handling of the above exception, another exception occurred:
21+
22+
Traceback (most recent call last):
23+
File "/home/coder/projects/accel_code/run.py", line 81, in main
24+
sub_id = redcap_query.redcap_query(lab_id, project, opts.api_key)
25+
File "/home/coder/projects/accel_code/redcap_query.py", line 14, in redcap_query
26+
project = redcap.Project(api_url, api_key)
27+
File "/opt/miniconda-latest/envs/accel/lib/python3.7/site-packages/redcap/project.py", line 53, in __init__
28+
self.configure()
29+
File "/opt/miniconda-latest/envs/accel/lib/python3.7/site-packages/redcap/project.py", line 59, in configure
30+
raise RedcapError("Exporting metadata failed. Check your URL and token.")
31+
requests.exceptions.RequestException: Exporting metadata failed. Check your URL and token.
32+
33+
###############################
34+
35+
2020-05-14 12:35:52,510 - accel_code.run - INFO - /mnt/nfs/lss/vosslabhpc/Projects/Accelerometer/3-Data/827/827 (2018-10-02)RAW.csv -> /mnt/nfs/lss/vosslabhpc/Projects/BikeExtend/3-Experiment/2-Data/BIDS/sub-2103/ses-accel1/beh/sub-2103_ses-accel1_accel.csv

0 commit comments

Comments
 (0)