From 8e9c1d189e419ba1ab3f7d0a40761253c5bf8144 Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Sun, 19 Oct 2025 12:42:02 -0400 Subject: [PATCH 1/2] fix: fix codespell, remove unused module, and fix string too long issue. --- doc/manual/source/conf.py | 31 +++++++----- src/diffpy/confutils/config.py | 92 +++++++++++++++++----------------- 2 files changed, 64 insertions(+), 59 deletions(-) diff --git a/doc/manual/source/conf.py b/doc/manual/source/conf.py index 88a36f5..514f189 100644 --- a/doc/manual/source/conf.py +++ b/doc/manual/source/conf.py @@ -4,7 +4,8 @@ # diffpy.Structure documentation build configuration file, created by # sphinx-quickstart on Tue Oct 22 12:02:48 2013. # -# This file is execfile()d with the current directory set to its containing dir. +# This file is execfile()d with the current directory set to +# its containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. @@ -16,6 +17,8 @@ import sys import time +from setup import versiondata + # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. @@ -25,12 +28,13 @@ # abbreviations ab_authors = "Xiaohao Yang, Simon J.L. Billinge group" -# -- General configuration ----------------------------------------------------- +# -- General configuration ---------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. # needs_sphinx = '1.0' -# Add any Sphinx extension module names here, as strings. They can be extensions +# Add any Sphinx extension module names here, as strings. +# They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = [ "sphinx.ext.autodoc", @@ -58,7 +62,6 @@ # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. -from setup import versiondata fullversion = versiondata.get("DEFAULT", "version") # The short X.Y version. @@ -85,7 +88,8 @@ # directories to ignore when looking for source files. exclude_patterns = [] -# The reST default role (used for this markup: `text`) to use for all documents. +# The reST default role (used for this markup: `text`) +# to use for all documents. # default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. @@ -108,7 +112,7 @@ # Display all warnings for missing links. nitpicky = True -# -- Options for HTML output --------------------------------------------------- +# -- Options for HTML output -------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. @@ -189,19 +193,20 @@ htmlhelp_basename = "Structuredoc" -# -- Options for LaTeX output -------------------------------------------------- +# -- Options for LaTeX output ------------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). - #'papersize': 'letterpaper', + # 'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). - #'pointsize': '10pt', + # 'pointsize': '10pt', # Additional stuff for the LaTeX preamble. - #'preamble': '', + # 'preamble': '', } # Grouping the document tree into LaTeX files. List of tuples -# (source start file, target name, title, author, documentclass [howto/manual]). +# (source start file, target name, title, author, +# documentclass [howto/manual]). latex_documents = [ ( "index", @@ -233,7 +238,7 @@ # latex_domain_indices = True -# -- Options for manual page output -------------------------------------------- +# -- Options for manual page output ------------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). @@ -243,7 +248,7 @@ # man_show_urls = False -# -- Options for Texinfo output ------------------------------------------------ +# -- Options for Texinfo output ----------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, diff --git a/src/diffpy/confutils/config.py b/src/diffpy/confutils/config.py index 358a7fa..f2da051 100644 --- a/src/diffpy/confutils/config.py +++ b/src/diffpy/confutils/config.py @@ -23,34 +23,23 @@ import argparse import os -import re -import sys from configparser import ConfigParser -from functools import partial try: from collections import OrderedDict -except: +except ImportError: from ordereddict import OrderedDict -from diffpy.confutils.tools import ( - FakeConfigFile, - StrConv, - _configPropertyR, - _configPropertyRad, - _configPropertyRW, - opt2Str, - str2bool, - str2Opt, -) +from diffpy.confutils.tools import FakeConfigFile, StrConv, opt2Str, str2Opt class ConfigBase(object): """_optdatalist_default, _optdatalist are metadata used to initialize the options, see below for examples. - options presents in --help (in cmd), config file, headers have same order as - in these list, so arrange them in right order here. + options presents in --help (in cmd), config file, + headers have same order as in these list, + so arrange them in right order here. optional args to control if the options presents in args, config file or file header @@ -129,7 +118,10 @@ class ConfigBase(object): "sec": "Control", "config": "n", "header": "n", - "h": "create a config file according to default or current values", + "h": ( + "create a config file according to default" + "or current values" + ), "d": "", }, ], @@ -206,7 +198,7 @@ class ConfigBase(object): "fliphorizontal", { "sec": "Beamline", - "h": "filp the image horizontally", + "h": "flip the image horizontally", "n": "?", "co": True, "d": False, @@ -228,8 +220,13 @@ class ConfigBase(object): "sec": "Others", "config": "f", "header": "f", - "h": "mask the edge pixels, first four means the number of pixels masked in each edge \ - (left, right, top, bottom), the last one is the radius of a region masked around the corner", + "h": ( + "mask the edge pixels, first four means" + " the number of pixels masked in each edge \n" + " (left, right, top, bottom)," + " the last one is the radius of a region" + " masked around the corner" + ), "n": 5, "d": [1, 1, 1, 1, 50], }, @@ -266,7 +263,7 @@ def __init__(self, filename=None, args=None, **kwargs): # update config, first detect if a default config should be load filename = self._findDefaultConfigFile(filename, args, **kwargs) - rv = self.updateConfig(filename, args, **kwargs) + self.updateConfig(filename, args, **kwargs) return # example, overload it @@ -293,9 +290,9 @@ def _findConfigFile(self, filename=None, args=None, **kwargs): :return: name of config file if found, otherwise None """ rv = None - if filename != None: + if filename is not None: rv = filename - if args != None: + if args is not None: if ("--configfile" in args) or ("-c" in args): obj = self.args.parse_args(args) rv = obj.configfile @@ -317,9 +314,9 @@ def _findDefaultConfigFile(self, filename=None, args=None, **kwargs): return: name of config file if found, otherwise None """ rv = self._findConfigFile(filename, args, **kwargs) - if rv == None: + if rv is None: for dconf in self._defaultdata["configfile"]: - if (os.path.exists(dconf)) and (rv == None): + if (os.path.exists(dconf)) and (rv is None): rv = dconf return rv @@ -500,7 +497,7 @@ def _copyConfigtoSelf(self, optnames=None): value copied from self.config to self.*option*'. Set None to update all """ - if optnames != None: + if optnames is not None: optnames = optnames if isinstance(optnames, list) else [optnames] else: optnames = [] @@ -522,7 +519,7 @@ def _copySelftoConfig(self, optnames=None): copied from self.*option* to self.config. Set None to update all """ - if optnames != None: + if optnames is not None: optnames = optnames if isinstance(optnames, list) else [optnames] else: optnames = [] @@ -543,8 +540,8 @@ def parseArgs(self, pargs): """Parse args and update the value in self.*option*, this will call the self.args() to parse args, - :param pargs: list of string, arguments to parse, usually - comming from sys.argv + :param pargs: list of string, arguments to parse, usually coming + from sys.argv """ obj = self.args.parse_args(pargs) changedargs = obj.__dict__.keys() @@ -576,7 +573,7 @@ def parseConfigFile(self, filename): :param filename: str, file name of config file (include path) """ - if filename != None: + if filename is not None: filename = os.path.abspath(filename) if os.path.exists(filename): self.configfile = filename @@ -608,16 +605,16 @@ def updateConfig(self, filename=None, args=None, **kwargs): self._preUpdateConfig(**kwargs) filename = self._findConfigFile(filename, args, **kwargs) - if filename != None: + if filename is not None: rv = self.parseConfigFile(filename) - if args != None: + if args is not None: rv = self.parseArgs(args) if kwargs != {}: rv = self.parseKwargs(**kwargs) if ( - (filename == None) - and ((args == None) or (args == [])) + (filename is None) + and ((args is None) or (args == [])) and (kwargs == {}) ): rv = self._updateSelf() @@ -641,18 +638,20 @@ def _postUpdateConfig(self, **kwargs): ########################################################################### def _createConfigFile(self): - """Write output config file if specfied in configuration the + """Write output config file if specified in configuration the filename is specified by self.createconfig.""" - if (self.createconfig != "") and (self.createconfig != None): + if (self.createconfig != "") and (self.createconfig is not None): self.writeConfig(self.createconfig, "short") self.createconfig = "" - if (self.createconfigfull != "") and (self.createconfigfull != None): + if (self.createconfigfull != "") and ( + self.createconfigfull is not None + ): self.writeConfig(self.createconfigfull, "full") self.createconfigfull = "" return def writeConfig(self, filename, mode="short", changeconfigfile=True): - """Write config to file. the file is compatiable with python + """Write config to file. the file is compatible with python package ConfigParser. :param filename: string, name of file @@ -663,7 +662,7 @@ def writeConfig(self, filename, mode="short", changeconfigfile=True): if changeconfigfile: self.configfile = os.path.abspath(filename) self._updateSelf() - # func decide if wirte the option to config according to mode + # func decide if write the option to config according to mode # options not present in self._optdata will not be written to config if mode.startswith("s"): mcond = ( @@ -707,15 +706,15 @@ def getHeader(self, title=None, mode="full"): mode, all options with 'a' will be written, in full mode, all options with 'a' or 'f' will be written :return: string, lines with line break that can be directly - writen to a text file + written to a text file """ lines = [] title = "# %s #" % ( - self._defaultdata["headertitle"] if title == None else title + self._defaultdata["headertitle"] if title is None else title ) lines.append(title) - # func decide if wirte the option to header according to mode + # func decide if write the option to header according to mode # options not present in self._optdata will not be written to header if mode.startswith("s"): mcond = ( @@ -757,7 +756,7 @@ def resetDefault(self, optnames=None): :param optnames: list of str, name of options to reset, None for all options """ - if optnames == None: + if optnames is None: optnames = self._optdata.keys() for optname in optnames: if self._optdata.has_key(optname): @@ -766,14 +765,15 @@ def resetDefault(self, optnames=None): return ########################################################################### - # IMPORTANT call this method if you want to add options as class attributes!!! + # IMPORTANT call this method if you want to add options + # as class attributes!!! @classmethod def initConfigClass(cls): """Init config class and add options to class. - IMPORTANT: call this method after you define the metadata of your config - class to add options as class attributes!!! + IMPORTANT: call this method after you define + the metadata of your config class to add options as class attributes!!! """ cls._preInitConfigClass() From 06813fdfce3fa445e12288d52749362cf99648f8 Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Sun, 19 Oct 2025 22:14:59 -0400 Subject: [PATCH 2/2] style: remove try except to import module --- src/diffpy/confutils/config.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/diffpy/confutils/config.py b/src/diffpy/confutils/config.py index f2da051..21045e9 100644 --- a/src/diffpy/confutils/config.py +++ b/src/diffpy/confutils/config.py @@ -23,13 +23,9 @@ import argparse import os +from collections import OrderedDict from configparser import ConfigParser -try: - from collections import OrderedDict -except ImportError: - from ordereddict import OrderedDict - from diffpy.confutils.tools import FakeConfigFile, StrConv, opt2Str, str2Opt