Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions doc/manual/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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",
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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).
Expand All @@ -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,
Expand Down
96 changes: 46 additions & 50 deletions src/diffpy/confutils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,19 @@

import argparse
import os
import re
import sys
from collections import OrderedDict
from configparser import ConfigParser
from functools import partial

try:
from collections import OrderedDict
except:
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
Expand Down Expand Up @@ -129,7 +114,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": "",
},
],
Expand Down Expand Up @@ -206,7 +194,7 @@ class ConfigBase(object):
"fliphorizontal",
{
"sec": "Beamline",
"h": "filp the image horizontally",
"h": "flip the image horizontally",
"n": "?",
"co": True,
"d": False,
Expand All @@ -228,8 +216,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],
},
Expand Down Expand Up @@ -266,7 +259,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
Expand All @@ -293,9 +286,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
Expand All @@ -317,9 +310,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

Expand Down Expand Up @@ -500,7 +493,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 = []
Expand All @@ -522,7 +515,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 = []
Expand All @@ -543,8 +536,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()
Expand Down Expand Up @@ -576,7 +569,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
Expand Down Expand Up @@ -608,16 +601,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()
Expand All @@ -641,18 +634,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
Expand All @@ -663,7 +658,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 = (
Expand Down Expand Up @@ -707,15 +702,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 = (
Expand Down Expand Up @@ -757,7 +752,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):
Expand All @@ -766,14 +761,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()

Expand Down