imip-agent

Change of imiptools/handlers/scheduling/quota.py

1040:8f2b373a311b
imiptools/handlers/scheduling/quota.py
     1.1 --- a/imiptools/handlers/scheduling/quota.py	Mon Feb 08 00:14:53 2016 +0100
     1.2 +++ b/imiptools/handlers/scheduling/quota.py	Mon Feb 08 00:47:00 2016 +0100
     1.3 @@ -76,15 +76,10 @@
     1.4      # Obtain the journal entries and limits.
     1.5  
     1.6      journal = handler.get_journal()
     1.7 -    journal.acquire_lock(quota)
     1.8 +    entries = journal.get_entries(quota, group)
     1.9  
    1.10 -    try:
    1.11 -        entries = journal.get_entries(quota, group)
    1.12 -        if _add_to_entries(entries, handler.obj.get_uid(), handler.obj.get_recurrenceid(), format_duration(total)):
    1.13 -            journal.set_entries(quota, group, entries)
    1.14 -
    1.15 -    finally:
    1.16 -        journal.release_lock(quota)
    1.17 +    if _add_to_entries(entries, handler.obj.get_uid(), handler.obj.get_recurrenceid(), format_duration(total)):
    1.18 +        journal.set_entries(quota, group, entries)
    1.19  
    1.20  def remove_from_quota(handler, args):
    1.21  
    1.22 @@ -100,15 +95,10 @@
    1.23      # Obtain the journal entries and limits.
    1.24  
    1.25      journal = handler.get_journal()
    1.26 -    journal.acquire_lock(quota)
    1.27 +    entries = journal.get_entries(quota, group)
    1.28  
    1.29 -    try:
    1.30 -        entries = journal.get_entries(quota, group)
    1.31 -        if _remove_from_entries(entries, handler.obj.get_uid(), handler.obj.get_recurrenceid(), format_duration(total)):
    1.32 -            journal.set_entries(quota, group, entries)
    1.33 -
    1.34 -    finally:
    1.35 -        journal.release_lock(quota)
    1.36 +    if _remove_from_entries(entries, handler.obj.get_uid(), handler.obj.get_recurrenceid(), format_duration(total)):
    1.37 +        journal.set_entries(quota, group, entries)
    1.38  
    1.39  def _get_quota_and_group(handler, args):
    1.40  
    1.41 @@ -254,15 +244,9 @@
    1.42      quota, organiser = _get_quota_and_identity(handler, args)
    1.43  
    1.44      journal = handler.get_journal()
    1.45 -    journal.acquire_lock(quota)
    1.46 -
    1.47 -    try:
    1.48 -        freebusy = journal.get_freebusy(quota, organiser)
    1.49 -        handler.update_freebusy(freebusy, organiser, True)
    1.50 -        journal.set_freebusy(quota, organiser, freebusy)
    1.51 -
    1.52 -    finally:
    1.53 -        journal.release_lock(quota)
    1.54 +    freebusy = journal.get_freebusy(quota, organiser)
    1.55 +    handler.update_freebusy(freebusy, organiser, True)
    1.56 +    journal.set_freebusy(quota, organiser, freebusy)
    1.57  
    1.58  def remove_from_quota_freebusy(handler, args):
    1.59  
    1.60 @@ -274,15 +258,9 @@
    1.61      quota, organiser = _get_quota_and_identity(handler, args)
    1.62  
    1.63      journal = handler.get_journal()
    1.64 -    journal.acquire_lock(quota)
    1.65 -
    1.66 -    try:
    1.67 -        freebusy = journal.get_freebusy(quota, organiser)
    1.68 -        handler.remove_from_freebusy(freebusy)
    1.69 -        journal.set_freebusy(quota, organiser, freebusy)
    1.70 -
    1.71 -    finally:
    1.72 -        journal.release_lock(quota)
    1.73 +    freebusy = journal.get_freebusy(quota, organiser)
    1.74 +    handler.remove_from_freebusy(freebusy)
    1.75 +    journal.set_freebusy(quota, organiser, freebusy)
    1.76  
    1.77  def _get_quota_and_identity(handler, args):
    1.78  
    1.79 @@ -300,6 +278,26 @@
    1.80  
    1.81      return quota, organiser
    1.82  
    1.83 +# Locking and unlocking.
    1.84 +
    1.85 +def lock_journal(handler, args):
    1.86 +
    1.87 +    "Using the 'handler' and 'args', lock the journal for the quota."
    1.88 +
    1.89 +    handler.get_journal().acquire_lock(_get_quota(handler, args))
    1.90 +
    1.91 +def unlock_journal(handler, args):
    1.92 +
    1.93 +    "Using the 'handler' and 'args', unlock the journal for the quota."
    1.94 +
    1.95 +    handler.get_journal().release_lock(_get_quota(handler, args))
    1.96 +
    1.97 +def _get_quota(handler, args):
    1.98 +
    1.99 +    "Return the quota using the 'handler' and 'args'."
   1.100 +
   1.101 +    return args and args[0] or handler.user
   1.102 +
   1.103  # Registry of scheduling functions.
   1.104  
   1.105  scheduling_functions = {
   1.106 @@ -307,6 +305,18 @@
   1.107      "schedule_across_quota" : schedule_across_quota,
   1.108      }
   1.109  
   1.110 +# Registries of locking and unlocking functions.
   1.111 +
   1.112 +locking_functions = {
   1.113 +    "check_quota" : lock_journal,
   1.114 +    "schedule_across_quota" : lock_journal,
   1.115 +    }
   1.116 +
   1.117 +unlocking_functions = {
   1.118 +    "check_quota" : unlock_journal,
   1.119 +    "schedule_across_quota" : unlock_journal,
   1.120 +    }
   1.121 +
   1.122  # Registries of listener functions.
   1.123  
   1.124  confirmation_functions = {