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