params.py (3885:fd4067a5b903) params.py (3932:62e915bb6704)
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

--- 504 unchanged lines hidden (view full) ---

513 if self.value == NextEthernetAddr:
514 if hasattr(self, 'addr'):
515 return self.addr
516 else:
517 return "NextEthernetAddr (unresolved)"
518 else:
519 return self.value
520
1# Copyright (c) 2004-2006 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright

--- 504 unchanged lines hidden (view full) ---

513 if self.value == NextEthernetAddr:
514 if hasattr(self, 'addr'):
515 return self.addr
516 else:
517 return "NextEthernetAddr (unresolved)"
518 else:
519 return self.value
520
521time_formats = [ "%a %b %d %H:%M:%S %Z %Y",
522 "%a %b %d %H:%M:%S %Z %Y",
523 "%Y/%m/%d %H:%M:%S",
524 "%Y/%m/%d %H:%M",
525 "%Y/%m/%d",
526 "%m/%d/%Y %H:%M:%S",
527 "%m/%d/%Y %H:%M",
528 "%m/%d/%Y",
529 "%m/%d/%y %H:%M:%S",
530 "%m/%d/%y %H:%M",
531 "%m/%d/%y"]
532
533
521def parse_time(value):
534def parse_time(value):
522 strings = [ "%a %b %d %H:%M:%S %Z %Y",
523 "%a %b %d %H:%M:%S %Z %Y",
524 "%Y/%m/%d %H:%M:%S",
525 "%Y/%m/%d %H:%M",
526 "%Y/%m/%d",
527 "%m/%d/%Y %H:%M:%S",
528 "%m/%d/%Y %H:%M",
529 "%m/%d/%Y",
530 "%m/%d/%y %H:%M:%S",
531 "%m/%d/%y %H:%M",
532 "%m/%d/%y"]
535 from time import gmtime, strptime, struct_time, time
536 from datetime import datetime, date
533
537
534 for string in strings:
535 try:
536 return time.strptime(value, string)
537 except ValueError:
538 pass
538 if isinstance(value, struct_time):
539 return value
539
540
541 if isinstance(value, (int, long)):
542 return gmtime(value)
543
544 if isinstance(value, (datetime, date)):
545 return value.timetuple()
546
547 if isinstance(value, str):
548 if value in ('Now', 'Today'):
549 return time.gmtime(time.time())
550
551 for format in time_formats:
552 try:
553 return strptime(value, format)
554 except ValueError:
555 pass
556
540 raise ValueError, "Could not parse '%s' as a time" % value
541
542class Time(ParamValue):
543 cxx_type = 'time_t'
544 def __init__(self, value):
557 raise ValueError, "Could not parse '%s' as a time" % value
558
559class Time(ParamValue):
560 cxx_type = 'time_t'
561 def __init__(self, value):
545 if isinstance(value, time.struct_time):
546 self.value = time.mktime(value)
547 elif isinstance(value, int):
548 self.value = value
549 elif isinstance(value, str):
550 if value in ('Now', 'Today'):
551 self.value = time.time()
552 else:
553 self.value = time.mktime(parse_time(value))
554 elif isinstance(value, (datetime.datetime, datetime.date)):
555 self.value = time.mktime(value.timetuple())
556 else:
557 raise ValueError, "Could not parse '%s' as a time" % value
562 self.value = parse_time(value)
558
559 def __str__(self):
563
564 def __str__(self):
560 return str(int(self.value))
565 tm = self.value
566 return ' '.join([ str(tm[i]) for i in xrange(8)])
561
562 def ini_str(self):
567
568 def ini_str(self):
563 return str(int(self.value))
569 return str(self)
564
565# Enumerated types are a little more complex. The user specifies the
566# type as Enum(foo) where foo is either a list or dictionary of
567# alternatives (typically strings, but not necessarily so). (In the
568# long run, the integer value of the parameter will be the list index
569# or the corresponding dictionary value. For now, since we only check
570# that the alternative is valid and then spit it into a .ini file,
571# there's not much point in using the dictionary.)

--- 462 unchanged lines hidden ---
570
571# Enumerated types are a little more complex. The user specifies the
572# type as Enum(foo) where foo is either a list or dictionary of
573# alternatives (typically strings, but not necessarily so). (In the
574# long run, the integer value of the parameter will be the list index
575# or the corresponding dictionary value. For now, since we only check
576# that the alternative is valid and then spit it into a .ini file,
577# there's not much point in using the dictionary.)

--- 462 unchanged lines hidden ---