Skip to content

Commit

Permalink
Issue 216 (CiscoDevNet#261)
Browse files Browse the repository at this point in the history
* Added weak "internal use" indicator

* Only print import identity statement used (CiscoDevNet#216)

* fix if statement
  • Loading branch information
Xiaoqin Zhu authored and psykokwak4 committed Oct 31, 2016
1 parent 6b0c0f4 commit 95d2926
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 37 deletions.
8 changes: 4 additions & 4 deletions ydkgen/printer/language_bindings_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ def _get_identity_subclasses_map(self):
identity_tuples = self._get_identity_tuples()
for (child_identity, base_identities) in identity_tuples:
for base_identity in base_identities:
if id(base_identity) in identity_subclasses:
existing_child_identities = identity_subclasses[id(base_identity)]
if base_identity in identity_subclasses:
existing_child_identities = identity_subclasses[base_identity]
existing_child_identities.append(child_identity)
identity_subclasses[id(base_identity)] = existing_child_identities
identity_subclasses[base_identity] = existing_child_identities
else:
identity_subclasses[id(base_identity)] = [child_identity]
identity_subclasses[base_identity] = [child_identity]
return identity_subclasses

def _get_identity_tuples(self):
Expand Down
36 changes: 18 additions & 18 deletions ydkgen/printer/python/init_file_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def print_nmsp_augment_finder_init(self, _, is_meta=False):
__path__ = pkgutil.extend_path(__path__, __name__)
def get_augments_bundles():
def _get_augments_bundles():
\"\"\"Collect augmentation contributors.\"\"\"
augment_contributors = []
for (_, name, ispkg) in pkgutil.iter_modules(models.__path__):
Expand All @@ -63,12 +63,12 @@ def get_augments_bundles():
return augment_contributors
def get_pkg_mod_names(path):
def _get_pkg_mod_names(path):
\"\"\"Return a set for Python module name under path.\"\"\"
return {{name for _, name, _ in pkgutil.iter_modules(path)}}
def patch(base_lines, patch_lines):
def _patch(base_lines, patch_lines):
\"\"\"Add patch to base, patched fields in base are wrapped with @@@.\"\"\"
patched_lines = []
i, j, docstring = 0, 0, False
Expand Down Expand Up @@ -117,38 +117,38 @@ def patch(base_lines, patch_lines):
return patched_lines
def patch_files(base, paths):
def _patch_files(base, paths):
\"\"\"Patch files in paths to base.\"\"\"
with open(base) as base_file:
base_lines = base_file.readlines()
for path in paths:
with open(path) as patch_file:
patch_lines = patch_file.readlines()
try:
base_lines = patch(base_lines, patch_lines)
base_lines = _patch(base_lines, patch_lines)
except IndexError:
# inconsistent modules.
pass
return ''.join([l for l in base_lines if l != '@@@'])
def get_patch_paths(path):
def _get_patch_paths(path):
\"\"\"Return patch module path.\"\"\"
base_dir = os.path.dirname(path)
base_file = os.path.basename(path).rstrip('c')
patch_mod_path = os.path.join(base_dir, base_file)
return patch_mod_path
def get_base_module_path(mod_name):
def _get_base_module_path(mod_name):
\"\"\"Return base module path.\"\"\"
base_dir = os.path.dirname(__file__)
base_mod = os.path.join(base_dir, '%s.py' % mod_name)
return base_mod
def get_patched_path(mod_name):
def _get_patched_path(mod_name):
\"\"\"Return patched module's path.\"\"\"
patched_dir = os.path.join(os.path.dirname(__file__), '_aug_patch')
if not os.path.exists(patched_dir):
Expand All @@ -157,17 +157,17 @@ def get_patched_path(mod_name):
return patched_mod
def merge_augmented_modules(mod_name, mod_aug_lst, prefix):
def _merge_augmented_modules(mod_name, mod_aug_lst, prefix):
\"\"\"Patch augmented module to base module.\"\"\"
patch_paths = []
for mod_fqn in mod_aug_lst:
mod = importlib.import_module(mod_fqn)
patch_path = get_patch_paths(mod.__file__)
patch_path = _get_patch_paths(mod.__file__)
patch_paths.append(patch_path)
base_mod = get_base_module_path(mod_name)
patched_mod = patch_files(base_mod, patch_paths)
patched_mod_path = get_patched_path(mod_name)
base_mod = _get_base_module_path(mod_name)
patched_mod = _patch_files(base_mod, patch_paths)
patched_mod_path = _get_patched_path(mod_name)
with open(patched_mod_path, 'w') as patched_file:
patched_file.write(patched_mod)
Expand All @@ -176,14 +176,14 @@ def merge_augmented_modules(mod_name, mod_aug_lst, prefix):
return mod
def shadow_module(augment_contributors):
def _shadow_module(augment_contributors):
\"\"\"Shadow existing module.\"\"\"
to_shadowed = {{}}
self_mod_names = get_pkg_mod_names(__path__)
self_mod_names = _get_pkg_mod_names(__path__)
for module in augment_contributors:
other_mod_names = get_pkg_mod_names(module.__path__)
other_mod_names = _get_pkg_mod_names(module.__path__)
to_update = other_mod_names & self_mod_names
for mod_name in list(to_update):
Expand All @@ -198,14 +198,14 @@ def shadow_module(augment_contributors):
for mod_name in to_shadowed:
mod_aug_lst = to_shadowed[mod_name]
mod_to_replace = '.'.join([__name__, mod_name])
mod = merge_augmented_modules(mod_name, mod_aug_lst, __name__)
mod = _merge_augmented_modules(mod_name, mod_aug_lst, __name__)
sys.modules[mod_to_replace] = mod
globals()[mod_to_replace] = mod
# set parent
parent_mod = sys.modules[__name__]
setattr(parent_mod, mod_name, mod)
shadow_module(get_augments_bundles())
_shadow_module(_get_augments_bundles())
""".format(relative_dot, meta_path))
32 changes: 20 additions & 12 deletions ydkgen/printer/python/module_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@

class ModulePrinter(FilePrinter):

def __init__(self, ctx, sort_clazz):
def __init__(self, ctx, extra_args):
super(ModulePrinter, self).__init__(ctx)
self.sort_clazz = sort_clazz
self.sort_clazz = extra_args.get('sort_clazz', False)
self.identity_subclasses = extra_args.get('identity_subclasses', {})

def print_header(self, package):
self._print_module_description(package)
Expand All @@ -60,22 +61,29 @@ def _print_module_description(self, package):

def _print_imports(self, package):
self._print_static_imports()
imports_to_print = []
imports_to_print = set()

for imported_type in package.imported_types():
import_stmt = 'from %s import %s' % (
imported_type.get_py_mod_name(), imported_type.qn().split('.')[0])
if import_stmt in imports_to_print:
continue
else:
imports_to_print.append(import_stmt)
if all((imported_type in self.identity_subclasses,
self._is_derived_identity(package, imported_type))):
imported_stmt = 'from %s import %s' % (
imported_type.get_py_mod_name(),
imported_type.qn().split('.')[0])
imports_to_print.add(imported_stmt)

imports_to_print = sorted(imports_to_print)
for import_to_print in imports_to_print:
self.ctx.writeln('%s' % import_to_print)
for imported_stmt in sorted(imports_to_print):
self.ctx.writeln(imported_stmt)

self.ctx.bline()

def _is_derived_identity(self, package, identity):
for ne in package.owned_elements:
if isinstance(ne, Class) and ne.is_identity():
for ext in ne.extends:
if ext == identity:
return True
return False

def _print_static_imports(self):
self.ctx.str('''
Expand Down
8 changes: 5 additions & 3 deletions ydkgen/printer/python/python_bindings_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ def print_python_module(self, package, index, path, size, sub):
self.print_init_file(path)

package.parent_pkg_name = sub
extra_args = {'sort_clazz': False,
'identity_subclasses': self.identity_subclasses}
self.print_file(get_python_module_file_name(path, package),
emit_module,
_EmitArgs(self.ypy_ctx, package, self.sort_clazz))
_EmitArgs(self.ypy_ctx, package, extra_args))

def print_meta_module(self, package, path):
self.print_init_file(path)
Expand Down Expand Up @@ -228,8 +230,8 @@ def emit_table_of_contents(ctx, packages, bundle_name):
DocPrinter(ctx, 'py').print_table_of_contents(packages, bundle_name)


def emit_module(ctx, package, sort_clazz):
ModulePrinter(ctx, sort_clazz).print_output(package)
def emit_module(ctx, package, extra_args):
ModulePrinter(ctx, extra_args).print_output(package)


def emit_test_module(ctx, package, identity_subclasses):
Expand Down

0 comments on commit 95d2926

Please sign in to comment.