Skip to content

Commit

Permalink
implemented #4
Browse files Browse the repository at this point in the history
  • Loading branch information
au5ton committed Jul 6, 2020
1 parent 345684b commit c39c3bb
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 52 deletions.
17 changes: 12 additions & 5 deletions bundler/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import tarfile
import argparse
from time import time
from shutil import rmtree
from shutil import rmtree, copyfile
from pathlib import Path
from bundle.patch import subjects, publications_courses
from bundle.patch import subjects, publications_courses, groups
from bundle import patch
from bundle import grade_distribution, subjects, publications_courses
from colorama import init
Expand All @@ -18,7 +18,7 @@
args = parser.parse_args()

# total tasks
N = 6
N = 8
M = 1
documents_path = Path(__file__).parent / '..' / 'documents'
exports_path = Path(__file__).parent / '..' / 'exports'
Expand All @@ -36,26 +36,33 @@
# process the raw data, generate intermediary format
for fmt in documents_path.iterdir():
# print thing
if(fmt.name in ['com.collegescheduler.uh.subjects', 'edu.uh.publications.courses']):
if(fmt.name in ['com.collegescheduler.uh.subjects', 'edu.uh.publications.courses', 'io.cougargrades.groups']):
print(f'{Fore.CYAN}[{M} / {N}] Bundling {fmt.name}{Style.RESET_ALL}')
M += 1
# actually do
if(fmt.name == 'com.collegescheduler.uh.subjects'):
subjects.process(fmt.resolve(), export_name / fmt.name)
if(fmt.name == 'edu.uh.publications.courses'):
publications_courses.process(fmt.resolve(), export_name / fmt.name)
if(fmt.name == 'io.cougargrades.groups'):
(export_name / fmt.name).mkdir(exist_ok=True)
copyfile(fmt / 'defaults.json', export_name / fmt.name / 'defaults.json')
print('\t✔')


# generate patch files
for fmt in documents_path.iterdir():
# print thing
if(fmt.name in ['com.collegescheduler.uh.subjects', 'edu.uh.publications.courses']):
if(fmt.name in ['com.collegescheduler.uh.subjects', 'edu.uh.publications.courses', 'io.cougargrades.groups']):
print(f'{Fore.CYAN}[{M} / {N}] Patching {fmt.name}{Style.RESET_ALL}')
M += 1
# actually do
if(fmt.name == 'com.collegescheduler.uh.subjects'):
patch.subjects.generate(export_name / fmt.name, export_name / 'io.cougargrades.publicdata.patch')
if(fmt.name == 'edu.uh.publications.courses'):
patch.publications_courses.generate(export_name / fmt.name, export_name / 'io.cougargrades.publicdata.patch')
if(fmt.name == 'io.cougargrades.groups'):
patch.groups.generate(export_name / fmt.name, export_name / 'io.cougargrades.publicdata.patch')

# generate the export file
print(f'{Fore.CYAN}[{M} / {N}] Compressing tarfile: {export_name}{Style.RESET_ALL}')
Expand Down
20 changes: 20 additions & 0 deletions bundler/bundle/patch/groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import json
from pathlib import Path
from .patchfile import Patchfile
from time import time_ns
from alive_progress import alive_bar

'''
Generates Patchfiles for the Core Curriculum
'''
def generate(source: Path, destination: Path):
destination.mkdir(exist_ok=True)
with open(source / 'defaults.json', 'r') as f:
data = json.loads(f.read())
with alive_bar(len(data)) as bar:
for item in data:
with open(destination / f'patch-0-{time_ns()}.json', 'w') as out:
out.write(str(
Patchfile(f'/groups/{item["identifier"]}').write(item)
))
bar()
38 changes: 26 additions & 12 deletions bundler/bundle/patch/publications_courses.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import csv
from pathlib import Path
from . import util
from .patchfile import Patchfile
from time import time_ns
from alive_progress import alive_bar

'''
Generates Patchfiles for connections to the UH Publications official course catalog
'''
def generate(source: Path, destination: Path):
print(source.name)
# destination.mkdir(exist_ok=True)
# with open(source / 'subjects.json', 'r') as f:
# data = json.loads(f.read())
# entries = [unwrap(s) for s in data]
# results = dict()
# for e in entries:
# results[e['abbreviation']] = e['description']
# with open(destination / 'entries.json', 'w') as ex:
# ex.write(json.dumps(entries))
# with open(destination / 'dictionary.json', 'w') as ex:
# ex.write(json.dumps(results))
destination.mkdir(exist_ok=True)
with open(source / 'pairs.csv', 'r') as infile:
with alive_bar(util.file_len((source / 'pairs.csv').resolve())-1) as bar:
reader = csv.DictReader(infile)
for row in reader:
with open(destination / f'patch-2-{time_ns()}.json', 'w') as out:
out.write(str(
Patchfile(f'/catalog/{row["department"]} {row["catalogNumber"]}').merge({
"publication": {
"title": row["title"],
"catoid": row["catoid"],
"coid": row["coid"],
"classification": row["classification"],
"url": f'http://publications.uh.edu/preview_course_nopop.php?catoid={row["catoid"]}&coid={row["coid"]}' if row["catoid"] != None and row["coid"] != None else ""
}
})
))
bar()
36 changes: 24 additions & 12 deletions bundler/bundle/patch/subjects.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import json
from pathlib import Path
from .patchfile import Patchfile
from time import time_ns
from alive_progress import alive_bar

'''
Generates patchfiles for individual subject groups
'''
def generate(source: Path, destination: Path):
print(source.name)
# destination.mkdir(exist_ok=True)
# with open(source / 'subjects.json', 'r') as f:
# data = json.loads(f.read())
# entries = [unwrap(s) for s in data]
# results = dict()
# for e in entries:
# results[e['abbreviation']] = e['description']
# with open(destination / 'entries.json', 'w') as ex:
# ex.write(json.dumps(entries))
# with open(destination / 'dictionary.json', 'w') as ex:
# ex.write(json.dumps(results))
destination.mkdir(exist_ok=True)
with open(source / 'entries.json', 'r') as f:
entries = json.loads(f.read())
with alive_bar(len(entries)) as bar:
for item in entries:
with open(destination / f'patch-1-{time_ns()}.json', 'w') as out:
out.write(str(
Patchfile(f'/groups/{item["abbreviation"]}').write({
"name": item["description"],
"identifier": item["abbreviation"],
"courses": [],
"keywords": [],
"description": f'Courses from the \"{item["abbreviation"]}\" subject.',
"courses_count": 0
})
))
bar()
7 changes: 7 additions & 0 deletions bundler/bundle/patch/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# see: https://stackoverflow.com/q/845058
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
7 changes: 4 additions & 3 deletions bundler/bundle/publications_courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def process(source: Path, destination: Path):

# create the output file
with open(destination / 'pairs.csv', 'w') as outfile:
writer = csv.DictWriter(outfile, ['catoid', 'coid', 'classification', 'department', 'catalogNumber'])
writer = csv.DictWriter(outfile, ['catoid', 'coid', 'classification', 'department', 'catalogNumber', 'title'])
writer.writeheader()
for p in source.glob('*.csv'):
print(f'\t{Style.DIM}{Path(p).name}{Style.RESET_ALL}')
with alive_bar(util.file_len(p)) as bar:
with alive_bar(util.file_len(p)-1) as bar:
with open(p, 'r') as infile:
reader = csv.DictReader(infile)
# for every row in this index.csv file
Expand All @@ -44,6 +44,7 @@ def process(source: Path, destination: Path):
"coid": row["coid"],
"classification": row["classification"],
"department": course.split(' ')[0],
"catalogNumber": course.split(' ')[1]
"catalogNumber": course.split(' ')[1],
"title": row["catalog_title"]
})
bar()
41 changes: 21 additions & 20 deletions bundler/bundle/subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@


def unwrap(s):
# see: https://stackoverflow.com/a/6208415
# paren[0] => '(Asian American Studies)'
parenthesis = re.search('\(([^\)]+)\)', s['title'])
d = dict()
d['abbreviation'] = s['id']
# exclude first and last characters
d['description'] = parenthesis[0][1:-1]
return d
# see: https://stackoverflow.com/a/6208415
# paren[0] => '(Asian American Studies)'
parenthesis = re.search('\(([^\)]+)\)', s['title'])
d = dict()
d['abbreviation'] = s['id']
# exclude first and last characters
d['description'] = parenthesis[0][1:-1]
return d

'''
Processes subject.json to be more accessible
Expand All @@ -29,15 +29,16 @@ def unwrap(s):
'''
def process(source: Path, destination: Path):
# print(source.name)
destination.mkdir(exist_ok=True)
with open(source / 'subjects.json', 'r') as f:
data = json.loads(f.read())
entries = [unwrap(s) for s in data]
results = dict()
for e in entries:
results[e['abbreviation']] = e['description']
with open(destination / 'entries.json', 'w') as ex:
ex.write(json.dumps(entries))
with open(destination / 'dictionary.json', 'w') as ex:
ex.write(json.dumps(results))
# print(source.name)
destination.mkdir(exist_ok=True)
with open(source / 'subjects.json', 'r') as f:
data = json.loads(f.read())
entries = [unwrap(s) for s in data]
results = dict()
for e in entries:
results[e['abbreviation']] = e['description']
with open(destination / 'entries.json', 'w') as ex:
ex.write(json.dumps(entries))
with open(destination / 'dictionary.json', 'w') as ex:
ex.write(json.dumps(results))
print('\t✔')
9 changes: 9 additions & 0 deletions documents/io.cougargrades.groups/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# io.cougargrades.groups

## Format

See: [@cougargrades/types](https://github.com/cougargrades/types/issues/5)

## Data Aquisition

Manually entered.
82 changes: 82 additions & 0 deletions documents/io.cougargrades.groups/defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
[
{
"name": "Communication",
"identifier": "10",
"description": "Courses which satisfy the \"Communication\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "Mathematics",
"identifier": "20",
"description": "Courses which satisfy the \"Mathematics\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "Life & Physical Sciences",
"identifier": "30",
"description": "Courses which satisfy the \"Life & Physical Sciences\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "Language, Philosophy, & Culture",
"identifier": "40",
"description": "Courses which satisfy the \"Language, Philosophy, & Culture\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "Creative Arts",
"identifier": "50",
"description": "Courses which satisfy the \"Creative Arts\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "American History",
"identifier": "60",
"description": "Courses which satisfy the \"American History\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "Government/Political Science",
"identifier": "70",
"description": "Courses which satisfy the \"Government/Political Science\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "Social & Behavioral Sciences",
"identifier": "80",
"description": "Courses which satisfy the \"Social & Behavioral Sciences\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "Writing in the Disciplines",
"identifier": "81",
"description": "Courses which satisfy the \"Writing in the Disciplines\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
},
{
"name": "Math/Reasoning",
"identifier": "91",
"description": "Courses which satisfy the \"Math/Reasoning\" component in the UH Core Curriculum.",
"keywords": [],
"courses": [],
"courses_count": 0
}
]

0 comments on commit c39c3bb

Please sign in to comment.