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

# 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/>. 

""" 

EntityHub Maps Module documentation (:mod:`entityhub.maps`) 

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

""" 

 

import csv 

 

 

class MapFile(object): 

    def __init__(self, mappath): 

        self._map = {} 

        self._umap = {} 

        self._strategy = {} 

        self._len = 0 

        with open(mappath) as f: 

            rdr = csv.reader(f) 

            for row in rdr: 

                if row[0] == "Canonical": 

                    continue 

                self._strategy[row[0]] = row[1] 

                vals = row[2:] 

                self._map[row[0]] = [] 

                self._umap[row[0]] = [] 

                for elem in vals: 

                    assert isinstance(elem, str) 

                    self._len += 1 

                    if elem.startswith('@AG@'): 

                        elem = elem[4:] 

                        self._map[row[0]].append(elem) 

                    else: 

                        self._umap[row[0]].append(elem) 

 

    def get_idents(self): 

        for key in sorted(self._map.keys()): 

            if len(self._map[key]) or len(self._umap[key]): 

                yield key 

 

    def get_upartnos(self, canonical): 

        return self._umap[canonical] 

 

    def get_apartnos(self, canonical): 

        return self._map[canonical] 

 

    def get_all_partnos(self, canonical): 

        return self._umap[canonical] + self._map[canonical] 

 

    def get_partnos(self, canonical): 

        if canonical not in self.get_idents(): 

            return [] 

        if len(self._umap[canonical]) > 0: 

            return self._umap[canonical] 

        elif len(self._map[canonical]) > 0: 

            return self._map[canonical] 

        else: 

            return [] 

 

    def get_strategy(self, canonical): 

        if canonical not in self.get_idents(): 

            return 'NOTRECOG' 

        return self._strategy[canonical] 

 

    def get_canonical(self, partno): 

        if len(self._umap) > 0: 

            for (k, v) in self._umap.iteritems(): 

                if partno in v: 

                    return k 

        if len(self._map) > 0: 

            for (k, v) in self._map.iteritems(): 

                if partno in v: 

                    return k 

        return None 

 

    def get_user_map(self): 

        return self._umap 

 

    def length(self): 

        return self._len