Is this light switch installation safe and legal?
My concern here would be for physical abuse

Right now, nothing stops those NM cables from getting whacked/pinched/crushed with books and such, especially if you have heavy tomes on your shelves.  This could be considered a violation of NEC 334.15(A) or (B):


  334.15 Exposed Work. In exposed work, except as provided
  in 300.11(A), cable shall be installed as specified in 334.15(A)
  through (C).
  
  (A) To Follow Surface. Cable shall closely follow the surface
  of the building finish or of running boards.
  
  (B) Protection from Physical Damage. Cable shall be protected from physical damage where necessary by rigid metal
  conduit, intermediate metal conduit, electrical metallic tubing,
  Schedule 80 PVC conduit, Type RTRC marked with the suffix -XW, or other approved means. Where passing through a floor,
  the cable shall be enclosed in rigid metal conduit, intermediate
  metal conduit, electrical metallic tubing, Schedule 80 PVC
  conduit, Type RTRC marked with the suffix -XW, or other
  approved means extending at least 150 mm (6 in.) above the
  floor.
  
  Type NMC cable installed in shallow chases or grooves in
  masonry, concrete, or adobe shall be protected in accordance
  with the requirements in 300.4(F) and covered with plaster,
  adobe, or similar finish.


You have two options to address this

There are two ways to address this issue: you can either replace the NM cables with a wiring method better suited to withstanding said reader-inflicted physical abuse, or you can put something in that will protect the boxes and cables from rough encounters with books while still allowing for access to them.  We'll discuss these options in turn.

Making this more rugged

While the normal wiring methods that are used to withstand the NEC's definition of "physical damage" are the various flavors of heavy-duty rigid conduit (RMC, IMC, EMT, Schedule 80 PVC, and RTRC-XW), surface metal raceway is also rated for such a duty, and doesn't suffer from the bending radius issues that other wiring methods in this application would have to deal with.

If you're using Legrand's Wiremold line for this, you'll need:


Two BW17 right angle raceway-to-1/2" conduit fittings, attached with 1/2" chase nipples and locknuts
Two BWH6 flat elbows
A BWH7 inside elbow
A length of BWH1 raceway
And a two gang metal extension ring for the old switch box


The idea here is that you use the extension ring instead of the normal starter box (as they don't make one in two gang, and two-gang to one-gang mud rings stick out too far) along with one of the BW17 fittings and associated parts to get the raceway going upward from the gang furthest from the inside corner, then use the two flat elbows and the inside elbow to get around the bend and going back down.  Then it's just a matter of using the other BW17 and its friends to transition the wires back into the box.

Once the raceway's in, then, you run individual THHN wires inside it to connect the circuits between the two boxes, just as if you were working in conduit.  (The existing NM cables can be tossed, or shucked and their wires kept for pigtail duty, your choice.)  Don't forget to plug the knockouts where the cables once entered/exited the boxes!

Using some cabinetry to box the boxes

If you feel yourself a better cabinetmaker than electrician, another option for this situation would be to put a cabinet divider wall in just to the right of this, and then add a cabinet door over the front, maintaining access while protecting the existing assembly from physical abuse or damage.  This also has the advantage of being a more finished look, at the cost of having an "un-cabinet" in your house that you can't really use for anything at all.  Making it match the existing built-in bookshelves may be a challenge, as well.