Switch to pathlib
This commit is contained in:
@@ -7,6 +7,7 @@ sys.path.insert(0, '/usr/lib/python3.6/site-packages')
|
||||
import requests
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import ast
|
||||
import argparse
|
||||
import zipfile
|
||||
@@ -44,28 +45,28 @@ def parse_blinfo(source: str) -> dict:
|
||||
raise BadAddon('No bl_info found')
|
||||
|
||||
|
||||
def extract_blinfo(path):
|
||||
def extract_blinfo(path: pathlib.Path) -> dict:
|
||||
"""Extract bl_info dict from addon at path (can be single file, module, or zip)"""
|
||||
|
||||
source = None
|
||||
# get last component of path, including when the path ends with trailing slash
|
||||
addon_name = os.path.split(path.rstrip(os.path.sep))[1]
|
||||
# get last component of path
|
||||
addon_name = path.parts[-1]
|
||||
|
||||
if os.path.isdir(path):
|
||||
with open(os.path.join(path, '__init__.py'), 'r') as f:
|
||||
if path.is_dir():
|
||||
with open(path / '__init__.py', 'r') as f:
|
||||
source = f.read()
|
||||
else:
|
||||
|
||||
# HACK: perhaps not the best approach determining filetype..?
|
||||
try:
|
||||
with zipfile.ZipFile(path, 'r') as z:
|
||||
with zipfile.ZipFile(str(path), 'r') as z:
|
||||
for fname in z.namelist():
|
||||
# HACK: this seems potentially fragile; depends on zipfile listing root contents first
|
||||
if fname.endswith('__init__.py'):
|
||||
source = z.read(fname)
|
||||
break
|
||||
except zipfile.BadZipFile:
|
||||
with open(path, 'r') as f:
|
||||
with path.open() as f:
|
||||
source = f.read()
|
||||
|
||||
if source == None:
|
||||
@@ -75,18 +76,18 @@ def extract_blinfo(path):
|
||||
|
||||
|
||||
|
||||
def make_repo(outpath):
|
||||
"""Make repo.json for files in directory 'outpath'"""
|
||||
def make_repo(repopath: pathlib.Path):
|
||||
"""Make repo.json for files in directory 'repopath'"""
|
||||
|
||||
repo_data = {}
|
||||
package_data = []
|
||||
|
||||
if not os.path.exists(outpath):
|
||||
raise FileNotFoundError
|
||||
if not repopath.is_dir():
|
||||
raise FileNotFoundError(repopath)
|
||||
|
||||
for addon in os.listdir(outpath):
|
||||
for addon_path in repopath.iterdir():
|
||||
package_datum = {}
|
||||
addon_path = os.path.join(outpath, addon)
|
||||
addon = addon_path.parts[-1]
|
||||
|
||||
try:
|
||||
bl_info = extract_blinfo(addon_path)
|
||||
@@ -106,7 +107,7 @@ def make_repo(outpath):
|
||||
|
||||
repo_data['packages'] = package_data
|
||||
|
||||
with open(os.path.join(outpath, "repo.json"), 'w', encoding='utf-8') as repo_file:
|
||||
with (repopath / 'repo.json').open('w', encoding='utf-8') as repo_file:
|
||||
json.dump(repo_data, repo_file, indent=4, sort_keys=True)
|
||||
|
||||
|
||||
@@ -122,7 +123,7 @@ if __name__ == '__main__':
|
||||
|
||||
make = subparsers.add_parser('make')
|
||||
make.add_argument('path')
|
||||
make.set_defaults(func=lambda args: make_repo(args.path))
|
||||
make.set_defaults(func=lambda args: make_repo(pathlib.Path(args.path)))
|
||||
|
||||
args = parser.parse_args()
|
||||
args.func(args)
|
||||
|
@@ -1,21 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import unittest
|
||||
import os.path
|
||||
from pathlib import Path
|
||||
import os
|
||||
import json
|
||||
import blenderpack
|
||||
|
||||
class test_blenderpack_make_repo(unittest.TestCase):
|
||||
|
||||
helper_path = os.path.join('tests', 'test_helpers')
|
||||
addon_path = os.path.join(helper_path, 'addons')
|
||||
helper_path = Path('tests', 'test_helpers')
|
||||
addon_path = Path(helper_path, 'addons')
|
||||
|
||||
def test_extract_blinfo_from_nonexistent(self):
|
||||
test_file = 'file_that_doesnt_exist'
|
||||
self.assertRaises(
|
||||
FileNotFoundError,
|
||||
blenderpack.extract_blinfo,
|
||||
os.path.join(self.addon_path, test_file)
|
||||
Path(self.addon_path, test_file)
|
||||
)
|
||||
|
||||
def test_extract_blinfo_from_nonaddon(self):
|
||||
@@ -23,22 +24,22 @@ class test_blenderpack_make_repo(unittest.TestCase):
|
||||
self.assertRaises(
|
||||
blenderpack.BadAddon,
|
||||
blenderpack.extract_blinfo,
|
||||
os.path.join(self.addon_path, test_file)
|
||||
Path(self.addon_path, test_file)
|
||||
)
|
||||
|
||||
def test_make_repo_valid(self):
|
||||
blenderpack.make_repo(os.path.join(self.helper_path, 'addons'))
|
||||
repojson = os.path.join(self.helper_path, 'addons', 'repo.json')
|
||||
blenderpack.make_repo(Path(self.helper_path, 'addons'))
|
||||
repojson = Path(self.helper_path, 'addons', 'repo.json')
|
||||
|
||||
self.assertTrue(os.path.exists(repojson))
|
||||
with open(repojson, 'r') as f:
|
||||
self.assertTrue(repojson.is_file())
|
||||
with repojson.open('r') as f:
|
||||
json.loads(f.read())
|
||||
|
||||
os.remove(repojson)
|
||||
self.fail('unfinished test')
|
||||
|
||||
def test_make_repo_from_nonexistent(self):
|
||||
blenderpack.make_repo(os.path.join(self.helper_path, 'addons'))
|
||||
blenderpack.make_repo(Path(self.helper_path, 'addons'))
|
||||
self.fail('unfinished test')
|
||||
|
||||
|
||||
@@ -51,8 +52,8 @@ bl_info_tests = {
|
||||
|
||||
def generate_test(test_file):
|
||||
def test(self):
|
||||
reality = str(blenderpack.extract_blinfo(os.path.join(self.addon_path, test_file)))
|
||||
with open(os.path.join(self.helper_path, test_file + '_output'), 'r') as f:
|
||||
reality = str(blenderpack.extract_blinfo(Path(self.addon_path, test_file)))
|
||||
with (self.helper_path / (test_file + '_output')).open() as f:
|
||||
expectation = f.read()
|
||||
self.assertEqual(expectation, reality)
|
||||
return test
|
||||
|
Reference in New Issue
Block a user