# Coverage for api/module.py : 58%

## 38 statements

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 import copy

2 import json

3 import os

4

5 from flask import Blueprint

6 from flask import redirect

7 from flask import render_template

8 from flask import url_for

9

10 from shopyo.api.assets import get_static

11

12

13 class ModuleHelp:

14 def __init__(self, dunderfile, dundername):

15 self.dirpath = os.path.dirname(os.path.abspath(dunderfile))

16 self.info = {}

17 self._context = {}

18

19 with open(self.dirpath + "/info.json") as f:

20 self.info = json.load(f)

21

22 self.blueprint_str = "{}_blueprint".format(self.info["module_name"])

23 self.blueprint = Blueprint(

24 "{}".format(self.info["module_name"]),

25 dundername,

26 template_folder="templates",

27 url_prefix=self.info["url_prefix"],

28 )

29

30 self._context.update({"info": self.info})

31

32 def render(self, filename, **kwargs):

33 """

34 renders file.html found in module/templates/module/file.html

35 """

36 return render_template(

37 "{}/{}".format(self.info["module_name"], filename), **kwargs

38 )

39

40 def redirect_url(self, url, **kwargs):

41 return redirect(url_for(url, **kwargs))

42

43 def context(self):

44 return copy.deepcopy(self._context)

45

46 def method(self, methodname):

47 return "{}.{}".format(self.info["module_name"], methodname)

48

49 def get_self_static(self, filename):

50 module_parent = os.path.dirname(self.dirpath)

51 module_folder = self.dirpath

52

53 module_parent = os.path.normpath(module_parent)

54 module_parent = os.path.basename(module_parent)

55

56 module_folder = os.path.normpath(module_folder)

57 module_folder = os.path.basename(module_folder)

58

59 print(module_parent, module_parent)

60 if module_parent.startswith("box__"):

61 boxormodule = f"{module_parent}/{module_folder}"

62 else:

63 boxormodule = module_folder

64 return get_static(boxormodule=boxormodule, filename=filename)