X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=juju%2Fclient%2Fcodegen.py;h=f8a792a7b6d121199c3f6780db241bccb7814ce9;hb=6a363710a84d62a6ad1268045f319611f356f453;hp=4fc99ebc24a37adfdd47217035a5fb93965ac83d;hpb=c2ca299be7f2663544d4d3ebd85ee19b5011b190;p=osm%2FN2VC.git diff --git a/juju/client/codegen.py b/juju/client/codegen.py index 4fc99eb..f8a792a 100644 --- a/juju/client/codegen.py +++ b/juju/client/codegen.py @@ -1,8 +1,14 @@ +from collections import defaultdict from io import StringIO from textwrap import indent class CodeWriter(StringIO): + """ + Blob of text that, when used in the context of facade.py, ends up + holding the source code for a Python class and associated methods. + + """ INDENT = " " CLASS = 0 @@ -17,3 +23,30 @@ class CodeWriter(StringIO): def __str__(self): return super(CodeWriter, self).getvalue() + + +class Capture(defaultdict): + """ + A collection of CodeWriter objects, together representing a Python + module. + + """ + + def __init__(self, default_factory=CodeWriter, *args, **kwargs): + super(Capture, self).__init__(default_factory, *args, **kwargs) + + def clear(self, name): + """ + Reset one of the keys in this class, if it exists. + + This is necessary, because we don't worry about de-duplicating + the schemas for each version of juju up front, and this gives + us a way to sort of de-duplicate on the fly, by resetting a + specific CodeWriter instance before we start to write a class + into it. + + """ + try: + del self[name] + except KeyError: + pass