grampg
API¶
When interfacing the grampg
you will instantiate PasswordGenerator
, configure your character sets if necessary, and call of()
on it to instantiate an internal Generator
. The PasswordGenerator
is in fact little more than a fluent interface to build generators. The generator instance is returned only when done()
is called.
By means of the PasswordGenerator
instance, the Generator
instance can then be progressively spec’ed, so passwords generated by it can conform to you’re twisted needs.
-
class
grampg.
PasswordGenerator
(from_sets={})[source]¶ Build the password generator.
Provides a fluent interface to build
Generator
instances, by means of method chaining.Exposes the character sets. Default character sets are provided for upper and lower case letters (upper_letters and lower_letters, respectively, all mashed up in letters) and numbers. A conjunction of the three is also provided, under the name alphanumeric.
A character set can be registered by keying its name to a list of eligible characters in the
sets
attribute, or by extending the default character sets during instantiation.-
at_least
(low, setname)[source]¶ Spec method: require no less than low but no more than high characters from that set. This spec defines a range of characters.
-
at_most
(high, setname)[source]¶ Spec method: require no more than high characters from that set. This spec defines a range of characters.
-
beginning_with
(setname)[source]¶ Spec method: passwords will start with a char from this set.
Some other spec method must be called to define a number or range for that same set. Beginning with characters not specified is an error.
-
between
(low, high, setname)[source]¶ Spec method: require no less than low but no more than high characters from that set. This spec defines a range of characters.
-
done
()[source]¶ Finalize the generator and return it.
The returned instance can receive calls to
generate()
, each of which will produce an independent password complying with the specs.
-
ending_with
(setname)[source]¶ Spec method: passwords will end with a char from this set.
Some other spec method must be called to define a number or range for that same set. Ending with characters not specified is an error.
-
of
()[source]¶ Commence a method chain building a fresh generator instance.
The generator instanciated by this call is new, but the character sets fed to it are always the same (the ones configured during
__init__()
). If a different character set is desired, a new instance ofPasswordGenerator
is neccessary.The generator will be finalized by a
done()
call, and then used by callinggenerate()
on it.
-
Once the you have specified your password scheme, you will have access to the generator instance.
-
class
grampg.
Generator
(sets)[source]¶ The generator object.
A generator instance undergoes three phases during its existance: create it with the character sets to choose from, specify it by calling its methods finalizing in a call to
done()
, and generate passwords with it by calling itsgenerate()
method.Character sets should not be modified once the generator is instantiated. If other character sets are required, a new instance should be used.
During the specification, repeated calls to the same method (consecutively or otherwise) overrides previous calls, so it is not an error to call them more than once. Specification is over after a call
done()
succeds. Once done, the generator cannot be further spec’ed, and only calls togenerate()
are valid (although it is possible to calldone()
over and over again, it does not have effect).Any attempt to add new specs to a done generator will raise
PasswordGeneratorIsDone
.Note
Generator instances should be built by means of
PasswordGenerator
, and only thegenerate()
method should ever be directly called on instances of this class.-
generate
()[source]¶ Return one generated password based on the collected specs.
Can be called any number of times, each yielding a new, independant password.
Raises
PasswordSpecsNonValidatedError
if the generator is not done (thedone()
method has not yet been called). RaisesPasswordSpecError
if frame spec methods (length
,beginning_with
ending_with
) collide.
-
In case of errors during the specification, the following exceptions are used.
-
exception
grampg.
PasswordSpecsError
[source]¶ Root of grampg exceptions.
Itself used to signal errors during specification or validation of a generator.
-
exception
grampg.
PasswordSpecsNonValidatedError
[source]¶ Raised when
generate()
is called on a generator before a it is done.