blob: 49e49cc8722942e94ad92b2c472a9fcd25239a00 [file] [log] [blame]
anjana_sreekumar@infosys.com991c2062020-01-08 11:42:57 +05301#
2# Copyright (c) 2019, Infosys Ltd.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import os
18from os import listdir
19from os.path import isfile, join
20
21# Globals
22ttFileName = ''
23outputDir = ''
24outputFile = ''
25outputFileKeyword = ''
26outputFileExt = ''
27outputFileName = ''
28mode = ''
29
30def WriteFile(dirName, fileName, content, fileMode):
31 filePath = join(dirName, fileName)
32 accessMode = 'w+'
33 if (fileMode == 'append') :
34 accessMode = 'a+'
35 with open(filePath, accessMode) as File:
36 File.write(content)
37
38def readFile(fileName):
39 if os.path.exists(fileName):
40 with open(fileName, 'r') as file:
41 return file.read()
42 return
43
44def searchWordInDir(path, word):
45 files = [f for f in listdir(path) if isfile(join(path, f))]
46 for i in files:
47 fileContent = open(join(path, i)).read()
48 if word in fileContent:
49 return True
50 return False
51
52
53def searchWordInFile(path, fileName, word):
54 if os.path.exists(join(path, fileName)):
55 with open(join(path, fileName)) as File:
56 fileContent = File.read()
57 if word in fileContent:
58 return True
59 return False
60
61def isFileEmpty(path, fileName):
62 fullPath = join(path, fileName)
63 if os.path.exists(fullPath):
64 if os.stat(fullPath).st_size > 0:
65 return 0
66 else :
67 return 1
68 return 1
69
70def getFileName(str):
71 if (str.find("_") != -1):
72 str = str.lower()
73 tokens = [x.capitalize() for x in str.split("_")]
74 fileName = ''
75 fileName = fileName.join(tokens)
76 return (fileName[0].lower() + fileName[1:])
77
78 return (str[0].lower() + str[1:])
79
80def get_key_values(obj, key):
81 arr = []
82
83 def extract(obj, arr, key):
84 if isinstance(obj, dict):
85 for k,v in obj.items():
86 if k == key:
87 arr.append(v)
88 elif isinstance(v, (dict, list)):
89 extract(v, arr, key)
90 elif isinstance(obj,list):
91 for item in obj:
92 extract(item, arr, key)
93 return arr
94
95 results = extract (obj, arr, key)
96 return results