refactor(perflint): improve imports, specifically in forloops

main
Marshall Hallenbeck 2023-05-05 14:44:11 -04:00
parent 788701cb2c
commit e5d997fb88
2 changed files with 19 additions and 15 deletions

View File

@ -1,7 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import importlib
import os
from os import listdir
from os.path import dirname
from os.path import join as path_join
import sys
import cme
@ -130,14 +132,14 @@ class ModuleLoader:
"""
modules = {}
modules_paths = [
os.path.join(os.path.dirname(cme.__file__), "modules"),
os.path.join(CME_PATH, "modules"),
path_join(dirname(cme.__file__), "modules"),
path_join(CME_PATH, "modules"),
]
for path in modules_paths:
for module in os.listdir(path):
for module in listdir(path):
if module[-3:] == ".py" and module != "example_module.py":
module_path = os.path.join(path, module)
module_path = path_join(path, module)
module_data = self.get_module_info(module_path)
modules.update(module_data)
return modules

View File

@ -2,13 +2,15 @@
# -*- coding: utf-8 -*-
import types
from importlib.machinery import SourceFileLoader
import os
from os import listdir
from os.path import join as path_join
from os.path import dirname, exists, expanduser
import cme
class ProtocolLoader:
def __init__(self):
self.cme_path = os.path.expanduser("~/.cme")
self.cme_path = expanduser("~/.cme")
def load_protocol(self, protocol_path):
loader = SourceFileLoader("protocol", protocol_path)
@ -19,23 +21,23 @@ class ProtocolLoader:
def get_protocols(self):
protocols = {}
protocol_paths = [
os.path.join(os.path.dirname(cme.__file__), "protocols"),
os.path.join(self.cme_path, "protocols"),
path_join(dirname(cme.__file__), "protocols"),
path_join(self.cme_path, "protocols"),
]
for path in protocol_paths:
for protocol in os.listdir(path):
for protocol in listdir(path):
if protocol[-3:] == ".py" and protocol[:-3] != "__init__":
protocol_path = os.path.join(path, protocol)
protocol_path = path_join(path, protocol)
protocol_name = protocol[:-3]
protocols[protocol_name] = {"path": protocol_path}
db_file_path = os.path.join(path, protocol_name, "database.py")
db_nav_path = os.path.join(path, protocol_name, "db_navigator.py")
if os.path.exists(db_file_path):
db_file_path = path_join(path, protocol_name, "database.py")
db_nav_path = path_join(path, protocol_name, "db_navigator.py")
if exists(db_file_path):
protocols[protocol_name]["dbpath"] = db_file_path
if os.path.exists(db_nav_path):
if exists(db_nav_path):
protocols[protocol_name]["nvpath"] = db_nav_path
return protocols