Skip to content

Commit

Permalink
prepare for locally hosted manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Apr 5, 2023
1 parent c94acba commit 5dfb10f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
40 changes: 29 additions & 11 deletions repogen/apidata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# -*- coding: utf-8 -*-
import json
import math
import urllib.parse
from os import makedirs
from os.path import exists, join
from pathlib import Path
from typing import List

import more_itertools

from markdown import Markdown

from repogen import pkg_info
Expand All @@ -16,6 +17,21 @@
MANIFEST_KEYS = ('id', 'title', 'iconUri', 'manifestUrl', 'manifest', 'manifestUrlBeta', 'manifestBeta', 'pool')


def fix_manifest_url(item: PackageInfo, app_dir: str):
manifest_url = urllib.parse.urlparse(item['manifestUrl'])
if manifest_url.scheme != 'file':
return

manifests_dir = Path(app_dir).joinpath('manifests')
if not manifests_dir.exists():
manifests_dir.mkdir()
manifest = item["manifest"]
manifest_path = manifests_dir.joinpath(f'{manifest["version"]}.json')
with manifest_path.open(mode='w') as mf:
json.dump(manifest, mf)
item['manifestUrl'] = manifest_path.as_posix().removeprefix('content')


def generate(packages: List[PackageInfo], outdir: str):
markdown = Markdown()

Expand All @@ -37,24 +53,25 @@ def package_item(p_info: PackageInfo, in_apps_dir: bool):

packages_length = len(packages)
max_page = math.ceil(packages_length / ITEMS_PER_PAGE)
for index, chunk in enumerate(
more_itertools.chunked(packages, ITEMS_PER_PAGE) if packages else [[]]
):
page = index + 1
json_file = join(appsdir, '%d.json' %
page) if page > 1 else join(outdir, 'apps.json')
with open(json_file, 'w', encoding='utf-8') as f:

def save_page(page: int, items: [PackageInfo]):
json_file = join(appsdir, '%d.json' % page) if page > 1 else join(outdir, 'apps.json')
with open(json_file, 'w', encoding='utf-8') as pf:
json.dump({
'paging': {
'page': page,
'count': len(chunk),
'count': len(items),
'maxPage': max_page,
'itemsTotal': packages_length,
},
'packages': list(map(lambda x: package_item(x, page > 1), chunk))
}, f, indent=2)
'packages': list(map(lambda x: package_item(x, page > 1), items))
}, pf, indent=2)

chunks = more_itertools.chunked(packages, ITEMS_PER_PAGE) if packages else [[]]
for index, chunk in enumerate(chunks):
for item in chunk:
app_dir = join(appsdir, item['id'])
fix_manifest_url(item, app_dir)
releases_dir = join(app_dir, 'releases')
if not exists(releases_dir):
makedirs(releases_dir)
Expand All @@ -64,6 +81,7 @@ def package_item(p_info: PackageInfo, in_apps_dir: bool):
desc_html = join(app_dir, 'full_description.html')
with open(desc_html, 'w', encoding='utf-8') as f:
f.write(markdown.convert(item['description']))
save_page(index + 1, chunk)
print('Generated json data for %d packages.' % len(packages))


Expand Down
4 changes: 3 additions & 1 deletion repogen/pkg_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import locale
import os
from datetime import datetime
from os import path
from os.path import isfile, join
from typing import TypedDict, Optional, List, NotRequired

Expand Down Expand Up @@ -32,8 +33,9 @@ class PackageInfo(TypedDict):


def from_package_info_file(info_path: str, offline=False) -> Optional[PackageInfo]:
extension = os.path.splitext(info_path)[1]
extension = path.splitext(info_path)[1]
content: PackageRegistry
print(f'Parsing package info file {path.basename(info_path)}')
if extension == '.yml':
pkgid, content = parse_yml_package(info_path)
elif extension == '.py':
Expand Down
1 change: 0 additions & 1 deletion repogen/pkg_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from datetime import datetime
from email.utils import parsedate_to_datetime
from json import JSONDecodeError
from os import path
from typing import Tuple, TypedDict, Optional, NotRequired, Literal
from urllib.parse import urljoin
from urllib.request import url2pathname
Expand Down

0 comments on commit 5dfb10f

Please sign in to comment.