Skip to content

Commit

Permalink
migrated to pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Apr 5, 2023
1 parent 39c5b2f commit be50c94
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 23 deletions.
3 changes: 1 addition & 2 deletions repogen/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
from os import path
from pathlib import Path

from repogen import apppage, apidata, pkg_info
Expand All @@ -18,7 +17,7 @@

args = parser.parse_args()

packages = pkg_info.list_packages(args.input_dir)
packages = pkg_info.list_packages(Path(args.input_dir))

if args.gen_api:
apidata.generate(packages, Path(args.output_dir, 'api'))
Expand Down
2 changes: 1 addition & 1 deletion repogen/apidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ def save_page(page: int, items: [PackageInfo]):
parser.add_argument('-o', '--output-dir', required=True)
args = parser.parse_args()

generate(pkg_info.list_packages(args.input_dir), args.output_dir)
generate(pkg_info.list_packages(Path(args.input_dir)), Path(args.output_dir))
22 changes: 9 additions & 13 deletions repogen/apppage.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
# -*- coding: utf-8 -*-
import math
from os.path import dirname, join
from pathlib import Path
from typing import List

import more_itertools
import pystache

from repogen import pkg_info
from repogen.common import ITEMS_PER_PAGE
from repogen.common import ITEMS_PER_PAGE, ensure_open
from repogen.pkg_info import PackageInfo


class AppListingGenerator:

def __init__(self, packages: List[PackageInfo]):
with open(join(dirname(__file__), 'templates', 'apps', 'detail.md'), encoding='utf-8') as f:
with Path(__file__).parent.joinpath('templates', 'apps', 'detail.md').open(encoding='utf-8') as f:
self.details_template = f.read()
with open(join(dirname(__file__), 'templates', 'apps', 'list.html'), encoding='utf-8') as f:
with Path(__file__).parent.joinpath('templates', 'apps', 'list.html').open(encoding='utf-8') as f:
self.index_template = f.read()

self.packages = packages

def gen_details(self, outdir):
def gen_details(self, outdir: Path):
for pkg in self.packages:
with open(join(outdir, '%s.md' % pkg['id']), 'w', encoding='utf-8') as f:
with ensure_open(outdir.joinpath('%s.md' % pkg['id']), mode='w', encoding='utf-8') as f:
f.write(pystache.render(self.details_template, pkg))

def _gen_page(self, outdir, items, pagination):
def _gen_page(self, outdir: Path, items, pagination):
page = pagination['page']
maxp = pagination['max']
prevp = pagination['prev']
Expand Down Expand Up @@ -62,7 +61,7 @@ def _nav_item(p: int):
def _page_path(p: int):
return 'apps/index.html' if p == 1 else 'apps/page/%d.html' % p

with open(join(outdir, ('apps-page-%d.html' % page)), 'w', encoding='utf-8') as f:
with ensure_open(outdir.joinpath('apps-page-%d.html' % page), mode='w', encoding='utf-8') as f:
f.write(pystache.render(self.index_template, {
'packages': items, 'pagePath': _page_path(page),
'firstPage': page == 1,
Expand All @@ -72,7 +71,7 @@ def _page_path(p: int):
}
}))

def gen_list(self, outdir):
def gen_list(self, outdir: Path):
pkgs = self.packages
packages_length = len(pkgs)
max_page = math.ceil(packages_length / ITEMS_PER_PAGE)
Expand All @@ -89,9 +88,6 @@ def gen_list(self, outdir):
def generate(packages: List[PackageInfo], outdir: Path, gen_details=True, gen_list=True):
generator = AppListingGenerator(packages)

if not outdir.exists():
outdir.mkdir(parents=True)

if gen_details:
generator.gen_details(outdir)
if gen_list:
Expand All @@ -109,4 +105,4 @@ def generate(packages: List[PackageInfo], outdir: Path, gen_details=True, gen_li
parser.add_argument('-o', '--output-dir', required=True)
args = parser.parse_args()

generate(pkg_info.list_packages(args.input_dir), Path(args.output_dir))
generate(pkg_info.list_packages(Path(args.input_dir)), Path(args.output_dir))
1 change: 0 additions & 1 deletion repogen/check_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import subprocess
from pathlib import Path

Expand Down
2 changes: 1 addition & 1 deletion repogen/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import urllib
from os import path, PathLike
from os import PathLike
from pathlib import Path
from typing import Callable, TypeVar
from urllib.parse import urljoin
Expand Down
4 changes: 2 additions & 2 deletions repogen/ipk_file.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import io
import json
import re
import tarfile
from typing import Optional, TypedDict, NotRequired
from typing import TypedDict, NotRequired

import ar
import io


class AppInfo(TypedDict):
Expand Down
3 changes: 1 addition & 2 deletions repogen/lintpkg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from os import path
from pathlib import Path
from urllib.parse import urlparse
from urllib.request import url2pathname
Expand Down Expand Up @@ -80,7 +79,7 @@ def _validate_manifest_url(url: str, key: str, e: [str]):
else:
e.append(f"{key} must be accessible")
case 'file':
assert path.isfile(url2pathname(manifest_url_pre.path))
assert Path(url2pathname(manifest_url_pre.path)).exists()
case _:
e.append(f"{key} must be HTTPS URL")

Expand Down
2 changes: 1 addition & 1 deletion repogen/pkg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def from_package_info(pkgid: str, content: PackageRegistry, offline=False):


def list_packages(pkgdir: Path, offline: bool = False) -> List[PackageInfo]:
paths: List[Path] = [pkgdir.joinpath(f) for f in pkgdir.iterdir() if f.is_file()]
paths: List[Path] = [f for f in pkgdir.iterdir() if f.is_file()]
return sorted(filter(lambda x: x, map(lambda p: from_package_info_file(p, offline), paths)),
key=lambda x: x['title'])

Expand Down

0 comments on commit be50c94

Please sign in to comment.