Skip to content

Commit

Permalink
Fix #64, also fix types and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nonprofittechy committed May 17, 2024
1 parent 0761ec4 commit 2246e74
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 33 deletions.
44 changes: 31 additions & 13 deletions docassemble/ALDashboard/aldashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@
]


def install_from_github_url(url: str, branch: str = ""):
def install_from_github_url(url: str, branch: str = "", pat: Optional[str] = None):
giturl = url.strip().rstrip("/")
if pat:
# modify so it looks like https://e8cc02bec7061de98ba4851263638d7483f63d41:x-oauth-basic@github.com/johnsmith/docassemble-missouri-familylaw
giturl = re.sub(r"^https://", f"https://{pat}:x-oauth-basic@", giturl)
if isinstance(branch, str):
branch = branch.strip()
if not branch:
Expand Down Expand Up @@ -221,8 +224,10 @@ def speedy_get_sessions(
"""
)
if not filename:
if not user_has_privilege(['admin', 'developer']):
raise Exception("You must provide a filename to filter sessions unless you are a developer or administrator.")
if not user_has_privilege(["admin", "developer"]):
raise Exception(
"You must provide a filename to filter sessions unless you are a developer or administrator."
)
filename = None # Explicitly treat empty string as equivalent to None
if not user_id:
user_id = None
Expand All @@ -233,7 +238,12 @@ def speedy_get_sessions(
with db.connect() as con:
rs = con.execute(
get_sessions_query,
{"user_id": user_id, "filename": filename, "filter_step1": filter_step1, "metadata": metadata_key_name},
{
"user_id": user_id,
"filename": filename,
"filter_step1": filter_step1,
"metadata": metadata_key_name,
},
)
sessions = [session for session in rs]

Expand Down Expand Up @@ -374,7 +384,8 @@ def nicer_interview_filename(filename: str) -> str:

return filename_parts[0]

def list_question_files_in_package(package_name:str) -> List[str]:

def list_question_files_in_package(package_name: str) -> Optional[List[str]]:
"""
List all the files in the 'data/questions' directory of a package.
Expand All @@ -386,19 +397,22 @@ def list_question_files_in_package(package_name:str) -> List[str]:
"""
try:
# Locate the directory within the package
directory_path = pkg_resources.resource_filename(package_name, 'data/questions')
directory_path = pkg_resources.resource_filename(package_name, "data/questions")

# List all files in the directory
if os.path.isdir(directory_path):
files = os.listdir(directory_path)
# Filter out directories, only keep files
files = [f for f in files if os.path.isfile(os.path.join(directory_path, f))]
files = [
f for f in files if os.path.isfile(os.path.join(directory_path, f))
]
return files
else:
return []
except Exception as e:
log(f"An error occurred with package '{package_name}': {e}")
return None
return []


def list_question_files_in_docassemble_packages():
"""
Expand All @@ -407,18 +421,22 @@ def list_question_files_in_docassemble_packages():
Returns:
Dict[str, List[str]]: A dictionary where the keys are package names and the values are lists of filenames in the 'data/questions' directory of the package.
"""
packages = get_package_info()[0] # get_package_info returns a tuple, the packages are in index 0
packages = get_package_info()[
0
] # get_package_info returns a tuple, the packages are in index 0

filtered_packages = [pkg for pkg in packages if pkg.package.name.startswith('docassemble.')]
filtered_packages = [
pkg for pkg in packages if pkg.package.name.startswith("docassemble.")
]

result = {}

# Iterate over each filtered package and list files in 'data/questions'
for package in filtered_packages:
package_name = package.package.name

files = list_question_files_in_package(package_name)
if files:
result[package_name] = files
result[package_name] = files

return result
return result
19 changes: 17 additions & 2 deletions docassemble/ALDashboard/data/questions/install_assembly_line.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fields:
- Server short title: the_config['default short title']
default: ${ the_config.get('default short title') }
- App name: the_config['appname']
default: ${ the_config.get('appname', '') }
default: ${ the_config.get('appname', the_config.get('default short title')) }
- note: |
---
Some features require background processing, which means you need
Expand Down Expand Up @@ -371,6 +371,9 @@ subquestion: |
1. Let users give feedback about an interview.
2. Display a "last updated" date on the "About" page of an interview.
1. Install and update packages from GitHub.
We recommend using **2 separate GitHub accounts** for these tasks.
First, in order to use GitHub you should create a dedicated GitHub account. You can use an account that already exists, but we recommend having an account that only handles Assembly Line functionality.
Expand All @@ -383,9 +386,21 @@ subquestion: |
1. Tap the checkbox for "repo" permissions.
1. Finish creating the personal access token.
1. Copy the token and paste it below
If you have a private repository, you will need a second token with "repo" permissions.
This token is only used to install and update packages, and should not be the same
as the account used for feedback. It is OK for this token to be associated with your
personal GitHub account.
fields:
- Personal access token: the_config['github issues']['token']
- Personal access token for authoring issues: the_config['github issues']['token']
default: ${ the_config['github issues'].get('token', '') }
- Personal access token for installing private repositories: the_config['assembly line']['github install token']
default: ${ the_config['assembly line'].get('github install token', '') }
required: False
help: |
If you have private repositories, you will need a second token with "repo" permissions.
This token is only used to install and update packages.
---
id: github owners list
continue button field: github_owners_list
Expand Down
39 changes: 28 additions & 11 deletions docassemble/ALDashboard/data/questions/install_packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ question: |
list collect: True
fields:
- Github URL: packages[i].github_url
- This is a private GitHub repo: packages[i].is_private
datatype: yesno
- Github [Personal Access Token](https://docassemble.org/docs/packages.html#github_install): packages[i].github_token
show if: packages[i].is_private
default: |
${ get_config('assembly line',{}).get("github install token", "") }
- Also add an alias: packages[i].add_alias
datatype: yesno
- YAML filename: packages[i].yaml_name
show if: packages[i].add_alias
- Short name or alias (no spaces): packages[i].alias
validate: |
lambda y: y.isidentifier()
show if: packages[i].add_alias
---
code: |
install_packages_task = background_action('install_packages_event')
Expand All @@ -38,19 +48,21 @@ event: install_packages_event
code: |
background_error_action("bg_failure")
for package in packages:
pkgname = install_from_github_url(package.github_url)
pkgname = install_from_github_url(package.github_url, pat=package.github_token if package.is_private else None)
reset(pkgname)
the_config = da_get_config()
if not the_config.get("dispatch"):
the_config["dispatch"] = {}
for package in packages:
package_regex = r"https:\/\/github\.com\/.*\/docassemble-([\w]*)"
match = re.search(package_regex, package.github_url)
if match:
package_path = f"docassemble.{ match.groups()[0] }:data/questions/{ package.yaml_name }"
the_config["dispatch"][package.alias] = package_path
results = da_write_config(the_config)
if any((package.add_alias for package in packages)):
the_config = da_get_config()
if not the_config.get("dispatch"):
the_config["dispatch"] = {}
for package in packages:
if package.add_alias:
package_regex = r"https:\/\/github\.com\/.*\/docassemble-([\w]*)"
match = re.search(package_regex, package.github_url)
if match:
package_path = f"docassemble.{ match.groups()[0] }:data/questions/{ package.yaml_name }"
the_config["dispatch"][package.alias] = package_path
results = da_write_config(the_config)
background_response_action("bg_success")
---
Expand All @@ -76,13 +88,18 @@ subquestion: |
% if it_worked:
It may take a few minutes for the installation process to complete.
% if any((package.add_alias for package in packages)):
You can now use these links to reach your interviews:
% for package in packages:
% if package.add_alias:
* [${package.alias}](/start/${package.alias})
% endif
% endfor
% endif
% else:
Something went wrong. Check the [worker.log](/logs?file=worker.log) to learn what.
% endif
buttons:
- Restart: restart
6 changes: 3 additions & 3 deletions docassemble/ALDashboard/package_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ def fetch_github_repo_version(repo_list, key_pkgs, github_user) -> dict:
version_num = decoded_line[str_start:str_end][9:].replace(
"',\n", ""
)
v[
"version"
] = version_num # Add version number to the original repo_list.
v["version"] = (
version_num # Add version number to the original repo_list.
)
has_version_num = True
break

Expand Down
3 changes: 2 additions & 1 deletion docassemble/ALDashboard/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ docassemble.webapp
types-setuptools
types-pycurl
types-requests
types-setuptools
mypy
openai
tiktoken
tiktoken
8 changes: 6 additions & 2 deletions docassemble/ALDashboard/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@

class Translation(NamedTuple):
file: DAFile # an XLSX or XLIFF file
untranslated_words: int # Word count for all untranslated segments that are not Mako or HTML
untranslated_words: (
int # Word count for all untranslated segments that are not Mako or HTML
)
untranslated_segments: int # Number of rows in the output that have untranslated text - one for each question, subquestion, field, etc.
total_rows: int

Expand All @@ -69,7 +71,9 @@ def translation_file(yaml_filename: str, tr_lang: str) -> Translation:
This code was adjusted from the Flask endpoint-only version in server.py. XLIFF support was removed
for now but can be added later.
"""
filetype: str = "XLSX" # Look in server.py for support of XLIFF format, but we won't implement it here
filetype: str = (
"XLSX" # Look in server.py for support of XLIFF format, but we won't implement it here
)
output_file = DAFile()
setup_translation()
if yaml_filename is None or not re.search(r"\S", yaml_filename):
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,3 @@ ignore_missing_imports = true
[[tool.mypy.overrides]]
module="mako.*"
ignore_missing_imports = true

0 comments on commit 2246e74

Please sign in to comment.