def do_save(self, arg): ''' Store current login credential into file setting, e.g. save settings.json ''' url = self.BROWSER_SESSION.LOGIN_iNFO.URL em = self.BROWSER_SESSION.LOGIN_iNFO.USERNAME pwd = self.BROWSER_SESSION.LOGIN_iNFO.PASSWORD if url == '' or em == '' or em == '': print( '\nCannot read your valid credential, No login session in current state.\nThis action is not available.' ) return self rootElement = 'credentials' b = [] a = rJ(arg).read() tempCredential = {'username': em, 'password': pwd, 'baseurl': url} print(a) if hasattr(a, "__getitem__"): a = a[rootElement] a.append(tempCredential) b = a else: b.append(tempCredential) b = {rootElement: b} rJ(arg).createFromObject(b).save() return self
def do_del(self, arg): ''' Delete the element on current saved filter ''' fls = self.FILTER self.tempFilterJson = self.FILTER print(''' 0 show 1 remove x exit ''') a = '' while a != 'x': for k in fls: print('%s : %s' % (k, fls[k])) a = input(self.chooselabel) if a == '0': print(self.tempFilterJson) if a == '1': del self.tempFilterJson[k] if a == 'x': break if fls == self.tempFilterJson: return if input('type \'s\' to save: ') == 's': rJ(self.filterFileName).createFromObject( self.tempFilterJson).save() return self
def do_4(self, arg): ''' Change meter state to `off`/`on`/`auto` for all meter in current filtered meters related command: `f`, `g` ''' if arg == '': state = self.myInput( 'Input new meter state with `off`/`on`/`auto` or key else to abort: ', 'auto') if state in ['off', 'on', 'auto']: self.currentWebsiteSession.METER.changeAllMeterState(state) else: metersLocal = rJ(arg).read() a = 1 z = len(metersLocal) for m in metersLocal: print('\n\nMeter no %s from %s ; SN = %s ; url = %s' % (a, z, m['meter_serial'], self.currentWebsiteSession.LOGIN_iNFO.URL)) a += 1 intState = m['meter_state'] state = 'off' if intState == 0 else 'on' if intState == 1 else 'auto' self.currentWebsiteSession.METER.changeAllMeterState( m['meter_serial'], state)
def do_s(self, name=''): ''' change current setting file to other exist file, e.g. s settings.json ''' defName = 'settings.json' name = defName if name == '' or name is None else name if not Path(name).exists(): name = defName a = rJ(name).read() self.settingJson = a['credentials']
def do_fn(self, name=''): ''' change current filter file to other exist file, e.g. fn filters.json ''' defName = 'filters.json' name = defName if name == '' or name is None else name if not Path(name).exists(): name = defName self.filterFileName = name self.FILTER = rJ(self.filterFileName).read() return self
def saveJson(self, url): url = str(url) if url == '' or len(url.split('/')) <= 2 or self.meterJsonArray == '': return outputFileName = url.split('/')[2] inp = input("type `sv` to save to %s -- json and csv file: " % (outputFileName)) if inp == 'sv': header = [] for key in self.meterJsonArray[0]: header.append(key) with open(outputFileName + '.csv', 'w', newline='') as mFile: out = csv.DictWriter(mFile, dialect='excel', fieldnames=header, delimiter=';', quoting=csv.QUOTE_MINIMAL) out.writeheader() out.writerows(self.meterJsonArray) rJ(outputFileName + '.json').createFromObject( self.meterJsonArray).save()
def do_7(self, arg): ''' Push new meter information on temporary file (end result/output) of `c` command, e.g: c "d:/myDir/temp_new_meter_info-sparkcloud.json" ''' self.currentWebsiteSession.METER.getMeterInfo( filterJson=self.currentFilter, useFilter=True) metersLocal = rJ(arg).read() a = 1 z = len(metersLocal) for m in metersLocal: print('\n\nMeter no %s from %s ; SN = %s ; url = %s' % (a, z, m['meter_serial'], self.currentWebsiteSession.LOGIN_iNFO.URL)) a += 1 self.editMeter(m, False)
def do_set(self, args=''): ''' Create a custom filter with these format (modified json format): "name_of_element":"operator":int, "name_of_element":"operator":"str", "name_of_element":"operator":bool e.g "meter_credit":"<=":23, "meter_serial":"%-":"SM-0076" ''' try: self.tempFilterJson = self.filterFromStr(args) print(''' 0 show 1 add more filter 2 merge to current filter 3 save as current filter h show help x exit ''') a = '' while a not in ['2', '3', 'x']: if a == '0': print(self.tempFilterJson) if a == '1': print(""" References: "address_city": "Sentani", "address_coords": "", "address_state": "Papua", "address_street1": "Jl. Pos 7", "address_street2": "", "customer_code": "Aryanto", "customer_name": "Aryanto", "customer_phone_number": null, "customer_phone_number_verified": false, "ground_name": "EVI-DevSite-Sentani", "ground_serial": "bTSq1_jS0dOGrnA_xyK9", "meter_active": true, "meter_credit_value": 103000, "meter_debt_value": 0, "meter_is_running_plan": false, "meter_plan_value": 0, "meter_serial": "SM5R-02-0000293A", "meter_state": 1, "meter_tags": "", "tariff_name": "Sumba - BUMDes", "tariff_plan_enabled": true Example: "address_city":"%!":"Sentani" """) filterReAdd = input('Input filter: ') filterReAdd = self.filterFromStr(filterReAdd) if hasattr(filterReAdd, '__getitem__'): for efra in filterReAdd: self.tempFilterJson[efra] = filterReAdd[efra] if a == 'h': print(self.intro) self.do_help('set') a = input(self.chooselabel) else: if a == '2': currentFilter = self.FILTER for fj in self.tempFilterJson: currentFilter[fj] = self.tempFilterJson[fj] rJ(self.filterFileName).createFromObject( currentFilter).save() if a == '3': while True: filename = input('Enter valid file name: ') if Path(filename).exists(): y = input( 'File with \'%s\' name exist, type `yes` to replace:' % (filename)) if y == 'yes': break elif '.json' in filename: break rJ(filename).createFromObject(self.tempFilterJson).save() except Exception as a: print(a) return self