@@ -397,45 +397,54 @@ def __setattr__(self, name, value):
397
397
self [name ] = value
398
398
399
399
def copy (self ):
400
- return AttributeDict (super ( AttributeDict , self ). copy ())
400
+ return AttributeDict (self . items ())
401
401
402
- class Color (AttributeDict ):
402
+ class Color (object ):
403
403
"""
404
- Stores the string and rgba representation of a color. Individual components
405
- of the color can be retrieved with the dot operator::
404
+ Stores a color name and rgba representation of a color. Individual
405
+ color components can be retrieved with the dot operator::
406
406
407
407
color = Color(r=255, g=0, b=0, a=75)
408
- tuple(color.r,color.g, color.b, color.a) = color.rgba
408
+ tuple(color.r,color.g, color.b, color.a) == color.rgba
409
409
410
- Because Color is an implementation of an AttributeDict you must specify
411
- each component by name in the constructor.
410
+ You can also create a color by name.
412
411
413
- Can print the string representation with str(Color)
414
- """
412
+ color = Color('Red')
415
413
416
- @property
417
- def rgba (self ):
418
- """Tuple containing the (r,g,b,a) representation of the color"""
419
- if 'r' not in self or 'g' not in self or 'b' not in self :
420
- hexstr = self .hex
414
+ Only standard Starcraft colors are supported. ValueErrors will be thrown
415
+ on invalid names or hex values.
416
+ """
417
+ def __init__ (self , name = None , r = 0 , g = 0 , b = 0 , a = 255 ):
418
+ if name :
419
+ if name not in COLOR_CODES_INV :
420
+ raise ValueError ("Invalid color name: " + name )
421
+ hexstr = COLOR_CODES_INV [name ]
421
422
self .r = int (hexstr [0 :2 ],16 )
422
423
self .g = int (hexstr [2 :4 ],16 )
423
424
self .b = int (hexstr [4 :6 ],16 )
424
425
self .a = 255
426
+ self .name = name
427
+ else :
428
+ self .r = r
429
+ self .g = g
430
+ self .b = b
431
+ self .a = a
432
+ if self .hex in COLOR_CODES :
433
+ self .name = COLOR_CODES [self .hex ]
434
+ else :
435
+ raise ValueError ("Invalid color hex code: " + self .hex )
436
+
437
+ @property
438
+ def rgba (self ):
439
+ """ Returns a tuple containing the color's (r,g,b,a) """
425
440
return (self .r ,self .g ,self .b ,self .a )
426
441
427
442
@property
428
443
def hex (self ):
429
444
"""The hexadecimal representation of the color"""
430
- if 'name' in self :
431
- return COLOR_CODES_INV .get (self .name )
432
- else :
433
- return "{0.r:02X}{0.g:02X}{0.b:02X}" .format (self )
434
-
445
+ return "{0.r:02X}{0.g:02X}{0.b:02X}" .format (self )
435
446
436
447
def __str__ (self ):
437
- if not hasattr (self ,'name' ):
438
- self .name = COLOR_CODES [self .hex ]
439
448
return self .name
440
449
441
450
def open_archive (replay_file ):
@@ -514,20 +523,16 @@ def merged_dict(a, b):
514
523
c .update (b )
515
524
return c
516
525
517
- def extension_filter (filename , extension ):
518
- name , ext = os .path .splitext (filename )
519
- return ext .lower ()[1 :] == extension .lower ()
520
-
521
526
def get_files (path , exclude = list (), depth = - 1 , followlinks = False , extension = None , ** extras ):
522
527
# os.walk and os.path.isfile fail silently. We want to be loud!
523
528
if not os .path .exists (path ):
524
529
raise ValueError ("Location `{0}` does not exist" .format (path ))
525
530
526
531
# If an extension is supplied, use it to do a type check
527
- if extension == None :
528
- type_check = lambda n : True
532
+ if extension :
533
+ type_check = lambda path : os . path . splitext ( path )[ 1 ][ 1 :]. lower () == extension . lower ()
529
534
else :
530
- type_check = functools . partial ( extension_filter , extension = extension )
535
+ type_check = lambda n : True
531
536
532
537
# os.walk can't handle file paths, only directories
533
538
if os .path .isfile (path ):
@@ -551,24 +556,23 @@ def get_files(path, exclude=list(), depth=-1, followlinks=False, extension=None,
551
556
552
557
553
558
class Length (timedelta ):
554
- """
555
- Extends the builtin timedelta class. See python docs for more info on
559
+ """ Extends the builtin timedelta class. See python docs for more info on
556
560
what capabilities this gives you.
557
561
"""
558
562
559
563
@property
560
564
def hours (self ):
561
- """The number of hours in represented."""
565
+ """ The number of hours in represented. """
562
566
return self .seconds / 3600
563
567
564
568
@property
565
569
def mins (self ):
566
- """The number of minutes in excess of the hours."""
570
+ """ The number of minutes in excess of the hours. """
567
571
return (self .seconds / 60 )% 60
568
572
569
573
@property
570
574
def secs (self ):
571
- """The number of seconds in excess of the minutes."""
575
+ """ The number of seconds in excess of the minutes. """
572
576
return self .seconds % 60
573
577
574
578
def __str__ (self ):
0 commit comments