|
1 | | -from resume import NLJ, shields_badges |
| 1 | +from collections import namedtuple |
| 2 | +from datetime import timedelta |
| 3 | +from functools import partial |
| 4 | +import json |
| 5 | +import yaml |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from requests_cache import CachedSession |
| 9 | + |
2 | 10 | from resume.badges import devicons_badges |
3 | 11 | from resume.salutations import typed_salutations |
4 | 12 |
|
| 13 | +class HttpClient(CachedSession): |
| 14 | + def __init__(self, expire_after=None): |
| 15 | + CachedSession.__init__(self, |
| 16 | + cache_name='mycull.dev', |
| 17 | + use_temp=True, |
| 18 | + expire_after=timedelta(minutes=30) if expire_after is None else expire_after, |
| 19 | + ) |
| 20 | + |
| 21 | +NLJ = partial('\n'.join) |
| 22 | +JSDELIVR_DEVICONS = "https://cdn.jsdelivr.net/gh/devicons/devicon@master/icons" |
| 23 | +DEVICONS_GITHUB = "https://github.com/devicons/devicon/raw/master/devicon.json" |
| 24 | +IMG_ALT = '{{ height={i.height} width={i.width} }}' |
| 25 | +Image = namedtuple('Image', 'text image_url height width') |
| 26 | +DevIcon = namedtuple('DevIcon', 'name tags svg_url color') |
| 27 | + |
| 28 | +DEVICONS_JSON = { |
| 29 | + icon['name']: DevIcon( |
| 30 | + icon['name'], |
| 31 | + icon['tags'], |
| 32 | + f"{JSDELIVR_DEVICONS}/{icon['name']}/{icon['name']}-{icon['versions']['svg'][0]}.svg", |
| 33 | + icon['color'], |
| 34 | + ) |
| 35 | + for icon in HttpClient().get(DEVICONS_GITHUB).json() |
| 36 | +} |
| 37 | + |
| 38 | +def load_resume(format='yaml', name='resume'): |
| 39 | + return { |
| 40 | + 'yaml': yaml.safe_load, |
| 41 | + 'json': json.load, |
| 42 | + }[format]((Path.cwd() / f'{name}.{format}').read_text()) |
| 43 | + |
| 44 | +def devicons_badges(company=None): |
| 45 | + skills_by_company = { |
| 46 | + job['id']: job['skills'].split() |
| 47 | + for job in load_resume()['work'] |
| 48 | + } |
| 49 | + |
| 50 | + if company is None or company not in skills_by_company: |
| 51 | + unique_skills = set() |
| 52 | + for all_skills in skills_by_company.values(): |
| 53 | + unique_skills |= set(all_skills) |
| 54 | + skills = list(unique_skills) |
| 55 | + else: |
| 56 | + skills = skills_by_company[company] |
| 57 | + |
| 58 | + return [ |
| 59 | + IMG_ALT.format(i=Image( |
| 60 | + text=skill, |
| 61 | + image_url=DEVICONS_JSON[skill].svg_url, |
| 62 | + height='30px', |
| 63 | + width='80px', |
| 64 | + )) |
| 65 | + for skill in skills |
| 66 | + if skill in DEVICONS_JSON |
| 67 | + ] |
| 68 | + |
| 69 | + badges = [] |
| 70 | + for skill in skills: |
| 71 | + options = dict( |
| 72 | + left_text=skill, |
| 73 | + left_title=skill, |
| 74 | + right_text='TODO: calc XP', |
| 75 | + right_color='white', |
| 76 | + ) |
| 77 | + |
| 78 | + icon = icons.get(skill) |
| 79 | + if icon: |
| 80 | + options['logo'] = icon.svg |
| 81 | + options['left_color'] = icon.color |
| 82 | + |
| 83 | + badges.append(badge(**options)) |
| 84 | + |
| 85 | + return badges |
| 86 | + |
5 | 87 | def skills_badges(company=None): |
6 | 88 | return NLJ(devicons_badges(company)) |
| 89 | + |
7 | 90 | def define_env(env): |
8 | 91 | env.variables['skills_badges'] = skills_badges |
9 | 92 | env.variables['typed_salutations'] = typed_salutations |
0 commit comments