Addon Manager: Black reformatting
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#***************************************************************************
|
||||
#* *
|
||||
#* Copyright (c) 2018 Gaël Écorchard <galou_breizh@yahoo.fr> *
|
||||
#* *
|
||||
#* This program is free software; you can redistribute it and/or modify *
|
||||
#* it under the terms of the GNU Lesser General Public License (LGPL) *
|
||||
#* as published by the Free Software Foundation; either version 2 of *
|
||||
#* the License, or (at your option) any later version. *
|
||||
#* for detail see the LICENCE text file. *
|
||||
#* *
|
||||
#* This program is distributed in the hope that it will be useful, *
|
||||
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
#* GNU Library General Public License for more details. *
|
||||
#* *
|
||||
#* You should have received a copy of the GNU Library General Public *
|
||||
#* License along with this program; if not, write to the Free Software *
|
||||
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
|
||||
#* USA *
|
||||
#* *
|
||||
#***************************************************************************
|
||||
# ***************************************************************************
|
||||
# * *
|
||||
# * Copyright (c) 2018 Gaël Écorchard <galou_breizh@yahoo.fr> *
|
||||
# * *
|
||||
# * This program is free software; you can redistribute it and/or modify *
|
||||
# * it under the terms of the GNU Lesser General Public License (LGPL) *
|
||||
# * as published by the Free Software Foundation; either version 2 of *
|
||||
# * the License, or (at your option) any later version. *
|
||||
# * for detail see the LICENCE text file. *
|
||||
# * *
|
||||
# * This program is distributed in the hope that it will be useful, *
|
||||
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
# * GNU Library General Public License for more details. *
|
||||
# * *
|
||||
# * You should have received a copy of the GNU Library General Public *
|
||||
# * License along with this program; if not, write to the Free Software *
|
||||
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
|
||||
# * USA *
|
||||
# * *
|
||||
# ***************************************************************************
|
||||
|
||||
import os
|
||||
import re
|
||||
@@ -36,6 +36,7 @@ from addonmanager_utilities import remove_directory_if_empty
|
||||
|
||||
try:
|
||||
from HTMLParser import HTMLParser
|
||||
|
||||
unescape = HTMLParser().unescape
|
||||
except ImportError:
|
||||
from html import unescape
|
||||
@@ -46,6 +47,7 @@ except ImportError:
|
||||
# different sources
|
||||
# @{
|
||||
|
||||
|
||||
class Macro(object):
|
||||
"""This class provides a unified way to handle macros coming from different sources"""
|
||||
|
||||
@@ -65,14 +67,14 @@ class Macro(object):
|
||||
return self.filename == other.filename
|
||||
|
||||
@classmethod
|
||||
def from_cache (self, cache_dict:Dict):
|
||||
def from_cache(self, cache_dict: Dict):
|
||||
instance = Macro(cache_dict["name"])
|
||||
for key,value in cache_dict.items():
|
||||
for key, value in cache_dict.items():
|
||||
instance.__dict__[key] = value
|
||||
return instance
|
||||
|
||||
def to_cache (self) -> Dict:
|
||||
""" For cache purposes this entire class is dumped directly """
|
||||
def to_cache(self) -> Dict:
|
||||
"""For cache purposes this entire class is dumped directly"""
|
||||
|
||||
return self.__dict__
|
||||
|
||||
@@ -85,7 +87,11 @@ class Macro(object):
|
||||
def is_installed(self):
|
||||
if self.on_git and not self.src_filename:
|
||||
return False
|
||||
return (os.path.exists(os.path.join(FreeCAD.getUserMacroDir(True), self.filename)) or os.path.exists(os.path.join(FreeCAD.getUserMacroDir(True), "Macro_" + self.filename)))
|
||||
return os.path.exists(
|
||||
os.path.join(FreeCAD.getUserMacroDir(True), self.filename)
|
||||
) or os.path.exists(
|
||||
os.path.join(FreeCAD.getUserMacroDir(True), "Macro_" + self.filename)
|
||||
)
|
||||
|
||||
def fill_details_from_file(self, filename):
|
||||
with open(filename) as f:
|
||||
@@ -123,7 +129,11 @@ class Macro(object):
|
||||
code = ""
|
||||
u = urlopen(url)
|
||||
if u is None:
|
||||
FreeCAD.Console.PrintWarning("AddonManager: Debug: connection is lost (proxy setting changed?)", url, "\n")
|
||||
FreeCAD.Console.PrintWarning(
|
||||
"AddonManager: Debug: connection is lost (proxy setting changed?)",
|
||||
url,
|
||||
"\n",
|
||||
)
|
||||
return
|
||||
p = u.read()
|
||||
if isinstance(p, bytes):
|
||||
@@ -132,12 +142,14 @@ class Macro(object):
|
||||
# check if the macro page has its code hosted elsewhere, download if
|
||||
# needed
|
||||
if "rawcodeurl" in p:
|
||||
rawcodeurl = re.findall("rawcodeurl.*?href=\"(http.*?)\">", p)
|
||||
rawcodeurl = re.findall('rawcodeurl.*?href="(http.*?)">', p)
|
||||
if rawcodeurl:
|
||||
rawcodeurl = rawcodeurl[0]
|
||||
u2 = urlopen(rawcodeurl)
|
||||
if u2 is None:
|
||||
FreeCAD.Console.PrintWarning("AddonManager: Debug: unable to open URL", rawcodeurl, "\n")
|
||||
FreeCAD.Console.PrintWarning(
|
||||
"AddonManager: Debug: unable to open URL", rawcodeurl, "\n"
|
||||
)
|
||||
return
|
||||
# code = u2.read()
|
||||
# github is slow to respond... We need to use this trick below
|
||||
@@ -165,14 +177,27 @@ class Macro(object):
|
||||
code = unescape(code)
|
||||
code = code.replace(b"\xc2\xa0".decode("utf-8"), " ")
|
||||
else:
|
||||
FreeCAD.Console.PrintWarning(translate("AddonsInstaller", "Unable to fetch the code of this macro.") + "\n")
|
||||
FreeCAD.Console.PrintWarning(
|
||||
translate(
|
||||
"AddonsInstaller", "Unable to fetch the code of this macro."
|
||||
)
|
||||
+ "\n"
|
||||
)
|
||||
|
||||
desc = re.findall(r"<td class=\"ctEven left macro-description\">(.*?)</td>", p.replace("\n", " "))
|
||||
desc = re.findall(
|
||||
r"<td class=\"ctEven left macro-description\">(.*?)</td>",
|
||||
p.replace("\n", " "),
|
||||
)
|
||||
if desc:
|
||||
desc = desc[0]
|
||||
else:
|
||||
FreeCAD.Console.PrintWarning(translate("AddonsInstaller",
|
||||
"Unable to retrieve a description for this macro.") + "\n")
|
||||
FreeCAD.Console.PrintWarning(
|
||||
translate(
|
||||
"AddonsInstaller",
|
||||
"Unable to retrieve a description for this macro.",
|
||||
)
|
||||
+ "\n"
|
||||
)
|
||||
desc = "No description available"
|
||||
self.desc = desc
|
||||
self.url = url
|
||||
@@ -184,7 +209,7 @@ class Macro(object):
|
||||
self.code = code
|
||||
self.parsed = True
|
||||
|
||||
def install(self, macro_dir:str) -> (bool, List[str]):
|
||||
def install(self, macro_dir: str) -> (bool, List[str]):
|
||||
"""Install a macro and all its related files
|
||||
|
||||
Returns True if the macro was installed correctly.
|
||||
@@ -195,7 +220,7 @@ class Macro(object):
|
||||
"""
|
||||
|
||||
if not self.code:
|
||||
return False,["No code"]
|
||||
return False, ["No code"]
|
||||
if not os.path.isdir(macro_dir):
|
||||
try:
|
||||
os.makedirs(macro_dir)
|
||||
@@ -203,7 +228,7 @@ class Macro(object):
|
||||
return False, [f"Failed to create {macro_dir}"]
|
||||
macro_path = os.path.join(macro_dir, self.filename)
|
||||
try:
|
||||
with codecs.open(macro_path, 'w', 'utf-8') as macrofile:
|
||||
with codecs.open(macro_path, "w", "utf-8") as macrofile:
|
||||
macrofile.write(self.code)
|
||||
except IOError:
|
||||
return False, [f"Failed to write {macro_path}"]
|
||||
@@ -226,11 +251,10 @@ class Macro(object):
|
||||
warnings.append(f"Failed to copy {src_file} to {dst_file}")
|
||||
if len(warnings) > 0:
|
||||
return False, warnings
|
||||
|
||||
|
||||
FreeCAD.Console.PrintMessage(f"Macro {self.name} was installed successfully.\n")
|
||||
return True, []
|
||||
|
||||
|
||||
def remove(self) -> bool:
|
||||
"""Remove a macro and all its related files
|
||||
|
||||
@@ -242,7 +266,7 @@ class Macro(object):
|
||||
return True
|
||||
macro_dir = FreeCAD.getUserMacroDir(True)
|
||||
macro_path = os.path.join(macro_dir, self.filename)
|
||||
macro_path_with_macro_prefix = os.path.join(macro_dir, 'Macro_' + self.filename)
|
||||
macro_path_with_macro_prefix = os.path.join(macro_dir, "Macro_" + self.filename)
|
||||
if os.path.exists(macro_path):
|
||||
os.remove(macro_path)
|
||||
elif os.path.exists(macro_path_with_macro_prefix):
|
||||
@@ -255,7 +279,10 @@ class Macro(object):
|
||||
os.remove(dst_file)
|
||||
remove_directory_if_empty(os.path.dirname(dst_file))
|
||||
except Exception:
|
||||
FreeCAD.Console.PrintWarning(f"Failed to remove macro file '{dst_file}': it might not exist, or its permissions changed\n")
|
||||
FreeCAD.Console.PrintWarning(
|
||||
f"Failed to remove macro file '{dst_file}': it might not exist, or its permissions changed\n"
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
# @}
|
||||
|
||||
Reference in New Issue
Block a user