From d851108387d04b4d3ed922e6cc259db6e963c5f0 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Wed, 13 Sep 2017 00:26:34 +0300 Subject: [PATCH] miscellaneous bug fixes and improvements --- pygnulib/config.py | 2 +- pygnulib/filesystem.py | 65 +++++++++++++++++++++++++++--------------- pygnulib/generator.py | 44 +++++++++++++--------------- pygnulib/module.py | 4 ++- pygnulib/parser.py | 22 +++++++------- 5 files changed, 78 insertions(+), 59 deletions(-) diff --git a/pygnulib/config.py b/pygnulib/config.py index aff0693bcf..8c1bb2e8f7 100644 --- a/pygnulib/config.py +++ b/pygnulib/config.py @@ -526,7 +526,7 @@ class Cache(Base): raise FileNotFoundError(path) with _codecs_.open(path, "rb", "UTF-8") as stream: data = stream.read() - regex = r"AC_DEFUN\(\[%s_FILE_LIST\], \[(.*?)\]\)".format(self["macro-prefix"]) + regex = r"AC_DEFUN\(\[{0}_FILE_LIST\], \[(.*?)\]\)".format(self["macro-prefix"]) pattern = _re_.compile(regex, _re_.S | _re_.M) match = pattern.findall(data) if match: diff --git a/pygnulib/filesystem.py b/pygnulib/filesystem.py index 8b23a09e66..84f3bb0434 100644 --- a/pygnulib/filesystem.py +++ b/pygnulib/filesystem.py @@ -15,26 +15,34 @@ from .module import File as _FileModule_ class Directory: """gnulib generic virtual file system""" - _SUBST_ = { - "build-aux" : "aux-dir", - "doc" : "doc-base", - "lib" : "source-base", - "m4" : "m4-base", - "tests" : "tests-base", - "tests=lib" : "tests-base", - "po" : "po-base", + _TABLE_ = { + "lib" : "source_base", + "doc" : "doc_base", + "m4" : "m4_base", + "tests" : "tests_base", + "tests=lib" : "tests_base", + "po" : "po_base", + "build-aux" : "aux_dir", } - def __init__(self, root, config): - _type_assert_("root", root, str) + def __init__(self, name, config): + _type_assert_("name", name, str) _type_assert_("config", config, _BaseConfig_) - if not _os_.path.exists(root): - raise FileNotFoundError(root) - if not _os_.path.isdir(root): - raise NotADirectoryError(root) + path = _os_.path.realpath(name) + if not _os_.path.exists(path): + raise FileNotFoundError(path) + if not _os_.path.isdir(path): + raise NotADirectoryError(path) self.__config = config - self.__root = _os_.path.realpath(root) + self.__name = name + self.__path = path + + + def __repr__(self): + module = self.__class__.__module__ + name = self.__class__.__name__ + return "%s.%s{%r}" % (module, name, self.name) def __getitem__(self, name): @@ -50,26 +58,31 @@ class Directory: parts += [part] continue if not replaced: - for old, new in Directory._SUBST_.items(): + for old, new in Directory._TABLE_.items(): if part == old: part = self.__config[new] replaced = True parts += [part] - path = _os_.path.sep.join([self.__root] + parts) + path = _os_.path.sep.join([self.__path] + parts) if not _os_.path.exists(path): raise FileNotFoundError(name) return path @property - def root(self): + def name(self): + return self.__name + + + @property + def path(self): """root directory path""" - return self.__root + return self.__path class Git(Directory): - """gnulib Git-based virtual file system""" + """gnulib git repository""" _EXCLUDE_ = { "." : str.startswith, "~" : str.endswith, @@ -83,9 +96,15 @@ class Git(Directory): } - def __init__(self, root, config): - super().__init__(root, config) - if not _os_.path.isdir(_os_.path.join(self.root, ".git")): + def __init__(self, name): + path = _os_.path.realpath(name) + if not _os_.path.exists(path): + raise FileNotFoundError(path) + if not _os_.path.isdir(path): + raise NotADirectoryError(path) + config = _BaseConfig_(root=path) + super().__init__(name, config) + if not _os_.path.isdir(_os_.path.join(self.path, ".git")): raise TypeError("%r is not a gnulib repository") diff --git a/pygnulib/generator.py b/pygnulib/generator.py index 3ee984149d..094d4d22e5 100644 --- a/pygnulib/generator.py +++ b/pygnulib/generator.py @@ -39,7 +39,9 @@ class Generator: ) def __repr__(self): - return "pygnulib.generator.Generator" + module = self.__class__.__module__ + name = self.__class__.__name__ + return "%s.%s" % (module, name) def __str__(self): return "\n".join([_ for _ in self]) @@ -112,8 +114,10 @@ class POMakefile(Generator): def __repr__(self): - fmt = "pygnulib.generator.POMakefile(po_base=%r, po_domain=%r)" - return fmt % (self.po_base, self.po_domain) + module = self.__class__.__module__ + name = self.__class__.__name__ + fmt = "%s.%s{po_base=%r, po_domain=%r}" + return fmt % (module, name, self.po_base, self.po_domain) def __iter__(self): @@ -147,8 +151,10 @@ class POTFILES(Generator): def __repr__(self): - fmt = "pygnulib.generator.POTFILES(files=%r)" - return fmt % self.files + module = self.__class__.__module__ + name = self.__class__.__name__ + fmt = "%s.%s{files=%r}" + return fmt % (module, name, self.files) def __iter__(self): @@ -203,15 +209,18 @@ class AutoconfSnippet(Generator): def __repr__(self): flags = [] + module = self.__class__.__module__ + name = self.__class__.__name__ if self.toplevel: flags += ["toplevel"] if self.libtool: flags += ["libtool"] if self.gettext: flags += ["gettext"] - fmt = "pygnulib.generator.AutoconfSnippet(include_guard_prefix=%r, flags=%s)" include_guard_prefix = self.__config.include_guard_prefix - return fmt % (include_guard_prefix, "|".join(flags)) + flags = "|".join(flags) + fmt = "%s.%s{include_guard_prefix=%r, flags=%s}" + return fmt % (module, name, include_guard_prefix, flags) def __iter__(self): @@ -268,8 +277,10 @@ class InitMacro(Generator): def __repr__(self): - fmt = "pygnulib.generator.InitMacro(macro_prefix=%r)" - return fmt % self.macro_prefix + module = self.__class__.__module__ + name = self.__class__.__name__ + fmt = "%s.%s{macro_prefix=%r}" + return fmt % (module, name, self.macro_prefix) @@ -283,11 +294,6 @@ class InitMacroHeader(InitMacro): super().__init__(config=config, macro_prefix=macro_prefix) - def __repr__(self): - fmt = "pygnulib.generator.InitMacroHeader(macro_prefix=%r)" - return fmt % self.macro_prefix - - def __iter__(self): # Overriding AC_LIBOBJ and AC_REPLACE_FUNCS has the effect of storing # platform-dependent object files in ${macro_prefix_arg}_LIBOBJS instead @@ -366,11 +372,6 @@ class InitMacroFooter(InitMacro): super().__init__(config=config, macro_prefix=macro_prefix) - def __repr__(self): - fmt = "pygnulib.generator.InitMacroFooter(macro_prefix=%r)" - return fmt % self.macro_prefix - - def __iter__(self): # Check the presence of files that are mentioned as AC_LIBSOURCES # arguments. The check is performed only when autoconf is run from the @@ -428,11 +429,6 @@ class InitMacroDone(InitMacro): return self.__source_base - def __repr__(self): - fmt = "pygnulib.generator.InitMacroDone(source_base=%r, macro_prefix=%r)" - return fmt % (self.source_base, self.macro_prefix) - - def __iter__(self): for line in InitMacroDone._TEMPLATE_: yield line.format(source_base=self.__source_base, macro_prefix=self.macro_prefix) diff --git a/pygnulib/module.py b/pygnulib/module.py index 014144407a..418a553453 100644 --- a/pygnulib/module.py +++ b/pygnulib/module.py @@ -272,7 +272,9 @@ class Base: def __repr__(self): - return self.__name + module = self.__class__.__module__ + name = self.__class__.__name__ + return "%s.%s{%r}" % (module, name, self.name) def __str__(self): diff --git a/pygnulib/parser.py b/pygnulib/parser.py index 9918313357..006b3ef212 100644 --- a/pygnulib/parser.py +++ b/pygnulib/parser.py @@ -84,7 +84,7 @@ class CommandLine: super().__init__(default=_argparse_.SUPPRESS, *args, **kwargs) - def __call__(self, parser, namespace, value, option): + def __call__(self, parser, namespace, value, option=None): if hasattr(namespace, "mode"): mode = getattr(namespace, "mode") if not self.__flags & mode and mode != CommandLine._HELP_: @@ -99,9 +99,10 @@ class CommandLine: super().__init__(*args, **kwargs) - def __call__(self, parser, namespace, value, option): - super().__call__(parser, namespace, value, option) + def __call__(self, parser, namespace, value, option=None): + args = (parser, namespace, value, option) setattr(parser, self.dest, self.__const) + super().__call__(*args) class _TrueOption_(_ConstOption_): @@ -133,7 +134,7 @@ class CommandLine: super().__init__(*args, **kwargs) - def __call__(self, parser, namespace, value, option): + def __call__(self, parser, namespace, value, option=None): args = (parser, namespace, value, option) if not hasattr(namespace, self.dest): setattr(namespace, self.dest, 0) @@ -154,7 +155,7 @@ class CommandLine: setattr(namespace, self.dest, CommandLine._HELP_) if old_mode != CommandLine._HELP_: if old_mode != new_mode: - if not old_mode == 0: + if old_mode != 0: fmt = "argument {0}: not allowed with {1}" parser.error(fmt.format(new_option, old_option)) setattr(namespace, "modules", list(value)) @@ -163,7 +164,7 @@ class CommandLine: class _AvoidOption_(_Option_): - def __call__(self, parser, namespace, value, option): + def __call__(self, parser, namespace, value, option=None): args = (parser, namespace, value, option) if not hasattr(namespace, self.dest): setattr(namespace, self.dest, list()) @@ -173,7 +174,7 @@ class CommandLine: class _VerbosityAction_(_Option_): - def __call__(self, parser, namespace, value, option): + def __call__(self, parser, namespace, value, option=None): args = (parser, namespace, value, option) if not hasattr(namespace, self.dest): setattr(namespace, self.dest, 0) @@ -185,7 +186,7 @@ class CommandLine: class _LicenseOption_(_Option_): - def __call__(self, parser, namespace, value, option): + def __call__(self, parser, namespace, value, option=None): args = (parser, namespace, value, option) if value not in ("2", "3"): parser.__error("illegal --license argument value") @@ -193,7 +194,7 @@ class CommandLine: class _LinkOption_(_Option_): - def __call__(self, parser, namespace, value, option): + def __call__(self, parser, namespace, value, option=None): args = (parser, namespace, value, option) if not hasattr(namespace, self.dest): setattr(parser, self.dest, CommandLine._LINK_NOTICE_) @@ -868,6 +869,7 @@ class CommandLine: class Option(_enum_.Flag): + """option bitwise flags""" DryRun = (1 << 0) Symlink = (1 << 1) Hardlink = (1 << 2) @@ -927,7 +929,7 @@ class CommandLine: mode = namespace.pop("mode", None) if mode is None: self.__parser.error("no operating mode selected") - if len(arguments) > 0 and mode != CommandLine._HELP_: + if arguments and mode != CommandLine._HELP_: fmt = "unrecognized arguments: {0}" arguments = " ".join(arguments) self.__parser.error(fmt.format(arguments)) -- 2.39.5