def test_guess_local_datetime_invalidString(self): s = "abc" try: datetimeutil.guess_local_datetime(s) self.fail("%s should not be a valid date time", s) except ValueError: pass
def _aggressive_process(self, line): words = [] for word in line.split(' '): # We only parse date time strings, so at least # YYYYMMDDHHmmss 14 characters if len(word) >= 14: w = _non_timestamp_chars_regex.sub("", word) if len(w) >= 14: try: dt = datetimeutil.guess_local_datetime(w) # We want to attach a local datetime before trailing # newlines or other whitespaces for this word m = re.match(r"(.*)(\s+)", word) if m: word = m.group(1) trailing_spaces = m.group(2) else: trailing_spaces = "" word = "%s(%s)%s" % ( word, dt.strftime(self.args.datetime_format), trailing_spaces, ) except ValueError as ex: pass words.append(word) return " ".join(words)
def test_guess_local_datetime_isoFormatWithZ(self): s = "2006-01-02T15:04:05Z" dt = datetimeutil.guess_local_datetime(s) self.assertEqual( "2006-01-02 07:04:05", dt.strftime(_LOCAL_TIMESTAMP_FORMAT), )
def test_guess_local_datetime_isoDateFormat(self): s = "2006-01-02" dt = datetimeutil.guess_local_datetime(s) self.assertEqual( "2006-01-02 00:00:00", dt.strftime(_LOCAL_TIMESTAMP_FORMAT), )
def test_guess_local_datetime_isoFormat(self): s = "2006-01-02T15:04:05-0700" dt = datetimeutil.guess_local_datetime(s) self.assertEqual( _LOCAL_TIMESTAMP_FOR_GO_REFERENCE_TIME, dt.strftime(_LOCAL_TIMESTAMP_FORMAT), )
def run(self): for n in self.args.numbers: try: n = int(n) dt, unit = datetimeutil.number_to_local_datetime(n) print( "%s: %s in %s" % ( n, dt.strftime(_datetime_format), unit, ) ) except ValueError: try: print( "%s: %s" % ( n, datetimeutil.guess_local_datetime(n).strftime( _datetime_format ), ) ) except: print("%s: invalid datetime" % n)
def run_as_subscriber(self): _log.info("I am a subscriber") client = create_pubsub_client() project_id = self._get_project_id() if not project_id: _log.error("Cannot find project_id, please specify --project-id") return _log.info("Project id: %s", project_id) full_topic_name = self._get_topic_name(project_id) _log.info("Full topic name: %s", full_topic_name) full_subscription_name = self._get_subscription_name(project_id) _log.info("Full subscription name: %s", full_subscription_name) # Create a POST body for the Pub/Sub request subscription_body = { # The name of the topic from which this subscription receives messages 'topic': full_topic_name, # Only needed if you are using push delivery # 'pushConfig': { # 'pushEndpoint': push_endpoint # } } subscription = None _log.info("Find or create subscription %s...", full_subscription_name) try: subscription = client.projects().subscriptions().get( subscription=full_subscription_name ).execute() except googleapiclient.errors.HttpError as error: # error probably means the subscription does not exist _log.debug("Error: %s", error) if not subscription: subscription = client.projects().subscriptions().create( name=full_subscription_name, body=subscription_body, ).execute() _log.info("Created: %s", subscription.get('name')) _log.info("Subscription resource: %s", subscription) # Create a POST body for the Pub/Sub request body = { # Setting ReturnImmediately to false instructs the API to wait # to collect the message up to the size of MaxEvents, or until # the timeout. 'returnImmediately': False, 'maxMessages': 1, } while True: _log.info("Pull one message...") try: resp = client.projects().subscriptions().pull( subscription=full_subscription_name, body=body ).execute() except KeyboardInterrupt: break _log.info("Response: %s", resp) received_messages = resp.get('receivedMessages') if received_messages is not None: ack_ids = [] for received_message in received_messages: pubsub_message = received_message.get('message') if pubsub_message: # Process messages _log.info( "Message decoded: %s", base64.b64decode(str(pubsub_message.get('data'))), ) _log.info( "Published local time: %s", datetimeutil.guess_local_datetime( pubsub_message.get('publishTime') ).strftime("%Y-%m-%d %H:%M:%S"), ) # Get the message's ack ID ack_ids.append(received_message.get('ackId')) # Create a POST body for the acknowledge request ack_body = {'ackIds': ack_ids} # Acknowledge the message. _log.info("Ack the message") client.projects().subscriptions().acknowledge( subscription=full_subscription_name, body=ack_body).execute()