def update(self, testing=False): update_list = self.get_update_info() version = self.current_version sc = SC() delim = get_delim() failed = [] success = [] # concatinate files if user is multiple updates behind files = self.condense_files(update_list['updates']) for file_ in files: try: # download file url_opener = URLOpener() text_file = url_opener.open(file_['url']) # get the full path to the file file_path = delim.join([root_dir()] + file_['path'].split('/')) norm_file_path = os.path.normpath(file_path) # open the file file_to_update = open(norm_file_path, 'w') file_to_update.write(text_file) file_to_update.close() if self.check_new_update(file_['version'], version): version = file_['version'] success += [file_] except Exception: failed += [file_] sc.dict['Server']['update']['software_version'] = version if testing is not True: sc.save_dict() return success, failed
def notify_admin(self, update_info=None, testing=False): # notify admin admin_notified = False admin_notified = self.send_update_notification(update_info=update_info) if admin_notified is True: # record admin Notification sc = SC() sc.dict['Server']['update']['admin_notified'] = True if testing is not True: sc.save_dict()
def update_configs(new): """ Add new configurations, but keep users' changes intact. This will have the wrong version number, which will have to be overwritten later """ sc = SC() new_dict = json.loads(new) # map old configs on top of new to retain user settings merge_dicts(new_dict, sc.dict) sc.dict = new_dict sc.save_dict()
def build_insp_html(shakemap, name=None, web=False, config=None): temp_manager = TemplateManager() if not config: config = temp_manager.get_configs('inspection', name=name) template = temp_manager.get_template('inspection', name=name) facility_shaking = shakemap.facility_shaking if len(facility_shaking) > 0: facility_shaking.sort(key=lambda x: x.weight, reverse=True) fac_details = { 'all': 0, 'gray': 0, 'green': 0, 'yellow': 0, 'orange': 0, 'red': 0 } for fs in facility_shaking: fac_details['all'] += 1 fac_details[fs.alert_level] += 1 return template.render(shakemap=shakemap, facility_shaking=facility_shaking, fac_details=fac_details, sc=SC(), config=config, web=web)
def build_new_event_html(events=None, notification=None, group=None, name=None, web=False, config=None): temp_manager = TemplateManager() if not config: if name is None and notification is not None: config = temp_manager.get_configs( 'new_event', name=notification.group.template) else: config = temp_manager.get_configs('new_event', name=name) if name is None and notification is not None: template = temp_manager.get_template( 'new_event', name=notification.group.template) else: template = temp_manager.get_template('new_event', name=name) return template.render(events=events, group=group, notification=notification, sc=SC(), config=config, web=web)
def __init__(self): sc = SC() self.json_url = sc.dict['Server']['update']['json_url'] self.current_version = sc.dict['Server']['update']['software_version'] self.current_update = sc.dict['Server']['update']['update_version'] self.admin_notified = sc.dict['Server']['update']['admin_notified'] self.sc_root_dir = root_dir()
def check_update(self, testing=False): ''' Check the list of updates to see if any of them require attention ''' sc = SC() self.current_version = sc.dict['Server']['update']['software_version'] update_list = self.get_update_info() update_required = False notify = False update_info = set() for update in update_list['updates']: if self.check_new_update(update['version'], self.current_version) is True: update_required = True update_info.add(update['info']) if self.check_new_update(update['version'], self.current_update) is True: # update current update version in sc.conf json sc = SC() sc.dict['Server']['update']['update_version'] = update[ 'version'] if testing is not True: sc.save_dict() notify = True return update_required, notify, update_info
def __init__(self): # get info from the config sc = SC() self.me = sc.smtp_username self.username = sc.smtp_username self.password = sc.smtp_password self.server_name = sc.smtp_server self.server_port = int(sc.smtp_port) self.security = sc.dict['SMTP']['security'] self.log = '' if sc.use_proxy is True: # try to wrap the smtplib library with the socks module if sc.proxy_username and sc.proxy_password: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS4', sc.proxy_server, sc.proxy_port, username=sc.proxy_username, password=sc.proxy_password) socks.wrap_module(smtplib) except: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS5', sc.proxy_server, sc.proxy_port, username=sc.proxy_username, password=sc.proxy_password) socks.wrap_module(smtplib) except: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS4', sc.proxy_server, sc.proxy_port) socks.wrap_module(smtplib) except: try: socks.set_default_proxy( 'socks.PROXY_TYPE_SOCKS5', sc.proxy_server, sc.proxy_port) socks.wrap_module(smtplib) except: self.log += 'Unable to access SMTP through proxy' else: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS4', sc.proxy_server, sc.proxy_port) socks.wrap_module(smtplib) except: try: socks.set_default_proxy('socks.PROXY_TYPE_SOCKS5', sc.proxy_server, sc.proxy_port) socks.wrap_module(smtplib) except: self.log += 'Unable to access SMTP through proxy'
def get_event_map(event): if not os.path.exists(event.directory_name): os.makedirs(event.directory_name) sc = SC() # download the google maps image url_opener = URLOpener() gmap = url_opener.open( "https://api.mapbox.com/styles/v1/mapbox/streets-v10/static/pin-s+F00(%s,%s)/%s,%s,5/200x200?access_token=%s" % (event.lon, event.lat, event.lon, event.lat, sc.map_key)) # and save it image_loc = os.path.join(event.directory_name, 'image.png') image = open(image_loc, 'wb') image.write(gmap) image.close()
def __init__(self, req_products=None, data_dir=''): sc = SC() self.req_products = req_products self.pref_products = [] self.server_address = '' self.json_feed_url = sc.geo_json_web self.ignore_nets = sc.ignore_nets.split(',') self.json_feed = '' self.earthquakes = {} self.data_dir = '' self.delim = '' self.log = '' self.query_period = 'day' if not self.req_products: self.req_products = sc.eq_req_products if not self.pref_products: self.pref_products = sc.dict['Services']['eq_pref_products'] if data_dir == '': self.get_data_path()
def open(url): """ Args: url (str): a string url that will be opened and read by urllib2 Returns: str: the string read from the webpage """ # create context to avoid certificate errors try: ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE except: ctx = None try: sc = SC() if sc.use_proxy is True: if sc.proxy_username and sc.proxy_password: proxy = urllib2.ProxyHandler({ 'http': "http://{0}:{1}@{2}:{3}".format( sc.proxy_username, sc.proxy_password, sc.proxy_server, sc.proxy_port), 'https': "http://{0}:{1}@{2}:{3}".format( sc.proxy_username, sc.proxy_password, sc.proxy_server, sc.proxy_port) }) auth = urllib2.HTTPBasicAuthHandler() opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler) if ctx is not None: url_obj = opener.open(url, timeout=60, context=ctx) else: url_obj = opener.open(url, timeout=60) url_read = url_obj.read() url_obj.close() return url_read else: proxy = urllib2.ProxyHandler({ 'http': 'http://{0}:{1}'.format(sc.proxy_server, sc.proxy_port), 'https': 'https://{0}:{1}'.format(sc.proxy_server, sc.proxy_port) }) opener = urllib2.build_opener(proxy) if ctx is not None: url_obj = opener.open(url, timeout=60, context=ctx) else: url_obj = opener.open(url, timeout=60) url_read = url_obj.read() url_obj.close() return url_read else: if ctx is not None: url_obj = urllib2.urlopen(url, timeout=60, context=ctx) else: url_obj = urllib2.urlopen(url, timeout=60) url_read = url_obj.read() url_obj.close() return url_read except Exception as e: raise Exception('URLOpener Error({}: {}, url: {})'.format( type(e), e, url))
def get_new_events(self, scenario=False): """ Checks the json feed for new earthquakes """ session = Session() sc = SC() self.read_json_feed() event_str = '' new_events = [] for eq_id in self.earthquakes.keys(): eq = self.earthquakes[eq_id] # ignore info from unfavorable networks and low mag eqs if (eq['properties']['net'] in self.ignore_nets or eq['properties']['mag'] < sc.new_eq_mag_cutoff): continue # get event id and all ids event = Event() event.all_event_ids = eq['properties']['ids'] if scenario is False: event.event_id = eq_id else: event.event_id = eq_id + '_scenario' event.all_event_ids = event.event_id # use id and all ids to determine if the event is new and # query the old event if necessary old_shakemaps = [] old_notifications = [] if event.is_new() is False: event.status = 'processed' ids = event.all_event_ids.strip(',').split(',') old_events = [(session.query(Event).filter( Event.event_id == each_id).first()) for each_id in ids] # remove older events for old_event in old_events: if old_event is not None: old_notifications += old_event.notifications old_shakemaps += old_event.shakemaps # if one of these old events hasn't had # notifications sent, this event should be sent if old_event.status == 'new': event.status = 'new' session.delete(old_event) else: event.status = 'new' # over ride new status if scenario if scenario is True: event.status = 'scenario' # Fill the rest of the event info event.directory_name = os.path.join(self.data_dir, event.event_id) event.title = self.earthquakes[eq_id]['properties']['title'] event.place = self.earthquakes[eq_id]['properties']['place'] event.time = self.earthquakes[eq_id]['properties']['time'] / 1000.0 event.magnitude = eq['properties']['mag'] event_coords = self.earthquakes[eq_id]['geometry']['coordinates'] event.lon = event_coords[0] event.lat = event_coords[1] event.depth = event_coords[2] # determine whether or not an event should be kept # based on group definitions keep_event = False groups = session.query(Group).all() if len(groups) > 0: for group in groups: if group.point_inside(event): keep_event = True else: keep_event = True if keep_event is False: continue if old_shakemaps: event.shakemaps = old_shakemaps if old_notifications: event.notifications = old_notifications session.add(event) session.commit() self.get_event_map(event) # add the event to the return list and add info to the # return string new_events += [event] event_str += 'Event: %s\n' % event.event_id Session.remove() # print event_str return new_events, event_str