Package: cs.app.flag
Persistent filesystem based flags for state and control.Latest release 20201228:
Import fix.Filesystem visible boolean flags
for control and status,
allowing easy monitoring of services or other status,
and control by flag management
for programmes which also monitor the flags.The flags are expressed as individual files with uppercase names
in a common directory ($HOME/var/flags by default);
an empty or missing file is "false"
and a nonempty file is "true".The Flags class provides easy Pythonic access to this directory.
It presents as a modifiable mapping whose keys are the flag names:flags = Flags()
  flags['UNTOPPOST'] = TrueThere is also a FlaggedMixin class providing convenient methods and attributes
for maintaining a collection of flags associated with some object
with flag names prefixed by the object's .name attribute
uppercased and with an underscore appended:class SvcD(...,FlaggedMixin):
    def __init__(self, name, ...)
      self.name = name
      FlaggedMixin.__init__(self)
      ...
    def disable(self):
      self.flag_disable = True
    def restart(self):
      self.flag_restart = True
    def _restart(self):
      self.flag_restart = False
      ... restart the SvcD ...so that an object set up as:svcd = SvcD("portfwd")
  print(svcd.flag_disable)accesses the flag named "PORTFWD_DISABLE".FunctionDummyFlags()factory to make a dummy flagslike object without persistent storageClassFlaggedMixinA mixin class adding flag_* and flagname_* attributes.This defines the following attributes on instances:flags: theFlagsinstance providing the flag values.flags_prefix: the prefix for the flags of interest.flagname_name: the full name within.flagsof the flag referred to asname.
This is.flags_prefix + '_' +nameif.flags_prefixis not empty,
or justnameotherwise.flag_name: the value from.flagsof the flag referred to asname.
This is a setable attribute
with changes propagated to.flags.MethodFlaggedMixin.__init__(self, flags=None, debug=None, prefix=None)Initialise the mixin.Parameters:flags: optional parameter;
ifNonedefaults to a new defaultFlags().prefix: optional prefix;
if not provided the prefix is derived
from the object's.nameattribute,
or is empty if there is no.nameMethodFlaggedMixin.__getattr__(self, attr)Support .flag_suffix and .flagname_suffix.MethodFlaggedMixin.__setattr__(self, attr, value)Support .flag_suffix=value.ClassFlags(collections.abc.MutableMapping,collections.abc.Mapping,collections.abc.Collection,collections.abc.Sized,collections.abc.Iterable,collections.abc.Container,FlaggedMixin)A mapping which directly inspects the flags directory.MethodFlags.__init__(self, flagdir=None, environ=None, lock=None, debug=None)Initialise theFlagsinstance.Parameters:flagdir: the directory holding flag state files;
if omitted use the value fromcs.env.FLAGDIR(environ)environ: the environment mapping to use,
defaultos.environlock: aLocklike mutex to control multithreaded access;
if omitted no locking is downdebug: debug mode, defaultFalseMethodFlags.__getitem__(self, k)Return the truthiness of this flag.
True means a non-empty file exists.MethodFlags.__iter__(self)Iterator returning the flag names in the directory.MethodFlags.__len__(self)Return the number of flag files.MethodFlags.__setitem__(self, k, value)Set the flag value.If true, write `"1"` to the flag file.
If false, truncate the flag file.MethodFlags.init(self)Ensure the flag directory exists.MethodFlags.update_prefix(self, prefix, updates, omitted_value=False)Update all flag values commencing withprefix,
falsifying any unmentioned flags.Parameters:prefix: common prefix for updated flags.updates: iterable of(flagname,flagvalue).omitted_value: value to be assigned to any unmentioned flags,
defaultFalse.
Set this toNoneto leave unmentioned flags alone.Functionlowername(s)Lowercase letters, transmute '_' to '-'. Note: NOT the reverse of uppername.Functionmain(argv=None)Main program: inspect or modify flags.Functionmain_flagset(argv=None, stdin=None)Main program for "flagset" command.ClassPolledFlags(builtins.dict)A mapping which maintains a dict of the current state of the flags directory
and updates it regularly.This allows an application to consult the flags very frequently
without hammering the filesystem.Functiontruthy(value)Decide whether a value is considered true.Strings are converted to:'0':False'1':True'true':True(case insensitive)'false':False(case insensitive)other string values are unchanged.Other types are converted withbool().Functionuppername(s)Uppercase letters, transmute some characters to '_' or '__'.Release LogRelease 20201228:
Import fix.Release 20190729:
Python 2 backport for super call. Other minor changes.Release 20190220:
FlaggedMixin: make .flags_prefix public; document mixin attributes.Release 20181109:
Assorted minor updates.Release 20170906.2:
tweak docstringRelease 20170906.1:
Improve module docstring.Release 20170906:
Fill out module docstring.Release 20170905:
Initial PyPI release.