Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

from __future__ import absolute_import, division, print_function 

 

import os.path 

 

import yaml 

 

from appr.formats.appr.manifest import ManifestBase 

from appr.pack import authorized_files 

from appr.render_jsonnet import RenderJsonnet, yaml_to_jsonnet 

 

 

__all__ = ['ManifestJsonnet'] 

 

MANIFEST_FILES = ['manifest.jsonnet', 'manifest.yaml', 'manifest.yml', 'kpm-manifest.jsonnet'] 

 

 

class ManifestJsonnet(ManifestBase): 

 

def __init__(self, package=None, tla_codes=None): 

self.tla_codes = tla_codes 

if package is not None: 

self._load_from_package(package) 

else: 

self._load_from_path() 

 

super(ManifestJsonnet, self).__init__() 

 

def _isjsonnet(self, package): 

if "manifest.yaml" in package.files: 

return False 

elif "manifest.jsonnet" in package.files: 

return True 

else: 

raise RuntimeError("Unknown manifest format") 

 

def _load_from_package(self, package): 

if self._isjsonnet(package): 

self._load_jsonnet(package.manifest, package.files) 

else: 

self._load_yaml(package.manifest, package.files) 

 

def _load_from_path(self): 

for filepath in MANIFEST_FILES: 

if os.path.exists(filepath): 

mfile = filepath 

break 

_, ext = os.path.splitext(mfile) 

with open(mfile) as f: 

auth_files = authorized_files() 

files = dict(zip(auth_files, [None] * len(auth_files))) 

if ext == '.jsonnet': 

self._load_jsonnet(f.read(), files) 

else: 

self._load_yaml(f.read(), files) 

 

def _load_jsonnet(self, jsonnetstr, files): 

k = RenderJsonnet(files) 

r = k.render_jsonnet(jsonnetstr, self.tla_codes) 

self.update(r) 

 

def _load_yaml(self, yamlstr, files): 

try: 

jsonnetstr = yaml_to_jsonnet(yamlstr, self.tla_codes) 

files['manifest.jsonnet'] = jsonnetstr 

self._load_jsonnet(jsonnetstr, files) 

except yaml.YAMLError as exc: 

print('Error in configuration file:') 

if hasattr(exc, 'problem_mark'): 

mark = exc.problem_mark # pylint: disable=E1101 

print('Error position: (%s:%s)' % (mark.line + 1, mark.column + 1)) 

raise exc