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

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

# Copyright (C) 2015 Chintalagiri Shashank 

# 

# This file is part of Tendril. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU Affero General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU Affero General Public License for more details. 

# 

# You should have received a copy of the GNU Affero General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

""" 

Core Dox Wallet Module (:mod:`tendril.dox.wallet`) 

================================================== 

 

This module provides a simple wallet for 'standard' documents. 

These documents would typically be stored within the 

:data:`tendril.utils.config.DOCUMENT_WALLET_ROOT`, and defined by the 

:data:`tendril.utils.config.DOCUMENT_WALLET` dictionary. Each `key, value` 

pair in the dictionary is a `key, filename` pair. 

 

This module is intended to be used by other ``tendril.dox`` modules to 

provide document sets that include standard documents such as scanned IDs, 

certificates, so on. 

 

.. rubric:: Functions 

 

.. autosummary:: 

 

    get_document_path 

    is_in_wallet 

 

""" 

 

from fs.opener import fsopendir 

from fs.utils import copyfile 

from fs import path 

from fs.errors import NoSysPathError 

 

from tendril.utils.config import DOCUMENT_WALLET 

from tendril.utils.config import DOCUMENT_WALLET_ROOT 

 

from tendril.utils.fsutils import temp_fs 

 

 

wallet_fs = fsopendir(DOCUMENT_WALLET_ROOT) 

wallet_temp_fs = temp_fs.makeopendir('wallet') 

 

 

def get_document_path(key): 

    """ 

    Returns the absolute path to the document in the wallet referred to 

    by the ``key``. 

 

    If the wallet fs root is not an OS filesystem, then it copies the 

    wallet document into the temporary folder and returns the path to it. 

 

    :param key: Key of the document you want. 

    :return: The absolute path to the document. 

    """ 

    if key in DOCUMENT_WALLET.keys(): 

        try: 

            return wallet_fs.getsyspath(DOCUMENT_WALLET[key]) 

        except NoSysPathError: 

            if not wallet_temp_fs.exists(DOCUMENT_WALLET[key]): 

                copyfile(wallet_fs, DOCUMENT_WALLET[key], 

                         wallet_temp_fs, DOCUMENT_WALLET[key]) 

            return wallet_temp_fs.getsyspath(DOCUMENT_WALLET[key]) 

    else: 

        raise ValueError 

 

 

def is_in_wallet(fpath): 

    """ 

    Checks whether a specified path points to a document in the wallet. 

    This function only checks whether the path is within the 

    :data:`tendril.utils.config.DOCUMENT_WALLET_ROOT`, and not whether the 

    document is defined in the :data:`tendril.utils.config.DOCUMENT_WALLET` 

    dictionary. 

 

    The intended use of this function is alongside PDF merge operations 

    after which the sources are deleted. Documents in the wallet should not 

    be deleted. Code performing the deletion can use this function to check 

    whether the document is in the wallet. 

 

    :param fpath: The path to the file. 

    :return: True if the document is in the wallet, False if not. 

 

    """ 

    if DOCUMENT_WALLET_ROOT in path.split(fpath)[0]: 

        return True