From 97160f6f7f727c6dc4133225e1a6c7ad153c4150 Mon Sep 17 00:00:00 2001 From: Bob Borges Date: Thu, 19 Sep 2024 15:05:59 +0200 Subject: [PATCH 1/2] feat: add gh link fn --- pyriksdagen/utils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pyriksdagen/utils.py b/pyriksdagen/utils.py index a876434..c256e8b 100644 --- a/pyriksdagen/utils.py +++ b/pyriksdagen/utils.py @@ -334,3 +334,21 @@ def get_data_location(partition): d["metadata"] = os.environ.get("METADATA_PATH", "data") return d[partition] + +def get_gh_link(_file, elem, + username='swerik-project', + repo='riksdaen-records', + branch='dev') + """ + return a formatted github link to an lxml element + + Args: + - _file: the path (from root) of the file in question + - elem: the element + - username: the username of the repo owner + - reponame: the repository containing the _file + - branch: the branch you want to link to + """ + line_number = elem.sourceline + gh = f"https://github.com/{username}/{repo}/blob/{branch}/{file}/#L{line_number}" + return gh From 45f6f3fd1a2c67414939b5ad183a69046b9694f0 Mon Sep 17 00:00:00 2001 From: Bob Borges Date: Thu, 19 Sep 2024 15:35:48 +0200 Subject: [PATCH 2/2] fix: make link fn actually fn --- pyriksdagen/utils.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/pyriksdagen/utils.py b/pyriksdagen/utils.py index c256e8b..4402f4c 100644 --- a/pyriksdagen/utils.py +++ b/pyriksdagen/utils.py @@ -13,7 +13,7 @@ from datetime import datetime import hashlib, uuid, base58, requests, tqdm import zipfile -import os +import os, sys from trainerlog import get_logger LOGGER = get_logger("pyriksdagen") @@ -335,12 +335,14 @@ def get_data_location(partition): return d[partition] -def get_gh_link(_file, elem, +def get_gh_link(_file, + elem=None, + line_number=None, username='swerik-project', - repo='riksdaen-records', - branch='dev') + repo='riksdagen-records', + branch='dev'): """ - return a formatted github link to an lxml element + return a formatted github link to an lxml element or specified line in _file Args: - _file: the path (from root) of the file in question @@ -349,6 +351,14 @@ def get_gh_link(_file, elem, - reponame: the repository containing the _file - branch: the branch you want to link to """ - line_number = elem.sourceline - gh = f"https://github.com/{username}/{repo}/blob/{branch}/{file}/#L{line_number}" + try: + assert (elem is not None or line_number is not None) and elem != line_number + except: + print("You have to pass an elem or a line number") + sys.exit() + if _file.startswith(repo): + _file = _file.replace(f"{repo}/", "") + if elem is not None: + line_number = elem.sourceline + gh = f"https://github.com/{username}/{repo}/blob/{branch}/{_file}/#L{line_number}" return gh