anjana_sreekumar@infosys.com | 991c206 | 2020-01-08 11:42:57 +0530 | [diff] [blame^] | 1 | # |
| 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 | |
| 17 | import os |
| 18 | from os import listdir |
| 19 | from os.path import isfile, join |
| 20 | |
| 21 | # Globals |
| 22 | ttFileName = '' |
| 23 | outputDir = '' |
| 24 | outputFile = '' |
| 25 | outputFileKeyword = '' |
| 26 | outputFileExt = '' |
| 27 | outputFileName = '' |
| 28 | mode = '' |
| 29 | |
| 30 | def 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 | |
| 38 | def readFile(fileName): |
| 39 | if os.path.exists(fileName): |
| 40 | with open(fileName, 'r') as file: |
| 41 | return file.read() |
| 42 | return |
| 43 | |
| 44 | def 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 | |
| 53 | def 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 | |
| 61 | def 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 | |
| 70 | def 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 | |
| 80 | def 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 |